Rambda
Faster alternative to Ramda in just 7kB
Argumentation
I admire Ramda as it is great library in what it does, but I used only small part of what it offers.
I wanted to optimize the size of my bundle, but already developed Ramda habits.
This lead me to the idea to recreate the funtionality of some Ramda methods and export that as library.
Example use
const R = require("rambda")
const result = R.compose(
R.filter(val => val>2),
R.flatten,
)([ [1], [2], [3], 4])
console.log(result) // => [3,4]
##Install
https://unpkg.com/rambda@0.3.4/webVersion.js
in your HTML
Differences between Rambda and Ramda
Rambda shadows only small part of the Ramda's API.
A few things to note:
-
Rambda's methods should be compatible with most of the basic Ramda's methods. For more complex and Ramda specific methods(such as R.__), you should expect a mismatch.
-
Rambda is tested for compatability with Ramda.flip, as this method could be useful in some cases.
-
Rambda's equals doesn't protect against circular structures as Ramda.equals does
Benchmark
Scroll to API
Disclaimer
- Documentation of the methods below is scraped from Ramda's website and could be removed in the future, if requested from Ramda's side.
API
api-list
add
R.add(2, 3);
R.add(7)(10);
adjust
- Applies a function to the value at the given index of an array, returning a
new copy of the array with the element at the given index replaced with the
result of the function application.
R.adjust(R.add(10), 1, [0, 1, 2]);
R.adjust(R.add(10))(1)([0, 1, 2]);
any
- Returns true if at least one of elements of the list match the predicate,
false otherwise.
Dispatches to the any method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
var lessThan0 = R.flip(R.lt)(0);
var lessThan2 = R.flip(R.lt)(2);
R.any(lessThan0)([1, 2]);
R.any(lessThan2)([1, 2]);
append
- Returns a new list containing the contents of the given list, followed by
the given element.
R.append('tests', ['write', 'more']);
R.append('tests', []);
R.append(['tests'], ['write', 'more']);
contains
- Returns true if the specified value is equal, in R.equals terms, to at
least one element of the given list; false otherwise.
R.contains(3, [1, 2, 3]);
R.contains(4, [1, 2, 3]);
R.contains([42], [[42]]);
drop
- Returns all but the first n elements of the given list, string, or
transducer/transformer (or object with a drop method).
Dispatches to the drop method of the second argument, if present.
R.drop(1, ['foo', 'bar', 'baz']);
R.drop(2, ['foo', 'bar', 'baz']);
R.drop(3, ['foo', 'bar', 'baz']);
R.drop(4, ['foo', 'bar', 'baz']);
R.drop(3, 'ramda');
dropLast
- Returns a list containing all but the last n elements of the given list.
R.dropLast(1, ['foo', 'bar', 'baz']);
R.dropLast(2, ['foo', 'bar', 'baz']);
R.dropLast(3, ['foo', 'bar', 'baz']);
R.dropLast(4, ['foo', 'bar', 'baz']);
R.dropLast(3, 'ramda');
equals
- Returns true if its arguments are equivalent, false otherwise. Handles
cyclical data structures.
Dispatches symmetrically to the equals methods of both arguments, if
present.
R.equals(1, 1);
R.equals(1, '1');
R.equals([1, 2, 3], [1, 2, 3]);
var a = {}; a.v = a;
var b = {}; b.v = b;
R.equals(a, b);
filter
- Takes a predicate and a "filterable", and returns a new filterable of the
same type containing the members of the given filterable which satisfy the
given predicate.
Dispatches to the filter method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
var isEven = n => n % 2 === 0;
R.filter(isEven, [1, 2, 3, 4]);
R.filter(isEven, {a: 1, b: 2, c: 3, d: 4});
find
- Returns the first element of the list which matches the predicate, or
undefined if no element matches.
Dispatches to the find method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
var xs = [{a: 1}, {a: 2}, {a: 3}];
R.find(R.propEq('a', 2))(xs);
R.find(R.propEq('a', 4))(xs);
findIndex
- Returns the index of the first element of the list which matches the
predicate, or -1 if no element matches.
Dispatches to the findIndex method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
var xs = [{a: 1}, {a: 2}, {a: 3}];
R.findIndex(R.propEq('a', 2))(xs);
R.findIndex(R.propEq('a', 4))(xs);
flatten
- Returns a new list by pulling every item out of it (and all its sub-arrays)
and putting them in a new array, depth-first.
R.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
head
- Returns the first element of the given list or string. In some libraries
this function is named first.
R.head(['fi', 'fo', 'fum']);
R.head([]);
R.head('abc');
R.head('');
indexOf
- Returns the position of the first occurrence of an item in an array, or -1
if the item is not included in the array. R.equals is used to determine
equality.
R.indexOf(3, [1,2,3,4]);
R.indexOf(10, [1,2,3,4]);
init
- Returns all but the last element of the given list or string.
R.init([1, 2, 3]);
R.init([1, 2]);
R.init([1]);
R.init([]);
R.init('abc');
R.init('ab');
R.init('a');
R.init('');
join
- Returns a string made by inserting the separator between each element and
concatenating all the elements into a single string.
var spacer = R.join(' ');
spacer(['a', 2, 3.4]);
R.join('|', [1, 2, 3]);
last
- Returns the last element of the given list or string.
R.last(['fi', 'fo', 'fum']);
R.last([]);
R.last('abc');
R.last('');
length
- Returns the number of elements in the array by returning list.length.
R.length([]);
R.length([1, 2, 3]);
map
- Takes a function and
a functor,
applies the function to each of the functor's values, and returns
a functor of the same shape.
Ramda provides suitable map implementations for Array and Object,
so this function may be applied to [1, 2, 3] or {x: 1, y: 2, z: 3}.
Dispatches to the map method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
Also treats functions as functors and will compose them together.
var double = x => x * 2;
R.map(double, [1, 2, 3]);
R.map(double, {x: 1, y: 2, z: 3});
match
- Tests a regular expression against a String. Note that this function will
return an empty array when there are no matches. This differs from
String.prototype.match
which returns null when there are no matches.
R.match(/([a-z]a)/g, 'bananas');
R.match(/a/, 'b');
R.match(/a/, null);
merge
- Create a new object with the own properties of the first object merged with
the own properties of the second object. If a key exists in both objects,
the value from the second object will be used.
R.merge({ 'name': 'fred', 'age': 10 }, { 'age': 40 });
var resetToDefault = R.merge(R.__, {x: 0});
resetToDefault({x: 5, y: 2});
omit
- Returns a partial copy of an object omitting the keys specified.
R.omit(['a', 'd'], {a: 1, b: 2, c: 3, d: 4});
path
- Retrieve the value at a given path.
R.path(['a', 'b'], {a: {b: 2}});
R.path(['a', 'b'], {c: {b: 2}});
pick
- Returns a partial copy of an object containing only the keys specified. If
the key does not exist, the property is ignored.
R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4});
R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4});
prepend
- Returns a new list with the given element at the front, followed by the
contents of the list.
R.prepend('fee', ['fi', 'fo', 'fum']);
prop
- Returns a function that when supplied an object returns the indicated
property of that object, if it exists.
R.prop('x', {x: 100});
R.prop('x', {});
propEq
- Returns true if the specified object property is equal, in R.equals
terms, to the given value; false otherwise.
var abby = {name: 'Abby', age: 7, hair: 'blond'};
var fred = {name: 'Fred', age: 12, hair: 'brown'};
var rusty = {name: 'Rusty', age: 10, hair: 'brown'};
var alois = {name: 'Alois', age: 15, disposition: 'surly'};
var kids = [abby, fred, rusty, alois];
var hasBrownHair = R.propEq('hair', 'brown');
R.filter(hasBrownHair, kids);
range
- Returns a list of numbers from from (inclusive) to to (exclusive).
R.range(1, 5);
R.range(50, 53);
repeat
- Returns a fixed list of size n containing a specified identical value.
R.repeat('hi', 5);
var obj = {};
var repeatedObjs = R.repeat(obj, 5);
repeatedObjs[0] === repeatedObjs[1];
replace
- Replace a substring or regex match in a string with a replacement.
R.replace('foo', 'bar', 'foo foo foo');
R.replace(/foo/, 'bar', 'foo foo foo');
R.replace(/foo/g, 'bar', 'foo foo foo');
sort
- Returns a copy of the list, sorted according to the comparator function,
which should accept two values at a time and return a negative number if the
first value is smaller, a positive number if it's larger, and zero if they
are equal. Please note that this is a copy of the list. It does not
modify the original.
var diff = function(a, b) { return a - b; };
R.sort(diff, [4,2,7,5]);
sortBy
- Sorts the list according to the supplied function.
var sortByFirstItem = R.sortBy(R.prop(0));
var sortByNameCaseInsensitive = R.sortBy(R.compose(R.toLower, R.prop('name')));
var pairs = [[-1, 1], [-2, 2], [-3, 3]];
sortByFirstItem(pairs);
var alice = {
name: 'ALICE',
age: 101
};
var bob = {
name: 'Bob',
age: -10
};
var clara = {
name: 'clara',
age: 314.159
};
var people = [clara, bob, alice];
sortByNameCaseInsensitive(people);
split
- Splits a string into an array of strings based on the given
separator.
var pathComponents = R.split('/');
R.tail(pathComponents('/usr/local/bin/node'));
R.split('.', 'a.b.c.xyz.d');
splitEvery
- Splits a collection into slices of the specified length.
R.splitEvery(3, [1, 2, 3, 4, 5, 6, 7]);
R.splitEvery(3, 'foobarbaz');
subtract
- Subtracts its second argument from its first argument.
R.subtract(10, 8);
var minus5 = R.subtract(R.__, 5);
minus5(17);
var complementaryAngle = R.subtract(90);
complementaryAngle(30);
complementaryAngle(72);
tail
- Returns all but the first element of the given list or string (or object
with a tail method).
Dispatches to the slice method of the first argument, if present.
R.tail([1, 2, 3]);
R.tail([1, 2]);
R.tail([1]);
R.tail([]);
R.tail('abc');
R.tail('ab');
R.tail('a');
R.tail('');
take
- Returns the first n elements of the given list, string, or
transducer/transformer (or object with a take method).
Dispatches to the take method of the second argument, if present.
R.take(1, ['foo', 'bar', 'baz']);
R.take(2, ['foo', 'bar', 'baz']);
R.take(3, ['foo', 'bar', 'baz']);
R.take(4, ['foo', 'bar', 'baz']);
R.take(3, 'ramda');
var personnel = [
'Dave Brubeck',
'Paul Desmond',
'Eugene Wright',
'Joe Morello',
'Gerry Mulligan',
'Bob Bates',
'Joe Dodge',
'Ron Crotty'
];
var takeFive = R.take(5);
takeFive(personnel);
takeLast
- Returns a new list containing the last n elements of the given list.
If n > list.length, returns a list of list.length elements.
R.takeLast(1, ['foo', 'bar', 'baz']);
R.takeLast(2, ['foo', 'bar', 'baz']);
R.takeLast(3, ['foo', 'bar', 'baz']);
R.takeLast(4, ['foo', 'bar', 'baz']);
R.takeLast(3, 'ramda');
test
- Determines whether a given string matches a given regular expression.
R.test(/^x/, 'xyz');
R.test(/^y/, 'xyz');
toLower
- The lower case version of a string.
R.toLower('XYZ');
toUpper
- The upper case version of a string.
R.toUpper('abc');
trim
- Removes (strips) whitespace from both ends of the string.
R.trim(' xyz ');
R.map(R.trim, R.split(',', 'x, y, z'));
type
- Gives a single-word string description of the (native) type of a value,
returning such answers as 'Object', 'Number', 'Array', or 'Null'. Does not
attempt to distinguish user Object types any further, reporting them all as
'Object'.
R.type({});
R.type(1);
R.type(false);
R.type('s');
R.type(null);
R.type([]);
R.type(/[A-z]/);
uniq
- Returns a new list containing only one copy of each element in the original
list. R.equals is used to determine equality.
R.uniq([1, 1, 2, 1]);
R.uniq([1, '1']);
R.uniq([[42], [42]]);
update
- Returns a new copy of the array with the element at the provided index
replaced with the given value.
R.update(1, 11, [0, 1, 2]);
R.update(1)(11)([0, 1, 2]);
values
- Returns a list of all the enumerable own properties of the supplied object.
Note that the order of the output array is not guaranteed across different
JS platforms.
R.values({a: 1, b: 2, c: 3});