Lambdajs

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"



Strings

charAt mdn

charAt :: Int → String → String
charAt(1, "Hello world"); // → "e"

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

charCodeAt mdn

charCodeAt :: Int → String → Int
charCodeAt(1, "Hello world"); // → 101

concat mdn

concat :: String -> String... -> String
concat("foo", "bar"); // → "foobar"


var fooSomething = concat("foo");
fooSomething("bar", "baz"); // → "foobarbaz"

indexOf mdn

indexOf :: String → String → Int
indexOf( "o", "hello world!" ); // → 4

indexOf_ mdn

indexOf_ :: String → Int → String → Int
indexOf_( "welcome", 14, "Hello world, welcome to the welcomed universe."); // → 28

lastIndexOf mdn

lastIndexOf :: String → String → Int
lastIndexOf( "o", "hello world!" ); // → 7

match mdn

match :: Regexp|String → String → [String]
var str = "The rain in SPAIN stays mainly in the plain"; 
match(/ain/g, str); // → ['ain','ain','ain']

replace mdn

replace :: Regexp|String → String → String → String
var str = "Visit Microsoft!";
replace("Microsoft","W3Schools", str); // → 'Visit W3Schools!'

search mdn

search :: Regexp|String → String → Int
var str="Visit W3Schools!";
search("W3Schools", str); / /6

slice mdn

slice :: Int → String → String
slice( 1, "hello world!" ); // → "ello world!"

slice_ mdn

slice_ :: Int → Int → String → String
slice_( 1, 10, "hello world!" ); // → "ello worl"

split mdn

split :: String → String → [String]
var str = "How are you doing today?";
split(" ", str)); // → ['How','are','you','doing','today?']

split_ mdn

split_ :: String → Int → String → [String]
var str = "How are you doing today?";
split_(" ", 2, str); // → ['How','are']

substring mdn

substring :: Int → String → String
var str = "Hello world!";
substring(3, str); // → 'lo world!'

substring_ mdn

substring_ :: Int → Int → String → String
var str = "Hello world!";
substring(3, 5, str); // → 'lo'

toLocaleLowerCase mdn

toLocaleLowerCase :: String → String
var str = "Hello World!";
toLocaleLowerCase(str); // → 'hello world!'

toLocaleUpperCase mdn

toLocaleUpperCase :: String → String
var str = "Hello World!";
toLocaleUpperCase(str); // → 'HELLO WORLD!'

toLowerCase mdn

toLowerCase :: String → String
var str = "Hello World!";
toLowerCase(str); // → 'hello world!'

toUpperCase mdn

toUpperCase :: String → String
var str = "Hello World!";
toUpperCase(str); // → 'HELLO WORLD!'

trim mdn

trim :: String → String
var str = " Hello World! ";
trim(str); // → 'Hello world!'


Arrays

concat mdn

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

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

every mdn

every :: (a → Boolean) → [a] → Boolean
every(lte(10), [12, 5, 8, 130, 44]); // → false
every(gte(4), [12, 5, 8, 130, 44]); // → true

filter mdn

filter :: (a → Boolean) → [a] → [a]
var isEven = function(n) { return !(n % 2); }
filter(isEven, [1,2,3,4]); // → [2,4];

forEach mdn

forEach :: (a -> _) -> [a] -> undefined
var x = 1;
var addToX = function(y){ x += y }
forEach(addToX, [2, 5]);
x; // → 8

indexOf mdn

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

indexOf_ mdn

indexOf_ :: a → Int → [a] → Int
indexOf_( 2, 2, [1, 2, 3, 2] ); // → 3

join mdn

join :: [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( 2, [1, 2, 3, 2, 4, 1, 0, 2, 4] ); // → 7

map mdn

map :: (a → b) → [a] → [b]
map(add(1), [1,2,3,4]); // → [2, 3, 4, 5]

pop mdn

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

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)

reduce mdn

reduce :: (b → a → b) → b → [a] → b
var words = ['foo', 'bar', 'baz'];
reduce(add, "", words) // → 'foobarbaz'

reduceRight mdn

reduceRight :: (b → a → b) → b → [a] → b
var words = ['foo', 'bar', 'baz'];
reduceRight(add, '', words); // → 'bazbarfoo'

reverse mdn

reverse :: a → a
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); // → [2, 3]
a; // → [1, 2, 3] (a is unaltered)

slice mdn

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

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

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]

sort mdn

sort :: [a] → [a]
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!"

propertyIsEnumerable mdn

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

exec mdn

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

hasOwnProperty mdn

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

isPrototypeOf mdn

isPrototypeOf ::
// 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

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

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