User Tools

Site Tools


Sidebar

Passage markup

basics

coding

extra

list

section

whitespace

Macros

basics

colour

data structure

date and time

game state

live

maths

number

revision

saving

showing and hiding

string

styling

url

deprecated

Types of data

Special keywords

Special passage tags

harlowe:lambda

Lambda data

Suppose you want to do a complicated task with an Array, like, say, convert all of its strings to lowercase, or check if its datamap have “health” data equal to 0, or join all of its strings together into a single string. You want to be able to tell Harlowe to search for “each string where the string's 1st letter is A”. You want to write a “function” for how the search is to be conducted.

Lambdas are user-created functions that let you tell certain macros, like (find:), (altered:) and (folded:), precisely how to search, alter, or combine the data provided to them.

There are several types of lambdas.

  • “where” lambdas, used by the (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 (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.
  • “making” lambdas, used by the (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 (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.

Lambdas use temp variables as “placeholders” for the actual values. For instance, in (find: _num where _num > 2, 5,6,0), the temp variable _num is used to mean each individual value given to the macro, in turn. It will be 5, 6 and 0, respectively. Importantly, this will not alter any existing temp variable called _num - the inside of a lambda can be thought of as a hook, so just as the inner _x in (set: _x to 1) |a>[ (set:_x to 2) ] is different from the outer _x, the _num in the 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 could, for instance, (set: $statsReadout to (_stat making _readout via _readout + "|" + _stat's name + ":" + _stat's value)), and then use $printStats with the (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. 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.

harlowe/lambda.txt · Last modified: 2017/10/10 00:39 (external edit)