Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Moses, a utility library for functional programming — Gideros Forum

Moses, a utility library for functional programming

Roland_YRoland_Y Member
edited July 2014 in Code snippets
Hi all,

Moses bumped to version 1.4.0.
Moses is mostly meant for functional programming, with Lua. It provides functions to operates on tables array-style, list-style, collection-style or object-style.

Here are some example:
local _ = require 'moses'
 
-- Creates an array of values from 1 to 20
local t = _.range(1,20)
 
-- Filter even values
t = _.select(t, function(i,v) return v%2~=0 end)
 
-- Double all values
t = _.map(t, function(i,v) return v*2 end)
The code below does the exact same thing, but uses chaining:
local _ = require 'moses'
local t = _:chain(_.range(1,20))
  :select(function(i,v) return v%2~=0 end)
  :map(function(i,v) return v*2 end)
  :value()
Or let us consider a dataset of records:
local logs = {
  {name = 'Allan', timestamp = 00578},  {name = 'John', timestamp = 20578},
  {name = 'Ronald', timestamp = 00579},  {name = 'Ronald', timestamp = 30578},
  {name = 'John', timestamp = 0057},  {name = 'Allan', timestamp = 0678},
  {name = 'Allan', timestamp = 00278},  {name = 'Peter', timestamp = 06578},
  {name = 'Peter', timestamp = 30578},  {name = 'Allan', timestamp = 0878},
  {name = 'Steve', timestamp = 50578},  {name = 'John', timestamp = 078},
}
We want to find out how many times Allan got connected:
print(_.count(_.pluck(logs, 'name'), 'Allan')) --> 4
Or find out the max timestamp:
print(_.max(_.pluck(logs, 'timestamp'))) --> 578
Or get the list of the unique visitors to the database:
_.each(_.unique(_.pluck(logs, 'name')),print) --> 'Allan', 'John', 'Ronald', 'Peter', 'Steve'
or the same list, chaining style:
_(logs):pluck('name'):unique():each(print) --> 'Allan', 'John', 'Ronald', 'Peter', 'Steve'
Find a complete tutorial here on Moses' API. The documentation is also available online, on in HTML format bundled with the library.

Moses 1.4.0 : source | Github | Url

Comments

Sign In or Register to comment.