Skip to content

Object Functions: List Manipulation and Control Flow

David Roberts edited this page Jul 2, 2023 · 7 revisions
List Manipulation
  • back(list) — gives the last element of a list, or null for an empty list
  • choose(list, (optional)scoring_expr) -> value — choose an item from the list according to which scores the highest according to the scoring expression, or at random by default.
  • choose(list, ffl) — Returns the object which the ffl ranks highest. This works like map or filter. So, for example, to choose the smallest object in a list, we'd say choose([1,2,3], -value). In this case, the list would evaluate to -1,-2,-3 and we'd choose the largest number, -1. So, because 1 evaluated to -1, we'd get 1 from the function.
  • find(list[, element_name='value'], test) — Returns the first element in list for which test returns true, otherwise return null. The test code may reference the element being tested by using the element_name string, like map() and friends do. The default name is 'value'. So, for example, to search for the first truthy element in a list, we would say: find(my_list, value) where mylist is something like [null, false, 'foo']. The return value in this case would be 'foo'. To check for values, we could say find(all_scores, 'score', score>4) where all_scores is a property you defined earlier, returning [1,2,3] at the moment. This would return null, which is falsey, because no score is above 4. To check if any element of a list is in another list, we could say: find(list_one, value in list_two. This would return the first element of list 1 that occurs in list 2.
  • flatten(list) — Returns a list with a depth of 1 containing the elements of any list passed in.
  • float_array(list, (opt) num_elements) -> callable — Converts a list of floating point values into a vector object. The resulting FloatArrayCallable can be converted back to a regular array for access by using .floats or .value. Those keys can be set to an array too, to update the stored value. See short_array() for details. This seems to be largely for engine-internal use, when it wants to pass a large, dense array to FFL to be passed back, so it is suggested to always use a regular array instead where you can.
  • fold(list, expr, [default]) -> any — expr has access to a and b, so to sum a list of numbers fold(list, a+b) works. (This is like value in map.) The optional default, unlike Javascript's more standard Array.reduce, is used only when list is empty.
  • head(list) — gives the first element of a list, or null for an empty list
  • index(list, value) -> index of value in list — Returns the index of the value in the list or -1 if value wasn't found in the list.
  • keys(map) -> list — gives the keys for a map
  • map(list A, B, ffl C) — For each element in list A, run C on it with the element available to C as whatever B is. B is evaluated as a string. For example, map(range(10), start_number, start_number*2) would return a list of even numbers from 0 to 18 inclusive.
  • mapping(x) — Turns the args passed in into a map. The first arg is a key, the second a value, the third a key, the fourth a value and so on and so forth.
  • range([start, ]finish): Returns a list containing all numbers smaller than the finish value and and larger than or equal to the start value. The start value defaults to 0.
  • range([start, ]finish[, step]) — Returns a list containing all numbers smaller than the finish value and and larger than or equal to the start value. The start value defaults to 0.
  • reduce() — See fold().
  • reverse(list) — reverses the given list
  • short_array(list) -> callable — Converts a list of integer values into a vector object. The resulting ShortArrayCallable can be converted back to a regular array for access by using .shorts or .value. Those keys can be set to an array too, to update the stored value. See also: float_array() This seems to be largely for engine-internal use, when it wants to pass a large, dense array to FFL to be passed back, so it is suggested to always use a regular array instead where you can. eg. short_array([1,2,3]).value = [1,2,3]
  • shuffle(list) - Returns a shuffled version of the list. Like shuffling cards.
  • sort(list, criteria) — Returns a nicely-ordered list. If you give it an optional formula such as a>b as the criteria it will sort it according to that. This example favours larger numbers first instead of the default of smaller numbers first. The criteria should be deterministic, or else the engine may segfault. A function, such as def(a,b) a > b is not deterministic and will not sort anything.
  • sum(list[, counter]) — Adds all elements of the list together. If counter is supplied, all elements of the list are added to the counter instead of to 0.
  • transform(list, ffl) — Deprecated in favour of map(). Each element of list has the ffl executed on it. This executed FFL is returned in a new list. The list element is accessed via v, and the iteration is accessed by i. Example: transform(['a','b','c'], v+i) produces ['a0', 'b1', 'c2'].
  • unique(list) — Returns a (sorted) list without duplicates.
  • unzip(list of lists) -> list of lists — Converts [[1,4],[2,5],[3,6]] -> [[1,2,3],[4,5,6]]
  • values(map) -> list — gives the values for a map
  • zip(list1, list2, expr) -> list3. Each element in list 3 is the elements from list 1 and list 2 ran through expr(a, b).
Control Flow
  • if(ffl_1, ffl_2[, ffl_3]) — When ffl_1 evaluates to a true value, return (and run) ffl_2. Otherwise, return ffl_3 if it's defined, and null if it's not.
  • switch(A, B, C[, D, E[, F, G [, ..., ..., [, Z]]]]) — Evaluate A. If B equals A, run C. Otherwise, if D equals A, evaluate E. Repeat for all args. If we have at the end one even-lettered arg without an odd match to run, this becomes the default value of the function which is evaluated if all else fails.
  • default(a,b) — If a is false, use b instead. (Basically, wraps if(a, a, b).)
Clone this wiki locally