functional

Gen­eric lib­rary for func­tional pro­gram­ming & work­ing with func­tions.

Source:

Methods

(inner) exec()

Source:

Im­me­di­ately ex­ecute the given func­tion.

const {exec} = re­quire('fer­rum');

// Nor­mal scopes can­not re­turn val­ues
let r;
{
  let x = 42, y = 5;
  r = x + y;
}

// Can be re­writ­ten as
const r = exec(() => {
  let x = 42, y = 5;
  re­turn  x + y;
});

(inner) identity()

Source:

Just a func­tion that re­turns it's ar­gu­ment!

const {iden­tity, list, fil­ter} = re­quire('fer­rum');

iden­tity(null) # => null
iden­tity(42) # => 42

// Iden­tity is some­times use­ful in higher or­der func­tions like
// fil­ter(); this ex­ample for in­stance re­moves all val­ues from
// the list that are falsy
pipe(
  [null, "asd", "", "foo"],
  fil­ter(iden­tity),
  list
);
// => ["asd", "foo"]

(inner) pipe(val, …fns) → {Any}

Source:

Pipeline a value through mul­tiple func­tion calls.

// Some­times you get very nes­ted func­tion in­voc­a­tions:

pipe(
  [1,2,null,3,4,null,5,1,3,2,null,1,4],
  fil­ter(iden­tity),
  uniq,
  map(plus(2)),
)
con­sole.log(pipe(
  4,
  (x) => x+2,
  (x) => x*3
));
// => 18
Parameters:
Name Type Attributes Description
val Any

The value to pipe through the func­tions

fns function <repeatable>

Mul­tiple func­tions

Returns:
Type
Any

(inner) compose(…fns) → {function}

Source:

Func­tion com­pos­i­tion.

This es­sen­tially be­haves like pipe() without tak­ing the ini­tial ar­gu­ment: ex­ecuted left-to-right/​top-to-bot­tom.

const {com­pose} = re­quire('fer­rum');

const fn = com­pose(
  (x) => x+2,
  (x) => x*3,
  (x) => `My Num­ber ${x}`
);

con­sole.log(fn(4)); // => "My Num­ber 18"
Parameters:
Name Type Attributes Description
fns function <repeatable>

Mul­tiple func­tions

Returns:

All the func­tions in the se­quence com­posed into one

Type
function

(inner) withFunctionName(name, fn, Just)

Source:

Manu­ally as­sign a name to a func­tion.

Parameters:
Name Type Description
name String

The new name of the func­tion.

fn function

The func­tion to as­sign a name to

Just function

re­turns fn again.

(inner) curry()

Source:

Auto­curry a func­tion!

ht­tps://​en.wiki­pe­dia.org/​wiki/​Cur­ry­ing

Any func­tion that has a fixed num­ber of para­met­ers may be cur­ried! Cur­ried para­met­ers will be in re­verse or­der. This is use­ful for func­tional pro­gram­ming, be­cause it al­lows us to use func­tion para­met­ers in the suf­fix po­s­i­tion when us­ing no cur­ring:

const toNum­ber = (seq) => map(seq, n => Num­ber(n));

// is the same as

const toNum­ber = map(n => Num­ber(n))

// or even

const toNum­ber = map(Num­ber);

Note how in the second ver­sion we spe­cified the last para­meter first due to cur­ry­ing.

Re­verse or­der only ap­plies in sep­ar­ate in­voc­a­tions:

const sum = (seq) => foldl(seq, 0, (a, b) => a+b);

// is the same as

const sum = foldl(0, (a, b) => a+b);

// or even

con­cat = sum = foldl(0, plus);

Note how in ver­sion two, we spe­cify the para­met­ers in or­der 2, 3, and then 1:

fn(a, b, c) <=> fn(c)(b)(a) <=> fn(b, c)(a)