Coder Social home page Coder Social logo

lua_fun's Introduction

lua_fun is a friendly library to help you get more productive with Lua. It packs common functional programming utilities that might save you some coding in the long run.

compatibility: lua5.3>=

Why?

lua_fun was created because I've not found a module that makes working with Lua and functional programming more productive without adding too much to the stack. lua_fun attempts to provide just enough with good compatibility with Lua standard modules.

How To

# How To Install
luarocks install lua_fun  # install globally
luarocks install --local lua_fun  # or install locally

# How To Generate Docs
ldoc .

# How To Run Tests
busted --lua=/usr/bin/lua

Getting Started

Documentation

lua_fun usage is advised in one of the following ways:

-- per function
local L = require('lua_fun').lambda

-- as a module
local fun = require('lua_fun')

Examples

local fun = require('lua_fun)
local l = fun.lambda
local generator = fun.generator
local totable = fun.totable
local keys = fun.keys
local values = fun.values
local call = fun.call
local compose = fun.compose
local flip = fun.flip
local get = fun.get
local pick = fun.pick
local factory = fun.factory
local map = fun.map
local filter = fun.filter
local reduce = fun.reduce
local zip = fun.zip
local partial = fun.partial
local memoize = fun.memoize

local sum = l'|a, b| a+b'
local sum2 = partial(sum, 2)
local pow2 = partial(flip(math.pow), 2)
local pow4 = compose(pow2, pow2)

assert.are.equal(sum2(3), 5)
assert.are.equal(pow2(3), 9)
assert.are.equal(pow4(2), 16)
assert.are.equal(call(sum, 3, 3), 6)
assert.are.equal(get(3, {5, 6, 7}), 7)
assert.are.equal(pick(2, table.unpack({5, 6, 7})), 6)

local function fibonacci (n)
    if n<3 then return 1 else
        return fibonacci(n-1) + fibonacci(n-2)
    end
end

fibonacci = memoize(fibonacci)  -- cached now =D

local cmp_table = function (a, b)
    for k, v in pairs(a) do
    if v ~= b[k] then return false end
    end

    for k, v in pairs(b) do
    if v ~= a[k] then return false end
    end

    return true
end

local gen, t

gen = filter(l'|v| v % 2 == 0', {1,2,3,4,5,6,7,8})
assert.are.equal(type(gen), 'function')

t = totable(gen)
assert.is_true(cmp_table(t, {2, 4, 6, 8}))

gen = map(l'|v| v * 2', t)
assert.are.equal(type(gen), 'function')

t = totable(gen)
assert.is_true(cmp_table(t, {4, 8, 12, 16}))

assert.are.equal(reduce(sum, t), 40)

local tkeys = totable(keys({a=10, b=20, c=30}))
local tvalues = totable(values({a=10, b=20, c=30}))

-- sort guaranties only for arrays
table.sort(tkeys)
table.sort(tvalues)

assert.are.same(tkeys, {'a', 'b', 'c'})
assert.are.same(tvalues, {10, 20, 30})

lua_fun's People

Contributors

imolein avatar italomaia avatar italomaia-bk avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

lua_fun's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.