[Lib/Lua] Moses (1.4.0)

Hi all,

Moses bumped to version 1.4.0.
Moses is a Lua library 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 examples:

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 <em>Allan</em> 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

I use Moses a lot. It makes lua much more usable. It’s great!

Thanks for the kind words :slight_smile:

Looks great, wish I had known about this sooner.

This is just awesome! 

I’ve been playing with some 2.5d perspective stuff, and got to a point where I was trying to rotate plane (forest for now) around its Y axis.  This would look ok for 1/2 the rotation, but the trees would overlap at some points, ruining the 3d illusion:

https://www.youtube.com/watch?v=Et0rIRPbepA

I needed a way to sort my table of objects by using a value that was buried in the objects structure: Object.vector.z

…and then with this moses piece:

[lua]

  _.each(_.sort(renderObjects.collection,
    function(a,b )
        if a.vector and b.vector then
           local testA,testB = a.vector.z,  b.vector.z
            return testA > testB
        else
            return true
        end
        
    end) ,
    function(i,v)
        
        if v.DO then
          –      print(i,renderObjects.distanceToCamera(v), v.vector.x,v.vector.y,v.vector.z)                    
            render(v)
            v.DG:toFront()
        end
        
    end )

[/lua]

I was able to make a custom table sorting function that uses the table object’s z vectors  to compare the items in the table.

The following results:

https://www.youtube.com/watch?v=JV5dnmUnonY

Magic, I tell you…

Thanks again, Roland,  This , and your Jumper code are truly excellent . I really appreciate the quality of your work, and the attention to detail

@rdelia : Thanks for the kind words. I spent some time watching those videos. Nice work indeed. :slight_smile:

I use Moses a lot. It makes lua much more usable. It’s great!

Thanks for the kind words :slight_smile:

Looks great, wish I had known about this sooner.

This is just awesome! 

I’ve been playing with some 2.5d perspective stuff, and got to a point where I was trying to rotate plane (forest for now) around its Y axis.  This would look ok for 1/2 the rotation, but the trees would overlap at some points, ruining the 3d illusion:

https://www.youtube.com/watch?v=Et0rIRPbepA

I needed a way to sort my table of objects by using a value that was buried in the objects structure: Object.vector.z

…and then with this moses piece:

[lua]

  _.each(_.sort(renderObjects.collection,
    function(a,b )
        if a.vector and b.vector then
           local testA,testB = a.vector.z,  b.vector.z
            return testA > testB
        else
            return true
        end
        
    end) ,
    function(i,v)
        
        if v.DO then
          –      print(i,renderObjects.distanceToCamera(v), v.vector.x,v.vector.y,v.vector.z)                    
            render(v)
            v.DG:toFront()
        end
        
    end )

[/lua]

I was able to make a custom table sorting function that uses the table object’s z vectors  to compare the items in the table.

The following results:

https://www.youtube.com/watch?v=JV5dnmUnonY

Magic, I tell you…

Thanks again, Roland,  This , and your Jumper code are truly excellent . I really appreciate the quality of your work, and the attention to detail

@rdelia : Thanks for the kind words. I spent some time watching those videos. Nice work indeed. :slight_smile: