User Tools

Site Tools


harlowe:folded

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

harlowe:folded [2017/10/09 20:39] (current)
Line 1: Line 1:
 +**(folded: // [[harlowe:​Lambda|lambda]],​ ...Any//) -> //Any//**
  
 +This takes a "​making"​ [[harlowe:​lambda|lambda]] and a sequence of values, and creates a new value (the "​total"​) by feeding every value in the
 +sequence to the lambda, akin to folding a long strip of paper into a single square.
 +
 +=== Example usage: ===
 +
 +  * ''​%%(folded:​ _enemy making _allHP via _allHP + _enemy'​s hp, ...$enemies)%%''​ will first set _sum to $enemies'​s 1st's hp, then add the remaining hp values in $enemies to it.
 +  * ''​%%(folded:​ _name making _allNames via _allNames + "/"​ + _name, ...(history:​ ))%%''​ will create a [[harlowe:​string|string]] of every passage name in the [[harlowe:​history|(history:​)]] [[harlowe:​array|array]],​
 +separated by a forward slash.
 +
 +=== Rationale: ===
 +
 +The [[harlowe:​for|(for:​)]] macro, while intended to display multiple copies of a hook, can also be used to run a single macro call multiple times. You may
 +wish to use this to repeatedly [[harlowe:​set|(set:​)]] a variable to itself plus one of the looped values (or some other operation). (folded:) is meant
 +to let you perform this in a shorter, more fluid fashion.
 +
 +Consider, first of all, a typical [[harlowe:​for|(for:​)]] and [[harlowe:​set|(set:​)]] loop such as the following:
 +
 +<​code>​
 +{(set:​$allNames to ""​)
 +(for: each _name, ...(history:​ ))[
 +    (set:​$allNames to it + "/"​ _name)
 +]}
 +You've visited: $allNames
 +</​code>​
 +This can be rewritten using (folded:) as follows. While this version may seem a little harder to read if you're not used to it, it
 +allows you to accomplish the same thing in a single line, by immediately using the macro'​s provided value without a variable:
 +
 +<​code>​
 +You've visited: (folded: _name making _allNames via _allNames + "/"​ + _name, ...(history:​ )))
 +</​code>​
 +This macro uses a lambda (which is the "temp variable ''​%%making%%''​ another temp variable ''​%%via%%''​ expression"​ expression) to run the
 +expression using every provided value, much like those repeated [[harlowe:​set|(set:​)]] calls.
 +
 +If you need to perform this operation at various different times in your story, you may wish to [[harlowe:​set|(set:​)]] the lambda into a variable,
 +so that you, for instance, might need only write:
 +
 +<​code>​
 +You've visited: (folded: $namesWithForwardSlashes,​ ...(history:​ )))
 +</​code>​
 +=== Details: ===
 +
 +Of course, if at any time the expression should cause an error, such as adding a [[harlowe:​number|number]] to a string, then an error will result.
 +
 +Both of the temp variables, the value and the total, can be named anything you want. As with other lambda macros, they don't exist
 +outside of it, won't alter identically-named temp variables outside of it, and can't be manually [[harlowe:​set|(set:​)]] within the lambda.
 +
 +You can refer to other variables, including other temp variables, in the ''​%%via%%''​ expression. For instance, you can write
 +''​%%(folded:​ _score making _totalScore via _totalScore + _score * _bonusMultiplier)%%''​. However, for obvious reasons,
 +if the outer temp variable is named the same as the lambda'​s temp variables, it can't be referred to in the expression.
 +
 +You can also use a "​where"​ clause inside the "​making"​ lambda to prevent an operation from occurring if a value isn't suitable -
 +''​%%(folded:​ _item making _total via _total + _item where _item > 0, ...$arr)%%''​ will only sum up the values in $arr which are greater than 0.
 +
 +=== See also: ===
 +
 +[[harlowe:​for|(for:​)]],​ [[harlowe:​altered|(altered:​)]]
harlowe/folded.txt ยท Last modified: 2017/10/09 20:39 (external edit)