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:altered

(altered: lambda, …Any) → array

This takes a “via” lambda and a sequence of values, and creates a new array with the same values in the same order, but altered via the operation in the lambda's “via” clause.

Example usage:

  • (altered: _monster via "Dark " + _monster, "Wolf", "Ape", "Triffid") produces (a: "Dark Wolf", "Dark Ape", "Dark Triffid")
  • (altered: _player via _player + (dm: "HP", _player's HP - 1), ...$players) produces an array of $players datamap whose “HP” datavalue is decreased by 1.

Rationale:

Transforming entire arrays or dataset, performing an operation on every item at once, allows arrays to be modified with the same ease that single values can - just as you can add some extra text to a string with a single +, so too can you add extra text to an entire array of strings using a single call to (altered:).

This macro uses a lambda (which is just the “temp variable via an expression” expression) to take each item in the sequence and produce a new value to populate the resulting array. For (altered: _a via _a + 1, 10,20,30) it will produce 10 + 1, 20 + 1 and 30 + 1, and put those into a new array.

Details:

Of course, if any operation applied to any of the values should cause an error, such as trying to add a string to a number, an error will result.

The temp variable, which you can name anything you want, is controlled entirely by the lambda - it doesn't exist outside of it, it won't alter identically-named temp variables outside of it, and you can't manually (set:) it within the lambda.

You can refer to other variables, including other temp variables, in the via expression. For instance, you can write (altered: _object via _playerName + "'s " + _object, "Glove", "Hat", "Purse"). However, for obvious reasons, if the outer temp variable is named the same as the lambda's temp variable, it can't be referred to in the expression.

If no values are given to (altered:) except for the lambda, an empty array will be produced.

See also:

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