On Thu, May 27, 2010 at 2:10 PM, Robert Mann <r...@free.fr> wrote: > > > // Return a list of all books with at least 'threshold' copies sold. > > function bestSellingBooks(threshold) { > > return bookList.filter( > > function (book) { return book.sales >= threshold; } > > ); > > } > > did I get it right?? as I understand it, the "closure" replaces a loop? > Assuming the function book loops through the books? > > Sort-of. The closure does not replace the loop. The closure is called by the loop. The loop is the filter function. Think of it like this (pseudo-code that does not work, but might be easier to read and get the idea across):
function make_adder(N) return function (X) return N+X end end Now, we can do this: add_10 = make_adder(10) add_5 = make_adder(5) Then we can call them like so: add_10(20) = 30 add_5(4) = 9 Now, in your example, there's a function "filter" that takes a function (closure) as an argument. It expects that function to return true or false, and if true, it will keep the element, otherwise it will toss it. More pseudo-code written in RevTalk style: function filter tList, tFunction local tFilteredList repeat for each line tItem in tList if tFunction(tItem) is true then put tItem & cr after tFilteredList end if end repeat return tFilteredList end filter Now, with our function, we could (in theory) do something like so (in a field control): function filterBookByName tTitle -- only keep titles that start with an article if the first word of tTitle is among the items of "a,an,the" then return true end if end filterBookByName And we can pass our lovely filter function to and get rid of a bunch of books: put filter(fld "All Books", filterBookByName) into fld "Filtered Books" Obviously this is just for show to try and indicate how the original JavaScript might translate over into RevTalk conceptually. It doesn't work, it doesn't even come close to doing the exact same thing and can't since RevTalk doesn't support lexical environments. What I did above would be closer to what's known as a first-class function. Hope this helps some. Jeff M. _______________________________________________ use-revolution mailing list use-revolution@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-revolution