### Check if List or String Starts With Prefix Source: https://ramdajs.com/docs Determines if a list begins with a specified sublist or if a string starts with a specified substring. Case-sensitive for strings. ```javascript R.startsWith('a', 'abc') //=> true ``` ```javascript R.startsWith('b', 'abc') //=> false ``` ```javascript R.startsWith(['a'], ['a', 'b', 'c']) //=> true ``` ```javascript R.startsWith(['b'], ['a', 'b', 'c']) //=> false ``` -------------------------------- ### Generate a list of numbers in a range Source: https://ramdajs.com/docs Use `range` to create a list of numbers starting from `from` (inclusive) up to `to` (exclusive). Handles both integer and floating-point boundaries. ```javascript R.range(1, 5); //=> [1, 2, 3, 4] R.range(1, 5.5); //=> [1, 2, 3, 4, 5] R.range(1.5, 5.5); //=> [1.5, 2.5, 3.5, 4.5] ``` -------------------------------- ### Take - Get First N Elements Source: https://ramdajs.com/docs Returns the first 'n' elements of a list, string, or transducer. If 'n' is greater than the list length, the entire list is returned. Can be partially applied. ```javascript R.take(1, ['foo', 'bar', 'baz']); //=> ['foo'] R.take(2, ['foo', 'bar', 'baz']); //=> ['foo', 'bar'] R.take(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz'] R.take(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz'] R.take(3, 'ramda'); //=> 'ram' const personnel = [ 'Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan', 'Bob Bates', 'Joe Dodge', 'Ron Crotty' ]; const takeFive = R.take(5); takeFive(personnel); //=> ['Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan'] ``` -------------------------------- ### Get All But Last Element of List Source: https://ramdajs.com/docs Use `init` to return a new list containing all elements except the last one. This function works for both lists and strings. ```javascript R.init([1, 2, 3]); //=> [1, 2] R.init([1, 2]); //=> [1] R.init([1]); //=> [] R.init([]); //=> [] R.init('abc'); //=> 'ab' R.init('ab'); //=> 'a' R.init('a'); //=> '' R.init(''); //=> '' ``` -------------------------------- ### Get First Element of List or String Source: https://ramdajs.com/docs Use `head` to retrieve the first element from a list or the first character from a string. Returns `undefined` for empty lists or strings. ```javascript R.head([1, 2, 3]); //=> 1 R.head([1]); //=> 1 R.head([]); //=> undefined R.head('abc'); //=> 'a' R.head('a'); //=> 'a' R.head(''); //=> undefined ``` -------------------------------- ### prepend Source: https://ramdajs.com/docs Returns a new list with the given element at the front, followed by the contents of the list. Added in v0.1.0. ```APIDOC ## prepend List `a → [a] → [a]` Parameters * el The item to add to the head of the output list. * list The array to add to the tail of the output list. Returns Array A new array. Added in v0.1.0 Returns a new list with the given element at the front, followed by the contents of the list. See also append. ```javascript R.prepend('fee', ['fi', 'fo', 'fum']); //=> ['fee', 'fi', 'fo', 'fum'] ``` ``` -------------------------------- ### remove List Source: https://ramdajs.com/docs Removes the sub-list of `list` starting at index `start` and containing `count` elements. Returns a copy of the list with the changes. ```APIDOC ## remove List `Number → Number → [a] → [a]` ### Parameters * start The position to start removing elements * count The number of elements to remove * list The list to remove from ### Returns Array A new Array with `count` elements from `start` removed. ### Description Removes the sub-list of `list` starting at index `start` and containing `count` elements. _Note that this is not destructive_ : it returns a copy of the list with the changes. No lists have been harmed in the application of this function. ### See Also without ### Example ```javascript R.remove(2, 3, [1,2,3,4,5,6,7,8]); //=> [1,2,6,7,8] ``` ``` -------------------------------- ### init Source: https://ramdajs.com/docs Returns all elements of a list or string except for the last one. ```APIDOC ## init List `[a] → [a]` `String → String` ### Parameters * **list** ### Returns * ### Description Returns all but the last element of the given list or string. See also last, head, tail. ``` -------------------------------- ### Slice List or String Source: https://ramdajs.com/docs Returns elements from a list or string within specified start and end indices. The start index is inclusive, and the end index is exclusive. Handles positive, negative, and Infinity indices. ```javascript R.slice(1, 3, ['a', 'b', 'c', 'd']); //=> ['b', 'c'] R.slice(1, Infinity, ['a', 'b', 'c', 'd']); //=> ['b', 'c', 'd'] R.slice(0, -1, ['a', 'b', 'c', 'd']); //=> ['a', 'b', 'c'] R.slice(-3, -1, ['a', 'b', 'c', 'd']); //=> ['b', 'c'] ``` ```javascript R.slice(0, 3, 'ramda'); //=> 'ram' ``` -------------------------------- ### take List Source: https://ramdajs.com/docs Returns the first n elements of a list or string. Added in v0.1.0. ```APIDOC ## take List `Number → [a] → [a]` `Number → String → String` ### Parameters * n * list ### Returns * ### Description 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. See also drop. ### Example ```javascript R.take(1, ['foo', 'bar', 'baz']); //=> ['foo'] R.take(2, ['foo', 'bar', 'baz']); //=> ['foo', 'bar'] R.take(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz'] R.take(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz'] R.take(3, 'ramda'); //=> 'ram' const personnel = [ 'Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan', 'Bob Bates', 'Joe Dodge', 'Ron Crotty' ]; const takeFive = R.take(5); takeFive(personnel); //=> ['Dave Brubeck', 'Paul Desmond', 'Eugene Wright', 'Joe Morello', 'Gerry Mulligan'] ``` ``` -------------------------------- ### keysIn Object Source: https://ramdajs.com/docs Returns a list containing the names of all the properties of the supplied object, including prototype properties. ```APIDOC ## keysIn Object `{k: v} → [k]` Parameters * obj The object to extract properties from Returns Array An array of the object's own and prototype properties. Added in v0.2.0 Returns a list containing the names of all the properties of the supplied object, including prototype properties. Note that the order of the output array is not guaranteed to be consistent across different JS platforms. See also keys, valuesIn. ```javascript const F = function() { this.x = 'X'; }; F.prototype.y = 'Y'; const f = new F(); R.keysIn(f); //=> ['x', 'y'] ``` ``` -------------------------------- ### range Source: https://ramdajs.com/docs Generates a list of numbers within a specified range (inclusive start, exclusive end). ```APIDOC ## range List `Number → Number → [Number]` ### Parameters * from The first number in the list. * to One more than the last number in the list. ### Returns Array The list of numbers in the set `[a, b)`. ### Added in v0.1.0 ### Description Returns a list of numbers from `from` (inclusive) to `to` (exclusive). ### Examples ```javascript R.range(1, 5); //=> [1, 2, 3, 4] R.range(1, 5.5); //=> [1, 2, 3, 4, 5] R.range(1.5, 5.5); //=> [1.5, 2.5, 3.5, 4.5] ``` ``` -------------------------------- ### Get Array Length with R.length Source: https://ramdajs.com/docs Returns the number of elements in an array. This is equivalent to the array's .length property. ```javascript R.length([]); //=> 0 R.length([1, 2, 3]); //=> 3 ``` -------------------------------- ### startsWith List Source: https://ramdajs.com/docs Checks if a list or string begins with a specified prefix. ```APIDOC ## startsWith List `[a] → [a] → Boolean` `String → String → Boolean` ### Parameters * **prefix** (Array | String) - The sublist or substring to check for at the beginning. * **list** (Array | String) - The list or string to check. ### Returns Boolean - True if the list/string starts with the prefix, false otherwise. ### Description Checks if a list starts with the provided sublist. Similarly, checks if a string starts with the provided substring. ### Examples ```javascript R.startsWith('a', 'abc') //=> true R.startsWith('b', 'abc') //=> false R.startsWith(['a'], ['a', 'b', 'c']) //=> true R.startsWith(['b'], ['a', 'b', 'c']) //=> false ``` ``` -------------------------------- ### memoizeWith Source: https://ramdajs.com/docs Takes a string-returning function `keyGen` and a function `fn` and returns a new function that returns cached results for subsequent calls with the same arguments. ```APIDOC ## memoizeWith Function `(*… → String) → (*… → a) → (*… → a)` ### Description Takes a string-returning function `keyGen` and a function `fn` and returns a new function that returns cached results for subsequent calls with the same arguments. When the function is invoked, `keyGen` is applied to the same arguments and its result becomes the cache key. If the cache contains something under that key, the function simply returns it and does not invoke `fn` at all. Otherwise `fn` is applied to the same arguments and its return value is cached under that key and returned by the function. ### Parameters * `keyGen` - The function to generate the cache key. * `fn` - The function to memoize. ### Returns function - Memoized version of `fn`. ### Examples ```javascript const withAge = memoizeWith(o => `${o.birth}/${o.death}`, ({birth, death}) => { console.log(`computing age for ${birth}/${death}`); return ({birth, death, age: death - birth}); }); withAge({birth: 1921, death: 1999}); //=> LOG: computing age for 1921/1999 //=> {birth: 1921, death: 1999, age: 78} withAge({birth: 1921, death: 1999}); //=> {birth: 1921, death: 1999, age: 78} ``` ``` -------------------------------- ### Get nth Argument from Function Source: https://ramdajs.com/docs Returns a function that, when called, returns its nth argument. Supports negative indexing from the end. ```javascript R.nthArg(1)('a', 'b', 'c'); //=> 'b' R.nthArg(-1)('a', 'b', 'c'); //=> 'c' ``` -------------------------------- ### Tail - Get All But First Element Source: https://ramdajs.com/docs Returns all elements of a list or string except for the first one. For objects with a 'tail' method, it dispatches to that method. ```javascript R.tail([1, 2, 3]); //=> [2, 3] R.tail([1, 2]); //=> [2] R.tail([1]); //=> [] R.tail([]); //=> [] R.tail('abc'); //=> 'bc' R.tail('ab'); //=> 'b' R.tail('a'); //=> '' R.tail(''); //=> '' ``` -------------------------------- ### pickAll Source: https://ramdajs.com/docs Similar to `pick` except that this one includes a `key: undefined` pair for properties that don't exist. Added in v0.1.0. ```APIDOC ## pickAll Object `[k] → {k: v} → {k: v}` Parameters * names an array of String property names to copy onto a new object * obj The object to copy from Returns Object A new object with only properties from `names` on it. Added in v0.1.0 Similar to `pick` except that this one includes a `key: undefined` pair for properties that don't exist. See also pick. ```javascript R.pickAll(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4} R.pickAll(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, e: undefined, f: undefined} ``` ``` -------------------------------- ### Get the last element of a list or string Source: https://ramdajs.com/docs Returns the last element of the given list or string. Returns undefined if the list or string is empty. ```javascript R.last([1, 2, 3]); //=> 3 R.last([1]); //=> 1 R.last([]); //=> undefined R.last('abc'); //=> 'c' R.last('a'); //=> 'a' R.last(''); //=> undefined ``` -------------------------------- ### Prepend an element to a list Source: https://ramdajs.com/docs Creates a new list with the specified element added to the beginning of the original list. The original list remains unchanged. ```javascript R.prepend('fee', ['fi', 'fo', 'fum']); //=> ['fee', 'fi', 'fo', 'fum'] ``` -------------------------------- ### head List Source: https://ramdajs.com/docs Returns the first element of a given list or string. ```APIDOC ## head List `[a] → a | Undefined` `String → String | Undefined` ### Parameters * **list** ### Returns * - The first element of the list or string. ### Description Returns the first element of the given list or string. In some libraries this function is named `first`. ### Example ```javascript R.head([1, 2, 3]); //=> 1 R.head([1]); //=> 1 R.head([]); //=> undefined R.head('abc'); //=> 'a' R.head('a'); //=> 'a' R.head(''); //=> undefined ``` ``` -------------------------------- ### Remove a sub-list by index and count Source: https://ramdajs.com/docs Use `R.remove` to create a new array with a specified number of elements removed starting from a given index. This operation is non-destructive. ```javascript R.remove(2, 3, [1,2,3,4,5,6,7,8]); //=> [1,2,6,7,8] ``` -------------------------------- ### Get Object Values Source: https://ramdajs.com/docs Returns an array of the values of the object's own enumerable properties. The order of the output array is not guaranteed across different JS platforms. ```javascript R.values({a: 1, b: 2, c: 3}); //=> [1, 2, 3] ``` -------------------------------- ### forEachObjIndexed Source: https://ramdajs.com/docs Iterate over an input object, calling a provided function for each key and value in the object. The function receives three arguments: value, key, and the object itself. It returns the original object. ```APIDOC ## forEachObjIndexed Object `((a, String, StrMap a) → Any) → StrMap a → StrMap a` ### Parameters * `fn` - The function to invoke. Receives three argument, `value`, `key`, `obj`. * `obj` - The object to iterate over. ### Returns Object - The original object. ### Description Iterate over an input `object`, calling a provided function `fn` for each key and value in the object. `fn` receives three argument: _(value, key, obj)_. ### Example ```javascript const printKeyConcatValue = (value, key) => console.log(key + ':' + value); R.forEachObjIndexed(printKeyConcatValue, {x: 1, y: 2}); //=> {x: 1, y: 2} // logs x:1 // logs y:2 ``` ``` -------------------------------- ### TakeLast - Get Last N Elements Source: https://ramdajs.com/docs Returns the last 'n' elements of a list or string. If 'n' is greater than the list length, the entire list is returned. ```javascript R.takeLast(1, ['foo', 'bar', 'baz']); //=> ['baz'] R.takeLast(2, ['foo', 'bar', 'baz']); //=> ['bar', 'baz'] R.takeLast(3, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz'] R.takeLast(4, ['foo', 'bar', 'baz']); //=> ['foo', 'bar', 'baz'] R.takeLast(3, 'ramda'); //=> 'mda' ``` -------------------------------- ### Map Functions with Pre- and Post-processors Source: https://ramdajs.com/docs Applies two functions as pre- and post- processors to a third function, effectively composing transformations. Useful for creating complex data transformations. ```javascript const decodeChar = R.promap(s => s.charCodeAt(), String.fromCharCode, R.add(-8)) const decodeString = R.promap(R.split(''), R.join(''), R.map(decodeChar)) decodeString("ziuli") //=> "ramda" ``` -------------------------------- ### until Logic Source: https://ramdajs.com/docs Takes a predicate, a transformation function, and an initial value, and returns a value of the same type as the initial value. It does so by applying the transformation until the predicate is satisfied, at which point it returns the satisfactory value. ```APIDOC ## until Logic `(a → Boolean) → (a → a) → a → a` ### Parameters * pred A predicate function * fn The iterator function * init Initial value ### Returns * Final value that satisfies predicate ### Description Takes a predicate, a transformation function, and an initial value, and returns a value of the same type as the initial value. It does so by applying the transformation until the predicate is satisfied, at which point it returns the satisfactory value. ### Example ```javascript R.until(R.gt(R.__, 100), R.multiply(2))(1) // => 128 ``` ``` -------------------------------- ### Get nth Element from List or String Source: https://ramdajs.com/docs Retrieves the nth element from a list or string. Negative offsets count from the end of the list. Returns undefined if the index is out of bounds. ```javascript const list = ['foo', 'bar', 'baz', 'quux']; R.nth(1, list); //=> 'bar' R.nth(-1, list); //=> 'quux' R.nth(-99, list); //=> undefined R.nth(2, 'abc'); //=> 'c' R.nth(3, 'abc'); //=> undefined ``` -------------------------------- ### of Function Source: https://ramdajs.com/docs Given a constructor and a value, returns a new instance of that constructor containing the value. ```APIDOC ## of Function `(* → {*}) → a → {a}` ### Parameters * `Ctor` (A constructor) * `val` (any value) ### Returns * An instance of the `Ctor` wrapping `val`. ### Added in v0.3.0 ### Description Given a constructor and a value, returns a new instance of that constructor containing the value. ### Note This `of` is different from the ES6 `of`; See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of ### Example ```javascript R.of(Array, 42); //=> [42] R.of(Array, [42]); //=> [[42]] R.of(Maybe, 42); //=> Maybe.Just(42) ``` ``` -------------------------------- ### min Source: https://ramdajs.com/docs Returns the smaller of its two arguments. ```APIDOC ## min Relation `Ord a => a → a → a` ### Parameters * a - The first value. * b - The second value. ### Returns * Returns the smaller of its two arguments. ### Example ```javascript R.min(789, 123); //=> 123 R.min('a', 'b'); //=> 'a' ``` ``` -------------------------------- ### Use R.__ Placeholder for Partial Application Source: https://ramdajs.com/docs The R.__ placeholder allows for flexible partial application of curried functions. It can be used to specify gaps in arguments, enabling arguments to be supplied in any order. ```javascript const greet = R.replace('{name}', R.__, 'Hello, {name}!'); greet('Alice'); //=> 'Hello, Alice!' ``` -------------------------------- ### Get Object Values Including Prototype Source: https://ramdajs.com/docs Returns an array of all properties, including prototype properties, of the supplied object. The order is not guaranteed to be consistent across different JS platforms. ```javascript const F = function() { this.x = 'X'; }; F.prototype.y = 'Y'; const f = new F(); R.valuesIn(f); //=> ['X', 'Y'] ``` -------------------------------- ### Instantiate Constructor with Value Source: https://ramdajs.com/docs Creates a new instance of a given constructor, wrapping a provided value. Falls back to wrapping in an array if no 'of' method is found. ```javascript R.of(Array, 42); //=> [42] R.of(Array, [42]); //=> [[42]] R.of(Maybe, 42); //=> Maybe.Just(42) ``` -------------------------------- ### TakeWhile - Elements from Start Until Predicate Fails Source: https://ramdajs.com/docs Returns elements from the beginning of a list or string until a predicate function returns false. The element causing the predicate to fail is excluded. Can act as a transducer. ```javascript const isNotFour = x => x !== 4; R.takeWhile(isNotFour, [1, 2, 3, 4, 3, 2, 1]); //=> [1, 2, 3] R.takeWhile(x => x !== 'd' , 'Ramda'); //=> 'Ram' ``` -------------------------------- ### lt Source: https://ramdajs.com/docs Returns `true` if the first argument is less than the second; `false` otherwise. ```APIDOC ## lt Relation `Ord a => a → a → Boolean` ### Parameters * a * b ### Returns Boolean Added in v0.1.0 Returns `true` if the first argument is less than the second; `false` otherwise. See also gt. ```js // Example Usage: R.lt(2, 1); //=> false R.lt(2, 2); //=> false R.lt(2, 3); //=> true R.lt('a', 'z'); //=> true R.lt('z', 'a'); //=> false ``` ``` -------------------------------- ### bind Source: https://ramdajs.com/docs Creates a function that is bound to a context. Note: R.bind does not provide the additional argument-binding capabilities of Function.prototype.bind. ```APIDOC ## bind Function `(* → *) → {*} → (* → *)` Parameters * fn The function to bind to context * thisObj The context to bind `fn` to Returns function A function that will execute in the context of `thisObj`. Added in v0.6.0 Creates a function that is bound to a context. Note: `R.bind` does not provide the additional argument-binding capabilities of Function.prototype.bind. See also partial. ``` Open in REPLRun it here const log = R.bind(console.log, console); R.pipe(R.assoc('a', 2), R.tap(log), R.assoc('a', 3))({a: 1}); //=> {a: 3} // logs {a: 2} ``` ``` -------------------------------- ### Find the index of the first element matching a predicate Source: https://ramdajs.com/docs Use `findIndex` to get the index of the first element in a list that satisfies a given predicate function. Returns `-1` if no element matches. ```javascript const xs = [{a: 1}, {a: 2}, {a: 3}]; R.findIndex(R.propEq(2, 'a'))(xs); //=> 1 R.findIndex(R.propEq(4, 'a'))(xs); //=> -1 ``` -------------------------------- ### on Function Source: https://ramdajs.com/docs Takes a binary function f, a unary function g, and two values. Applies g to each value, then applies the result of each to f. Added in v0.28.0. ```APIDOC ## on Function `((a, a) → b) → (c → a) → c → c → b` ### Parameters * f a binary function * g a unary function * a any value * b any value ### Returns any The result of `f` ### Also known as P combinator. ### Example ```javascript const eqBy = R.on((a, b) => a === b); eqBy(R.prop('a'), {b:0, a:1}, {a:1}); //=> true; const containsInsensitive = R.on(R.includes, R.toLower); containsInsensitive('o', 'FOO'); //=> true ``` ``` -------------------------------- ### Get Object Property or Default Value Source: https://ramdajs.com/docs Retrieves a property's value from an object, returning a default value if the property is missing, null, undefined, or NaN. Useful for safe property access. ```javascript const alice = { name: 'ALICE', age: 101 }; const favorite = R.prop('favoriteLibrary'); const favoriteWithDefault = R.propOr('Ramda', 'favoriteLibrary'); favorite(alice); //=> undefined favoriteWithDefault(alice); //=> 'Ramda' ``` -------------------------------- ### Split List Based on Predicate Source: https://ramdajs.com/docs Splits a list into two parts based on a predicate function. The first list contains elements that do not satisfy the predicate, and the second list starts with the first element that does satisfy it. ```javascript R.splitWhen(R.equals(2), [1, 2, 3, 1, 2, 3]); //=> [[1], [2, 3, 1, 2, 3]] ``` -------------------------------- ### pick Source: https://ramdajs.com/docs Returns a partial copy of an object containing only the keys specified. If the key does not exist, the property is ignored. Added in v0.1.0. ```APIDOC ## pick Object `[k] → {k: v} → {k: v}` Parameters * names an array of String property names to copy onto a new object * obj The object to copy from Returns Object A new object with only properties from `names` on it. Added in v0.1.0 Returns a partial copy of an object containing only the keys specified. If the key does not exist, the property is ignored. See also omit, props. ```javascript R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4} R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1} ``` ``` -------------------------------- ### binary Source: https://ramdajs.com/docs Wraps a function of any arity (including nullary) in a function that accepts exactly 2 parameters. Any extraneous parameters will not be passed to the supplied function. ```APIDOC ## binary Function `(a → b → c → … → z) → ((a, b) → z)` Parameters * fn The function to wrap. Returns function A new function wrapping `fn`. The new function is guaranteed to be of arity 2. Added in v0.2.0 Wraps a function of any arity (including nullary) in a function that accepts exactly 2 parameters. Any extraneous parameters will not be passed to the supplied function. See also nAry, unary. ``` Open in REPLRun it here const takesThreeArgs = function(a, b, c) { return [a, b, c]; }; takesThreeArgs.length; //=> 3 takesThreeArgs(1, 2, 3); //=> [1, 2, 3] const takesTwoArgs = R.binary(takesThreeArgs); takesTwoArgs.length; //=> 2 // Only 2 arguments are passed to the wrapped function takesTwoArgs(1, 2, 3); //=> [1, 2, undefined] ``` ``` -------------------------------- ### lens Source: https://ramdajs.com/docs Returns a lens for the given getter and setter functions. The getter "gets" the value of the focus; the setter "sets" the value of the focus. The setter should not mutate the data structure. ```APIDOC ## lens Object `(s → a) → ((a, s) → s) → Lens s a` `Lens s a = Functor f => (a → f a) → s → f s` ### Parameters * getter * setter ### Returns Lens Added in v0.8.0 Returns a lens for the given getter and setter functions. The getter "gets" the value of the focus; the setter "sets" the value of the focus. The setter should not mutate the data structure. See also view, set, over, lensIndex, lensProp. ```js // Example Usage: const xLens = R.lens(R.prop('x'), R.assoc('x')); R.view(xLens, {x: 1, y: 2}); //=> 1 R.set(xLens, 4, {x: 1, y: 2}); //=> {x: 4, y: 2} R.over(xLens, R.negate, {x: 1, y: 2}); //=> {x: -1, y: 2} ``` ``` -------------------------------- ### Get own enumerable property keys of an object Source: https://ramdajs.com/docs Returns an array containing the names of all the enumerable own properties of the supplied object. The order of properties in the output array is not guaranteed to be consistent across different JavaScript platforms. ```javascript R.keys({a: 1, b: 2, c: 3}); //=> ['a', 'b', 'c'] ``` -------------------------------- ### Project Specific Properties from Objects Source: https://ramdajs.com/docs Creates an array of objects containing only the specified properties from a list of objects. Useful for selecting subsets of data. ```javascript const abby = {name: 'Abby', age: 7, hair: 'blond', grade: 2}; const fred = {name: 'Fred', age: 12, hair: 'brown', grade: 7}; const kids = [abby, fred]; R.project(['name', 'grade'], kids); //=> [{name: 'Abby', grade: 2}, {name: 'Fred', grade: 7}] ``` -------------------------------- ### insertAll Source: https://ramdajs.com/docs Inserts all elements from a sub-list into a list at a specified index, returning a new list. ```APIDOC ## insertAll List `Number → [a] → [a] → [a]` ### Parameters * **index** The position to insert the sub-list * **elts** The sub-list to insert into the Array * **list** The list to insert the sub-list into ### Returns Array A new Array with `elts` inserted starting at `index`. ### Description Inserts the sub-list into the list, at the specified `index`. _Note that this is not destructive_ : it returns a copy of the list with the changes. No lists have been harmed in the application of this function. ``` -------------------------------- ### Get own and prototype property keys of an object Source: https://ramdajs.com/docs Returns an array containing the names of all properties of the supplied object, including those inherited from its prototype chain. The order of properties in the output array is not guaranteed to be consistent across different JavaScript platforms. ```javascript const F = function() { this.x = 'X'; }; F.prototype.y = 'Y'; const f = new F(); R.keysIn(f); //=> ['x', 'y'] ``` -------------------------------- ### Get Empty Value of Type Source: https://ramdajs.com/docs Returns the empty value of its argument's type. Ramda defines the empty value of Array (`[]`), Object (`{}`), String (`''`), Map (`new Map()`), Set (`new Set()`), TypedArray, and Arguments. Other types are supported if they define `.empty` or implement the FantasyLand Monoid spec. Dispatches to the `empty` method of the first argument, if present. ```javascript R.empty(Just(42)); //=> Nothing() R.empty([1, 2, 3]); //=> [] R.empty('unicorns'); //=> '' R.empty({x: 1, y: 2}); //=> {} R.empty(Uint8Array.from('123')); //=> Uint8Array [] R.empty(Set); //=> Set() ``` -------------------------------- ### Create a Partially Applied Function Source: https://ramdajs.com/docs Takes a function `f` and a list of arguments, returning a new function that, when called, applies `f` to the initial arguments followed by the new arguments. Useful for creating specialized versions of general functions. ```javascript const multiply2 = (a, b) => a * b; const double = R.partial(multiply2, [2]); double(3); //=> 6 const greet = (salutation, title, firstName, lastName) => salutation + ', ' + title + ' ' + firstName + ' ' + lastName + '!'; const sayHello = R.partial(greet, ['Hello']); const sayHelloToMs = R.partial(sayHello, ['Ms.']); sayHelloToMs('Jane', 'Jones'); //=> 'Hello, Ms. Jane Jones!' ``` -------------------------------- ### Create Object from Key-Value Pairs with fromPairs Source: https://ramdajs.com/docs Creates a new object from a list of key-value pairs. If a key appears multiple times, the rightmost pair is used. ```javascript R.fromPairs([['a', 1], ['b', 2], ['c', 3]]); //=> {a: 1, b: 2, c: 3} ``` -------------------------------- ### apply Source: https://ramdajs.com/docs Applies function `fn` to the argument list `args`. This is useful for creating a fixed-arity function from a variadic function. ```APIDOC ## apply Function `(*… → a) → [*] → a` ### Parameters * **fn** ((*… → a)) - The function which will be called with `args` * **args** ([*]) - The arguments to call `fn` with ### Returns a - The result, equivalent to `fn(...args)` ### Example ```javascript const nums = [1, 2, 3, -99, 42, 6, 7]; R.apply(Math.max, nums); //=> 42 ``` ``` -------------------------------- ### Create a new object with specified properties Source: https://ramdajs.com/docs Constructs a new object containing only the properties listed in the `names` array from the source object. Properties not found in the source are ignored. ```javascript R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4} ``` ```javascript R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1} ``` -------------------------------- ### Create Object from Keys and Values with R.zipObj Source: https://ramdajs.com/docs Use R.zipObj to create an object where keys are taken from the first list and values from the second. Pairing is truncated to the shorter list's length. ```javascript R.zipObj(['a', 'b', 'c'], [1, 2, 3]); //=> {a: 1, b: 2, c: 3} ``` -------------------------------- ### Insert Sub-list into List at Index Source: https://ramdajs.com/docs Use `insertAll` to create a new list with a sub-list inserted at a specific index. This operation is not destructive and returns a copy. ```javascript R.insertAll(2, ['x','y','z'], [1,2,3,4]); //=> [1,2,'x','y','z',3,4] ``` -------------------------------- ### keys Object Source: https://ramdajs.com/docs Returns a list containing the names of all the enumerable own properties of the supplied object. ```APIDOC ## keys Object `{k: v} → [k]` Parameters * obj The object to extract properties from Returns Array An array of the object's own properties. Added in v0.1.0 Returns a list containing the names of all the enumerable own properties of the supplied object. Note that the order of the output array is not guaranteed to be consistent across different JS platforms. See also keysIn, values, toPairs. ```javascript R.keys({a: 1, b: 2, c: 3}); //=> ['a', 'b', 'c'] ``` ``` -------------------------------- ### construct Function Source: https://ramdajs.com/docs Wraps a constructor function inside a curried function that can be called with the same arguments and returns the same type. ```APIDOC ## construct Function `(* → {*}) → (* → {*})` ### Parameters * fn - The constructor function to wrap. ### Returns function Added in v0.1.0 ### Description Wraps a constructor function inside a curried function that can be called with the same arguments and returns the same type. See also invoker. ### Request Example ```javascript // Constructor function function Animal(kind) { this.kind = kind; }; Animal.prototype.sighting = function() { return "It's a " + this.kind + "!"; } const AnimalConstructor = R.construct(Animal) // Notice we no longer need the 'new' keyword: AnimalConstructor('Pig'); //=> {"kind": "Pig", "sighting": function (){...}}; const animalTypes = ["Lion", "Tiger", "Bear"]; const animalSighting = R.invoker(0, 'sighting'); const sightNewAnimal = R.compose(animalSighting, AnimalConstructor); R.map(sightNewAnimal, animalTypes); //=> ["It's a Lion!", "It's a Tiger!", "It's a Bear!"] ``` ``` -------------------------------- ### View Data Structure with Lens Source: https://ramdajs.com/docs Returns a 'view' of the given data structure, determined by the given lens. The lens's focus determines which portion of the data structure is visible. ```javascript const xLens = R.lensProp('x'); R.view(xLens, {x: 1, y: 2}); //=> 1 R.view(xLens, {x: 4, y: 2}); //=> 4 ``` -------------------------------- ### fromPairs Source: https://ramdajs.com/docs Creates a new object from a list of key-value pairs. If a key appears in multiple pairs, the rightmost pair is included in the object. ```APIDOC ## fromPairs List `[[k,v]] → {k: v}` ### Parameters * `pairs` - An array of two-element arrays that will be the keys and values of the output object. ### Returns Object - The object made by pairing up `keys` and `values`. ### Description Creates a new object from a list key-value pairs. If a key appears in multiple pairs, the rightmost pair is included in the object. ### Example ```javascript R.fromPairs([['a', 1], ['b', 2], ['c', 3]]); //=> {a: 1, b: 2, c: 3} ``` ``` -------------------------------- ### join List Source: https://ramdajs.com/docs Returns a string made by inserting the `separator` between each element and concatenating all the elements into a single string. ```APIDOC ## join List `String → [a] → String` Parameters * separator The string used to separate the elements. * xs The elements to join into a string. Returns String str The string made by concatenating `xs` with `separator`. Added in v0.1.0 Returns a string made by inserting the `separator` between each element and concatenating all the elements into a single string. See also split. ```javascript const spacer = R.join(' '); spacer(['a', 2, 3.4]); //=> 'a 2 3.4' R.join('|', [1, 2, 3]); //=> '1|2|3' ``` ``` -------------------------------- ### Create a new object by picking properties that satisfy a predicate Source: https://ramdajs.com/docs Builds a new object containing only the key-value pairs from the source object where the key satisfies the provided predicate function. Useful for filtering object properties based on key criteria. ```javascript const isUpperCase = (val, key) => key.toUpperCase() === key; R.pickBy(isUpperCase, {a: 1, b: 2, A: 3, B: 4}); //=> {A: 3, B: 4} ``` -------------------------------- ### objOf Object Source: https://ramdajs.com/docs Creates an object containing a single key:value pair. ```APIDOC ## objOf Object `String → a → {String:a}` ### Parameters * `key` (String) * `val` (a) ### Returns Object ### Added in v0.18.0 ### Description Creates an object containing a single key:value pair. ### See also pair ### Example ```javascript const matchPhrases = R.compose( R.objOf('must'), R.map(R.objOf('match_phrase')) ); matchPhrases(['foo', 'bar', 'baz']); //=> {must: [{match_phrase: 'foo'}, {match_phrase: 'bar'}, {match_phrase: 'baz'}]} ``` ``` -------------------------------- ### gt Source: https://ramdajs.com/docs Returns true if the first argument is greater than the second; false otherwise. ```APIDOC ## gt Relation `Ord a => a → a → Boolean` ### Parameters * `a` * `b` ### Returns Boolean ### Description Returns `true` if the first argument is greater than the second; `false` otherwise. ### Example ```javascript R.gt(2, 1); //=> true R.gt(2, 2); //=> false R.gt(2, 3); //=> false R.gt('a', 'z'); //=> false R.gt('z', 'a'); //=> true ``` ``` -------------------------------- ### Curry Function for Partial Application Source: https://ramdajs.com/docs Use `curry` to create a curried version of a function, allowing for partial application of arguments one at a time or in groups. The placeholder `R.__` can be used for gaps. ```javascript const addFourNumbers = (a, b, c, d) => a + b + c + d; const curriedAddFourNumbers = R.curry(addFourNumbers); const f = curriedAddFourNumbers(1, 2); const g = f(3); g(4); //=> 10 ``` ```javascript // R.curry not working well with default parameters const h = R.curry((a, b, c = 2) => a + b + c); h(1)(2)(7); //=> Error! (`3` is not a function!) ``` -------------------------------- ### pickBy Source: https://ramdajs.com/docs Returns a partial copy of an object containing only the keys that satisfy the supplied predicate. Added in v0.8.0. ```APIDOC ## pickBy Object `((v, k) → Boolean) → {k: v} → {k: v}` Parameters * pred A predicate to determine whether or not a key should be included on the output object. * obj The object to copy from Returns Object A new object with only properties that satisfy `pred` on it. Added in v0.8.0 Returns a partial copy of an object containing only the keys that satisfy the supplied predicate. See also pick, filter. ```javascript const isUpperCase = (val, key) => key.toUpperCase() === key; R.pickBy(isUpperCase, {a: 1, b: 2, A: 3, B: 4}); //=> {A: 3, B: 4} ``` ```