Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Lambda Functions

1383104764

Edited 1383105115
Lithl
Pro
Sheet Author
API Scripter
So. Those of us who create scripts regularly probably know how to pass around functions as parameters to other functions -- we do it every time we use the on function that's part of Roll20 or the each function from underscore. Whether we make use of it in any other context is another story, in part because it can be kind of clunky. function where(list, predicate) { var result = []; _.each(list, function(val) { if(predicate(val)) result.push(val); }); } var mylist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']; log(where(mylist, function(val) { return val.charCodeAt(0) > 50; })); In some other languages and in mathematics, simple functions (or not so simple, as the case can sometimes be) can be represented using lambda . Generally, the syntax is something like "x => x * x" or "(a, b) => a == b". In C# syntax, for example, the above call to where could be: where(mylist, val => val.charCodeAt(0) > 50); It's certainly cleaner to read! Unfortunately, Javscript does not have lambdas. ... until someone writes a library! There are actually a couple available. I've used Functional Javascript , myself. If you download it, the to-function.js file is what creates lambdas. Even better, to-function.js is only 220 lines with extensive comments. If you strip out the documentation (and the copyright *cough*), it's under 100, and not even all of that is required for the general use case! With to-function in a campaign, I can do lambdas like this: var myfunc = 'x -> x+5'.lambda(); log(myfunc(10)); // -> log prints "15" var myvar = 'x -> x * x'.lambda()(5); log(myvar); // -> log prints "25" log('x -> y -> x + 2 * y'.lambda()(1)(2)); // -> log prints "5" log('a, b -> a == b'.lambda()(5, 15)); // -> log prints "false" The lambdas created by to-function even have access to other variables and functions in the scope: on('chat:message', 'msg -> log(msg.who)'.lambda()); // -> log prints player/character name when they post a chat message on('chat:message', 'msg -> log(msg.who), sendChat("player|"+msg.playerid, "/me is speaking in tongues!")'.lambda()); // -> log prints player/character name when they post a chat message, and then posts an emote to chat... and then repeats // because I didn't write any checks into the silly lambda example. Executing this line will lock up a campaign! =P Lambdas are tons of fun when you've got a clean syntax for them. Obviously, lambda functions aren't the answer to every problem. But they can be quite useful. Now if only we could get some kind of requires() functionality from a script distribution center... /hint hint
Very interesting