Functional-js: lib1 – the base

This library takes all the methods on instances of strings, arrays, objects, numbers, and regexp's and turns them into functions that all:

  1. Are curried
  2. Take only a fixed number of arguments
  3. Returns a single value
  4. Does not mess with the argument(s) you gave the function

Example:

// Normal way:
var a = [1, 2, 3];
a.push(4); // → 4
a; // → [1, 2, 3, 4]


// lib1 way:
var b = [1, 2, 3];
push(4, b); // → [1, 2, 3, 4]
b; // → [1, 2, 3] (b is unaltered)


// We can also curry pop()
var c = [1, 2, 3];
var addFour = pop(4);
addFour(c); // → [1, 2, 3, 4]


// Functions that operate on arrays can (usually) also take strings
// Just think of "hello" as ['h','e','l','l','o'] and it should make sense
push(" world", "hello"); // → "hello world"

Together with a compose() we can do nice things like:

// Currying some functions
var fetchFirstSix = substr(0,6);
var addName = push("Bill"); // concat() will work too

// Combining them
var greet = compose(addName, fetchFirstSix);

greet("Hello world"); // → "Hello Bill"



The docs

charAt mdn

charAt :: int → "a" → "a"
charAt(1, "Hello world"); // → "e"

var thirdChar = charAt(2);
thirdChar("Hello world"); // → "l"

charCodeAt mdn

charCodeAt :: int → "a" → int
charCodeAt(1, "Hello world"); // → 101

concat mdn

concat :: a → a → a
concat("foo", "bar"); // → "barfoo"

concat("foo", [1, 2, 3] ); // → [1, 2, 3, "foo"]

concat([4, 5], [1, 2, 3] ); // → [1, 2, 3, 4, 5]

contains mdn

contains :: "a" → a → Boolean
contains("foo", "foobar"); // → true

contains("foo", [1, 2, "foo", 4] ); // → true

contains("foo", [1, 2, "bar", 4]; // → false

var hasBar = contains("bar");
hasBar("foobar"); // → true

endsWith mdn

endsWith :: "a" → a → Boolean
endsWith( 'world', 'hello world' ); // → true

endsWith('a', [1, 2, 3]); // → false

endsWith('a', [1, 2, 3, 'a'] ); // → true

exec mdn

exec :: RegExp → "a" → [a]
exec( /(hello \S+)/, 'This is a hello world!')[1]; // → "hello world!" 

hasOwnProperty mdn

hasOwnProperty ::
// TODO - Have a look in the mdn documentation just above

indexOf mdn

indexOf :: a → int
indexOf( "o", "hello world!" ); // → 4

indexOf( 2, [1, 2, 3] ); // → 1

isPrototypeOf mdn

isPrototypeOf ::
// TODO - Have a look in the mdn documentation just above

join mdn

join :: [a] → "a"

Currently the only function that takes an array but not a string.

join(" - ", [1, 2, 3]); // → "1 - 2 - 3"

var joinWithDash = join(" - ");
joinWithDash([1, 2, 3]); // → "1 - 2 - 3"

lastIndexOf mdn

lastIndexOf :: a → a → int
lastIndexOf( "o", "hello world!" ); // → 7

lastIndexOf( 2, [1, 2, 3, 2, 4, 1, 0, 2, 4] ); // → 7

length mdn

length :: a → int

Returns the length of a string or array

length( [1, 2, 3] ); // → 3

length( "hello" ); // → 5

localCompare mdn

localCompare ::
// TODO - Have a look in the mdn documentation just above

match mdn

match ::
// TODO - Have a look in the mdn documentation just above

pop mdn

pop :: a → a
var a = [1, 2, 3];
pop(a); // → 3
a; // → [1, 2, 3] (a is unaltered)

// Works for strings too
pop("hello"); // → "o"

pop_ mdn

pop_ :: a → a
var a = [1, 2, 3];
pop_(a); // → [1, 2]
a; // → [1, 2, 3] (a is unaltered)

pop_("hello"); // → "hell"

push mdn

push :: a → a → a
var a = [1, 2, 3];
push('z', a); // → [1, 2, 3, 'z']
a; // → [1, 2, 3] (a is unaltered)

push("oo", "hello"); // → "hellooo"

propertyIsEnumerable mdn

propertyIsEnumerable ::
// TODO - Have a look in the mdn documentation just above

replace mdn

replace ::
// TODO - Have a look in the mdn documentation just above

reverse mdn

reverse :: a → a
reverse("hello"); // → "olleh"

var a = [1, 2, 3]
reverse(a); // → [3, 2, 1]
a; // → [1, 2, 3] (a is unaltered)

shift mdn

shift :: a → a
var a = [1, 2, 3];
shift(a); // → 1
a; // → [1, 2, 3] (a is unaltered)

shift("hello"); // → "h"

shift_ mdn

shift_ :: a → a
var a = [1, 2, 3];
shift_(a); // → [2, 3]
a; // → [1, 2, 3] (a is unaltered)

shift_("hello"); // → "ello"

slice mdn

slice :: int → int → a → a
var a = [1, 2, 3];

slice(1, 3, a); // → [2, 3]

var getFirstTwo = slice(0, 2);
getFirstTwo(a); // → [1, 2]

slice( 1, 10, "hello world" ); // → "ello worl"

sort mdn

sort :: a → a
sort("hello"); // → "ehllo"

sort(["Delta", "alpha", "CHARLIE", "bravo"]);
// → ["CHARLIE", "Delta", "alpha", "bravo"]

splice mdn

splice :: int → int → a → a
splice(2, 3, "Hello world!"); → "He world!"

splice(2, 3, ["zero", "one", "two", "three", "four", "five"]);
// → ["zero", "one", "five"]

splice_ mdn

splice_ :: int → int → a → a → a
splice_(2, 3, "hi", ["zero", "one", "two", "three", "four", "five"]);
// → ["zero", "one", "hi", "five"]

splice_(0, 5, "What a wonderful", "Hello world!")
// → "What a wonderful world!"

split mdn

split ::
// TODO - Have a look in the mdn documentation just above

startsWith mdn

startsWith ::
// TODO - Have a look in the mdn documentation just above

substr mdn

substr ::
// TODO - Have a look in the mdn documentation just above

substring mdn

substring ::
// TODO - Have a look in the mdn documentation just above

test mdn

test ::
// TODO - Have a look in the mdn documentation just above

toExponential mdn

toExponential ::
// TODO - Have a look in the mdn documentation just above

toFixed mdn

toFixed ::
// TODO - Have a look in the mdn documentation just above

toLocaleLowerCase mdn

toLocaleLowerCase ::
// TODO - Have a look in the mdn documentation just above

toLocaleUpperCase mdn

toLocaleUpperCase ::
// TODO - Have a look in the mdn documentation just above

toPrecision mdn

toPrecision ::
// TODO - Have a look in the mdn documentation just above

toString mdn

toString ::
// TODO - Have a look in the mdn documentation just above

toUpperCase mdn

toUpperCase ::
// TODO - Have a look in the mdn documentation just above

trim mdn

trim ::
// TODO - Have a look in the mdn documentation just above

trimLeft mdn non-standard

trimLeft ::
// TODO - Have a look in the mdn documentation just above

trimRight mdn non-standard

trimRight ::
// TODO - Have a look in the mdn documentation just above

unshift mdn

unshift ::
// TODO - Have a look in the mdn documentation just above

valueOf mdn

valueOf ::
// TODO - Have a look in the mdn documentation just above