Functional Programming

From Federal Burro of Information
Revision as of 18:58, 17 November 2020 by David (talk | contribs) (Created page with "notes from the field. In general, new_collection = collection.map(function) is a short-cut for: new_collection[] for (element in collection) new_collection[] += funct...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

notes from the field.

In general,

new_collection = collection.map(function)

is a short-cut for:

new_collection[]
for (element in collection)
 new_collection[] += function(element)

And:

new_collection = collection.filter(function)

is a short-cut for:

new_collection[]
for (element in collection):
  if function(element):
    new_collection[] += function(element)

this

doubled = [1, 2, 3, 4, 5].map(x => x * 2)

is equivalent to:

doubled[]
for (x in [1, 2, 3, 4, 5]):
  doubled[] += x * 2

that