/lamda

A functional programming library for Lua, inspired by Ramda.

Primary LanguageLuaMIT LicenseMIT

lamda

CircleCI Coverage Status

A functional programming library for Lua, inspired by Ramda.

Install

$ luarocks install lamda

Usage

local lamda = require('lamda')

local shout = lamda.pipe(string.upper, function(s) return s .. '!' end)

shout('lamda rocks') -- returns 'LAMDA ROCKS!'

API

flip(fn)

Creates a new function with the order of the first two arguments reversed.

local join = lamda.flip(table.concat)

join(',', {1, 2, 3}) -- returns '1,2,3'

join(separator, tbl)

Creates a new string by concatenating all elemements and putting a separator in between.

lamda.join(',', {1, 2, 3}) -- returns '1,2,3'

concat(first_table, second_table)

Creates a new array by concatenating the given arrays.

lamda.concat({1, 2, 3}, {4, 5, 6}) -- returns {1, 2, 3, 4, 5, 6}

pipe(fn, ...)

Creates a new function by composing the given functions from left to right. The first function may have any arity while the remaining functions must be unary.

local function add(a, b)
  return a + b
end

local function square(n)
  return n ^ 2
end

local add_and_square = lamda.pipe(add, square)

add_and_square(2, 3) -- returns 25

partial(fn, ...)

Takes a function f and a variable number of arguments and creates a new function g. When applied, g returns the result of applying f to theinitially provided arguments followed by the arguments to g.

local function prepend(first_string, second_string)
  return
end

local add_two = lamda.partial(add, 2)

partial_right(fn, ...)

any_pass(predicate, ...)

sort(comparator, tbl)

Creates a new array by sorting the given array according to a comparator function. The comparator function receives two arguments and must return true if the first argument should come first in the sorted array.

local numbers = {2, 1, 3}

lamda.sort(function(a, b) return a < b end, numbers) -- returns {1, 2, 3}