ku

A lightweight functional programming utility suite inspired by wu.js and Underscore.

ku.add(x, y)


Takes two values and returns their sum in the form of y + x.

ku.map(ku.add('.json'), ['package', 'component', 'bower']);
// => ['package.json', 'component.json', 'bower.json']

ku.and(x, y)


Takes two values then performs an AND comparison in the form of y && x.

Note that you can use this as a null-value catcher in a similar vein of Haskell’s Maybe monad, but more primitive since this is a static mapping.

var toBool = ku.and(true);
toBool(undefined); // => undefined
toBool(9000); // => true

ku.attr(attr, value)


Takes an attribute name and an object and returns that attribute of the object.

var getBanlist = ku(ku.map(ku.attr('username'),
                    ku.filter(ku.attr('banned'));

getBanlist([
  {username: 'L8D', banned: false, id: 123},
  {username: 'D8I', banned: true, id: 234},
  {username: 'tj', banned: true, id: 345}
]);
// => ['D8I', 'tj']

ku.cmod(x, y)


Same as ku.mod but return accurate modulo operation in the form of ((x % y) + y) % y to support operation on negative numbers

ku.compo(props, object)


Takes an object of properties and an object then returns a boolean of wether or not all properties of the properties object are equal to the same set of properties on the given object.

var firstTwoCorrect = ku.compo({foo: 1, bar: 2});

firstTwoCorrect({foo: 1, bar: 2, baz: 3, quux: 4}); // => true
firstTwoCorrect({foo: 0, bar: 2}) // => false
firstTwoCorrect({bax: 3, quux: 4}) // => false

ku.compose(func, func2)


Takes two functions and returns a new function of those two functions composed together.

ku.compose(ku.add(1), ku.sub(2))(1) // => 1 - 2 + 1 = 0

ku.curry(func, [expected=func.length])


Creates a wrapper function that performs automatic curry (keep returning collector functions until it has collected enough arguments to call the original function).

On a more technical level, Takes a function and returns a carrier function that will keep returning itself (the function) until it has recieved a number of arguments greater than or equal to the original function’s arity.

When it has “collected” enough arguments it will apply those arguments to the original function and then either return the result, or if the result is a function and there are extra aguments, it will apply the extra arguments to the result and return that.

ku.div(x, y)


Takes two numbers and return their quotient in the form of y / x.

ku.drop(amount, values)


Takes an amount, n, and an array of values then returns that array except for its last n values.

ku.eq(x, y)


Takes two values and performs a static JavaScript comparison in the form of y === x.

ku.filter(iterator, values)


Takes an iterator and an array of values, then returns a subset of the array containing elements where the iterator returned a truthy value.

var isOdd = ku(ku.eq(1), ku.mod(2));

ku.filter(isOdd, [1, 2, 3, 4, 5]) // => [1, 3, 5]

ku.find(iterator, values)


Takes an iterator and an array of values, then iterates over each element in that array until the iterator returns a truthy value, then returns that element.

ku.findI(iterator, values)


Same as ku.find except it returns the index of the element instead of the element itself.

ku.flip(func)


Takes a function and returns a new function with its first two arguments reversed.

ku.add('foo', ' bar'); // => ' barfoo'
ku.flip(ku.add)('foo', ' bar') // => 'foo bar'

ku.func(iterator)


Takes an iterator and returns the corresponding iterator callback. This is used by methods like find, map and filter to generate their iterator.

If a string is supplied, then it uses ku.attr. Known as the _.pluck notation in LoDash.

If an object is supplied, then it uses ku.combo. Known as the _.where notation in Lodash.

If a function is supplied, then it just uses that.

ku.head(values)


Takes an array of values and returns the first element of that array. Same as ku.take(1) or ku.attr(0).

ku.init(values)


Takes an array of values and returns that array except for the last element.

ku.ku(x, y)


Reversed version of ku.add for doing more efficient string concatenation.

var addWWW = ku.addf('www.');

addWWW('google.com'); // => 'www.google.com'

ku.map(iterator, values)


Takes an iterator and an array of values then iterates over each value and returns a array of the results of the iterator.

ku.map(ku.add(1), [1, 2, 3, 4, 5]); // => [2, 3, 4, 5, 6]

ku.max(numbers)


Takes an array of numbers and returns the highest value according to Math.max.

ku.method(attr, args...)


Takes an attribute and any number of arguments and returns a new function that will take an object and apply the given arguments to that object’s attribute (method).

ku.min(numbers)


Takes an array of numbers and returns the lowest value according to Math.min.

ku.mod(x, y)


Takes two numbers and returns the remainder in the form of x % y.

ku.mul(x, y)


Takes two numbers and returns their product in the form of y * x.

ku.negate(x)


Takes a number and returns it negated.

ku.not(value)


Takes any value and returns a boolean of the negated value.

ku.not(true) // => false

ku.or(x, y)


Takes two values and performs and OR comparison in the form of y || x.

Note that you can use this as a default-value catcher in a similar vein of Haskell’s Either monad.

// #fff is default color
var getColors = ku(ku.map(ku.or('#fff')), ku.pluck('color'));

getColors([{color: '#000'}, {}, {color: '#00f'}, {color: '#f00'}, {}]);
// => ['#000', '#fff', '#00f', '#f00', '#fff']

ku.pluck(attr, values)


Takes an attribute and array of values, then maps over each value and returns the value of that value’s attribute. This is the same as ku(ku.map, ku.attr).

var getUsernames = ku.pluck('username');
getUsernames([
  {username: 'L8D', password: 'somepassword', id: 123},
  {username: 'D8I', password: 'otherpassword', id: 234},
  {username: 'tj', password: 'whoknows', id: 345}
]);
// => ['L8D', 'D8I', 'tj']

ku.push(value, values)


Takes a single value and an array of values and returns a concatened array of values with the first value pushed to the end.

someEventStream.map(ku.push(Bacon.noMore));

ku.sub(x, y)


Takes two numbers and returns their difference in the form of y - x.

ku.tail(values)


Takes an array of values and returns that array except for the first element. Same as ku.method('slice', 1)

ku.take(amount, values)


Takes an amount, n, and an array of values then returns the first n elements of those values.

ku.take(5, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
// => [1, 2, 3, 4, 5]

ku.wrap(attr, value)


Takes an attribute name an a value, then returns an object with that attribute equal to the value.

ku.wrap('success', response); // => {success: response}

ku.zero(value)


Takes any value and returns a boolean of the double-negated value.

// filter out events that have no items in their array
someEventStream.filter(ku(ku.zero, ku.attr('length')))
  .map(template)
  .assign($('#some-element'), 'html');

ku.zip(values)


Takes an array of arrays and returns an array of grouped elements of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.

Note that this same function can be used to unzip zipped arrays also.

var a = ku.zip([['fred', 'barney'], [30, 40], [true, false]]);
// => [['fred', 30, true], ['barney', 40, false]]

ku.zip(a); // => [['fred', 'barney'], [30, 40], [true, false]]

ku.zipo(keys, values)


Takes an array of keys and an array of values then returns an object with properties equal to each key and value pair.

ku.zipo(['foo', 'bar', 'baz', 'quux'], [1, 2, 3, 4]);
// => {foo: 1, bar: 2, baz: 3, quux: 4}