Functional Programming Types of Useful Functions

Published on Author craigzearfossLeave a comment

List utilities:

  • head() — get first element
  • tail() — get all but first elements
  • last() — get last element
  • length() — count of elements

Predicates / comparators (test an element, return boolean):

  • equal()
  • greaterThan()
  • lessThan()

List transformations:

  • map() ([x]) -> [y] take list x and apply a transformation to each element in that list, return new list y
  • reverse() ([1, 2, 3]) -> [3, 2, 1]

List searches:

  • find() ([x]) -> x take list x and return first element that matches a predicate
  • filter() ([x]) -> [y] take list x and return all items matching predicate

List Reducers / folds:

  • reduce() — ([x], function[, accumulator]) -> apply function to each element and accumulate the results as a single value
  • any() — true if any values match predicate
  • all() — true if all values match predicate
  • sum() — total of all values
  • product() — product of all values
  • maximum() — highest value
  • minimum() — lowest value
  • concat() — take a list of lists and return a single, concatenated list in list order
  • merge() — take a list of lists and return a single, merged list in element order

Iterators / Generators / Collectors (infinite lists):

  • sample() — return current value of continuous input source (temperature, form input, toggle switch state, etc…)
  • repeat() — (1) -> [1, 1, 1, 1, 1, 1,…]
  • cycle() / loop() — When the end of the list is reached, wrap around to the beginning again.

Source:

Leave a Reply

Your email address will not be published. Required fields are marked *