/FN

A collection of useful kinda, sorta functional... Functions for JavaScript.

Primary LanguageJavaScriptGNU Affero General Public License v3.0AGPL-3.0

Classes

FN

Functions

any(lst)boolean

FN.any is a function which evaluates a number of expressions and returns true if ANY of the expressions themselves are true.

Example(s):

FN.any([1 === 1, 5 === 5]);

all(lst)boolean

FN.all is a function which evaluates a number of expressions and returns true only if ALL of the expressions are themselves true.

Example(s):

FN.all([1 === 1, 5 === 5]);

first(lst)object | undefined

FN.first returns the first element of a list.

Example(s):

FN.first([1, 2, 3, 4, 5]);

last(lst)object | undefined

FN.last is the inverse of FN.first and returns the final element in a list.

Example(s):

FN.last([1, 2, 3, 4, 5]);

nth(lst, n)object | undefined

FN.nth complements FN.first and FN.last by providing a means to grab an arbitrary element in a list by numeric index.

Example(s):

FN.nth([1, 2, 3, 4, 5], 2);

rest(lst)array | undefined

FN.rest complements FN.first by passing everything execept the first element into a callback.

Example(s):

FN.rest([1, 2, 3, 4, 5]);

take(lst, n)array

FN.take returns a new list from the n number of elements from the list passed into it.

Example(s):

FN.take(["Lions", "Tigers", "Bears"], 2);

FN.take(FN.range(10, 0, 2), 2);

if(cond, cb)object | undefined

FN.if is a single branch function. It expects a callback and a single boolean expression.

NOTE: FN.any and FN.all can be used here.

Example(s):

FN.if(true, () => { console.log("Is true"); });

FN.if(FN.any([1 === 1, 2 === 2]), () => { console.log("Is true"); });

ifElse(cond, cb1, cb2)object | undefined

FN.ifElse expands upon FN.if by permitting the user to provide a second callback function to be executed in the event that the if expression evaluates to false.

NOTE: Multiple FN.fElse can be nested inside of the callback functions.

Example(s):

FN.ifElse(true, () => { console.log("Is true"); },() => { console.log("Is false"); });

FN.ifElse(FN.all([1 === 1, 2 === 2]), () => { console.log("Is true"); }, () => { console.log("Is false"); });

let(objectContext, cb)object

FN.let creates a new isolated execution context with a set of values initilised within the context and visible only for the duration of the callback function. Note that it acts as a closure and does have access to outer variables.

Example(s):

FN.let({"age": 29, "name": "Neil Munro"}, function() { console.log("Hi my name is " + this.name + " and I am " + this.age + " years old."); });

range([start], stop, [step])array

FN.range generates and array of numeric values based on criteria that the programmer enters.

Example(s):

FN.range(0, 10, 1);

cond(...lst)object | undefined

FN.cond is analogous to a switch statement. It evaluates each expression in turn until it first the first one that evaluates to true and runs its acompanying function.

Example(s):

FN.cond( 1 === 2, () => { console.log("first"); }, 2 === 3, () => { console.log("second"); }, "tmp" === "tmp", () => { console.log("third"); } );

everyOther(cb, arr, step)undefined

FN.everyOther is a function for applying the callback for every N elemet in an array.

Example(s):

FN.everyOther((elm) => { console.log(elm); }, [0, 1, 2, 3, 4, 5, 6], 2);

case(val, ...lst)object | undefined

FN.case is a function that evaluates a set of conditions against a sentinal condition, the acompanying function is ran. A default condition can be passed and a final callback passed for the default condition.

Example(s):

FN.case(9, 1, () => 12, 2, () => 22, 3, () => 3*2, FN.default, () => 19);

sum(lst)number | undefined

FN.sum is a function that takes a variable number of arguments and returns the sum of all arguments all arguments must be numbers, if not undefined is returned.

Example(s):

FN.sum([1, 2, 3, 4, 5]);

FN

Kind: global class
Author: Neil Munro neilmunro@gmail.com

new FN()

FN is a functional library providing functions inspired (ripped off) by LISP/Clojure.

No, I have no shame.

This is kinda just research, experimenting with functional programming in JS. If you however find this library useful let me know and I'll keep working on it, if you have suggestions, I might even incorporate your ideas.

This is designed to be used from within ES6. Other older versions of ES may not work properly.

Copyright (c) Neil Munro 2015-2016.

any(lst) ⇒ boolean

FN.any is a function which evaluates a number of expressions and returns true if ANY of the expressions themselves are true.

Example(s):

FN.any([1 === 1, 5 === 5]);

Kind: global function
Returns: boolean - The result of any of the expressions being true.

Param Type Description
lst array The list of boolean expressions to FN.any.

all(lst) ⇒ boolean

FN.all is a function which evaluates a number of expressions and returns true only if ALL of the expressions are themselves true.

Example(s):

FN.all([1 === 1, 5 === 5]);

Kind: global function
Returns: boolean - The result of all of the expressions being true.

Param Type Description
lst array The list of boolean expressions to FN.all.

first(lst) ⇒ object | undefined

FN.first returns the first element of a list.

Example(s):

FN.first([1, 2, 3, 4, 5]);

Kind: global function
Returns: object | undefined - The first element of the list or undefined.

Param Type Description
lst array The list to get the first element of.

last(lst) ⇒ object | undefined

FN.last is the inverse of FN.first and returns the final element in a list.

Example(s):

FN.last([1, 2, 3, 4, 5]);

Kind: global function
Returns: object | undefined - The last element of the list or undefined.
See: FN.first

Param Type Description
lst array The arguments to FN.last.

nth(lst, n) ⇒ object | undefined

FN.nth complements FN.first and FN.last by providing a means to grab an arbitrary element in a list by numeric index.

Example(s):

FN.nth([1, 2, 3, 4, 5], 2);

Kind: global function
Returns: object | undefined - The nth element of the list or undefined.
See

  • FN.first
  • FN.last
Param Type Description
lst array The list to get the nth element of.
n number The nth element in the list to try and get.

rest(lst) ⇒ array | undefined

FN.rest complements FN.first by passing everything execept the first element into a callback.

Example(s):

FN.rest([1, 2, 3, 4, 5]);

Kind: global function
Returns: array | undefined - The rest of the list or undefined.
See: FN.first

Param Type Description
lst array The arguments to FN.rest.

take(lst, n) ⇒ array

FN.take returns a new list from the n number of elements from the list passed into it.

Example(s):

FN.take(["Lions", "Tigers", "Bears"], 2);

FN.take(FN.range(10, 0, 2), 2);

Kind: global function
Returns: array - A new list made up of the n number of elements, if n is bigger than the list the whole list is returned.

Param Type Description
lst array The arguments to FN.take.
n number The number of elements to take from the array lst.

if(cond, cb) ⇒ object | undefined

FN.if is a single branch function. It expects a callback and a single boolean expression.

NOTE: FN.any and FN.all can be used here.

Example(s):

FN.if(true, () => { console.log("Is true"); });

FN.if(FN.any([1 === 1, 2 === 2]), () => { console.log("Is true"); });

Kind: global function
Returns: object | undefined - The result of the callback or undefined.
See: FN.ifElse

Param Type Description
cond boolean The single boolean expression to FN.if.
cb function The callback to execute if true.

ifElse(cond, cb1, cb2) ⇒ object | undefined

FN.ifElse expands upon FN.if by permitting the user to provide a second callback function to be executed in the event that the if expression evaluates to false.

NOTE: Multiple FN.fElse can be nested inside of the callback functions.

Example(s):

FN.ifElse(true, () => { console.log("Is true"); },() => { console.log("Is false"); });

FN.ifElse(FN.all([1 === 1, 2 === 2]), () => { console.log("Is true"); }, () => { console.log("Is false"); });

Kind: global function
Returns: object | undefined - The result of the callback or undefined.
See: FN.if

Param Type Description
cond boolean The boolean expression to FN.ifElse.
cb1 function The callback to execute in the form: () => {...} if true.
cb2 function The callback to execute in the form: () => {...} if false.

let(objectContext, cb) ⇒ object

FN.let creates a new isolated execution context with a set of values initilised within the context and visible only for the duration of the callback function. Note that it acts as a closure and does have access to outer variables.

Example(s):

FN.let({"age": 29, "name": "Neil Munro"}, function() { console.log("Hi my name is " + this.name + " and I am " + this.age + " years old."); });

Kind: global function
Returns: object - The return value of the callback function.
See: FN.if

Param Type Description
objectContext object An object containing the key/value pairs
cb function The callback to execute in the form: function() {...}. You MUST use the original function() {} form as these bind a 'this' value to the scope that FN.let provides. that are to be created inside the execution context.

range([start], stop, [step]) ⇒ array

FN.range generates and array of numeric values based on criteria that the programmer enters.

Example(s):

FN.range(0, 10, 1);

Kind: global function
Returns: array - The array built from lst.

Param Type Description
[start] number The number to start from.
stop number The number to go to.
[step] number The number of steps/intervals.

cond(...lst) ⇒ object | undefined

FN.cond is analogous to a switch statement. It evaluates each expression in turn until it first the first one that evaluates to true and runs its acompanying function.

Example(s):

FN.cond( 1 === 2, () => { console.log("first"); }, 2 === 3, () => { console.log("second"); }, "tmp" === "tmp", () => { console.log("third"); } );

Kind: global function
Returns: object | undefined - The result of the executed function or undefined.

Param Type Description
...lst boolean | function An array of expression/function pairs to evaluate/run.

everyOther(cb, arr, step) ⇒ undefined

FN.everyOther is a function for applying the callback for every N elemet in an array.

Example(s):

FN.everyOther((elm) => { console.log(elm); }, [0, 1, 2, 3, 4, 5, 6], 2);

Kind: global function
Returns: undefined - everyOther does not return anything it applies a callback for every other element in a list.

Param Type Description
cb function The function to apply to n elements of the array.
arr array The array containing the elements to apply the function to.
step number The interval of steps to apply a function to.

case(val, ...lst) ⇒ object | undefined

FN.case is a function that evaluates a set of conditions against a sentinal condition, the acompanying function is ran. A default condition can be passed and a final callback passed for the default condition.

Example(s):

FN.case(9, 1, () => 12, 2, () => 22, 3, () => 3*2, FN.default, () => 19);

Kind: global function
Returns: object | undefined - The result of the executed function or undefined.

Param Type Description
val number | string The sentinal condition.
...lst number | string | boolean | function The condition/function pairs to check against the sentinal and execute, if true.

sum(lst) ⇒ number | undefined

FN.sum is a function that takes a variable number of arguments and returns the sum of all arguments all arguments must be numbers, if not undefined is returned.

Example(s):

FN.sum([1, 2, 3, 4, 5]);

Kind: global function
Returns: number | undefined - The sum of the provided arguments.

Param Type Description
lst array The list of numbers to sum.