This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
harlowe:lambda [2017/10/09 20:39] 127.0.0.1 external edit |
harlowe:lambda [2019/04/16 04:08] (current) l |
||
---|---|---|---|
Line 12: | Line 12: | ||
* "where" lambdas, used by the [[harlowe:find|(find:)]] macro, are used to search for and filter data. The lambda ''%%_item where _item's 1st is "A"%%'' tells the macro to searches for items whose ''%%1st%%'' is the string "A". | * "where" lambdas, used by the [[harlowe:find|(find:)]] macro, are used to search for and filter data. The lambda ''%%_item where _item's 1st is "A"%%'' tells the macro to searches for items whose ''%%1st%%'' is the string "A". | ||
* "via" lambdas, used by the [[harlowe:altered|(altered:)]] macro, are used to transform and change data. The lambda ''%%_item via _item + "s"%%'' tells the macro to add the string "s" to the end of each item. | * "via" lambdas, used by the [[harlowe:altered|(altered:)]] macro, are used to transform and change data. The lambda ''%%_item via _item + "s"%%'' tells the macro to add the string "s" to the end of each item. | ||
+ | * "when" lambdas are a variant of "where" used exclusively by [[harlowe:event|(event:)]], and are used to specify a live event when a hook should be shown. The lambda ''%%when $fuel > 8%%'' tells [[harlowe:event|(event:)]] to show the attached hook when ''%%$fuel%%'' is increased (due to an interaction macro like [[harlowe:link-repeat|(link-repeat:)]], a [[harlowe:live|(live:)]] macro, or anything else). This really shouln't be called a "lambda", but you can perhaps think of it in terms of it filtering moments in time that pass or fail the condition. | ||
* "making" lambdas, used by the [[harlowe:folded|(folded:)]] are used to build or "make" a single data value by adding something from each item to it. The lambda ''%%_item making _total via _total + (max: _item, 0)%%'' tells the macro to add each item to the total, but only if the item is greater than 0. (Incidentally, you can also use "where" inside a "making" lambda - you could rewrite that lambda as ''%%_item making _total via _total + _item where _item > 0%%''.) | * "making" lambdas, used by the [[harlowe:folded|(folded:)]] are used to build or "make" a single data value by adding something from each item to it. The lambda ''%%_item making _total via _total + (max: _item, 0)%%'' tells the macro to add each item to the total, but only if the item is greater than 0. (Incidentally, you can also use "where" inside a "making" lambda - you could rewrite that lambda as ''%%_item making _total via _total + _item where _item > 0%%''.) | ||
* For certain macros, like [[harlowe:for|(for:)]], you may want to use a "where" lambda that doesn't filter out any of the values - ''%%_item where true%%'', for instance, will include every item. There is a special, more readable shorthand for this type of "where" lambda: writing just ''%%each _item%%'' is equivalent. | * For certain macros, like [[harlowe:for|(for:)]], you may want to use a "where" lambda that doesn't filter out any of the values - ''%%_item where true%%'', for instance, will include every item. There is a special, more readable shorthand for this type of "where" lambda: writing just ''%%each _item%%'' is equivalent. | ||
Line 21: | Line 22: | ||
lambda will not affect any other ''%%_num%%''. | lambda will not affect any other ''%%_num%%''. | ||
- | An important feature is that you can save lambdas into variables, and reuse them in your story easily. You | + | You can use the "it" shorthand to save on having to write the temporary variable's name multiple times. ''%%_num where _num > 2%%'' can be rewritten as''%%_num where it > 2%%''. Not only that, but you can even save on naming the temporary variable at all, by just using ''%%where%%'' (or ''%%via%%'' or ''%%making%%'') without the name and only using ''%%it%%'' to refer to the variable: ''%%where it > 2%%''. |
- | could, for instance, ''%%(set: $statsReadout to (_stat making _readout via _readout + "|" + _stat's name + ":" + _stat's value))%%'', | + | |
- | and then use $printStats with the [[harlowe:folded|(folded:)]] macro in different places, such as ''%%(folded: $statsReadout, ...(dataentries: $playerStats))%%'' for displaying the player's stats, ''%%(folded: $statsReadout, ...(dataentries: $monsterStats))%%'' for a monster's stats, etc. | + | An important feature is that you can save lambdas into variables, and reuse them in your story easily. You could, for instance, ''%%(set: $statsReadout to (_stat making _readout via _readout + "|" + _stat's name + ":" + _stat's value))%%'', and then use $printStats with the [[harlowe:folded|(folded:)]] macro in different places, such as ''%%(folded: $statsReadout, ...(dataentries: $playerStats))%%'' for displaying the player's stats, ''%%(folded: $statsReadout, ...(dataentries: $monsterStats))%%'' for a monster's stats, etc. |
Lambdas are named after the lambda calculus, and the "lambda" keyword used in many popular programming languages. | Lambdas are named after the lambda calculus, and the "lambda" keyword used in many popular programming languages. | ||
They may seem complicated, but as long as you think of them as just a special way of writing a repeating instruction, | They may seem complicated, but as long as you think of them as just a special way of writing a repeating instruction, | ||
and understand how their macros work, you may find that they are very convenient. | and understand how their macros work, you may find that they are very convenient. |