TOPIC: A Trick Borrowed from JavaScript
#1286
A Trick Borrowed from JavaScript 1 Year, 7 Months ago
I just finished reading JavaScript: The Good Parts (recommended)

One of its examples of functional programming seemed like a good thing to add to Suneido.

The goal is to write something like:
Code:

list = [[name: 'Fred', age: 23], [name: 'Andy', age: 45]]
list.Sort(By(#age))


To enable this, you define By as:
Code:

function (field)
    {
    return {|x, y| x[field] < y[field] }
    }


Unlike JavaScript, Suneido functions are not lexically scoped, so they would not work for this. But blocks are lexically scoped so they work fine.

Note: In Suneido you could also write this as:

Code:

list.Sort({|x, y| x.name < y.name })
 
 
Last Edit: 2009/01/07 16:39 By andrew.
 
andrew