recursify
Functional programming has gotten bigger. The below functions(mostly built-in to JS) are defined using recursion. Enjoy.
Install
$ npm install recursify
Usage
var R = require('recursify');
R.map(function(x) { return x * 3}, [1,2,3]);
// return [3,6,9];
R
Recursify
-
R
-
~isString(input) ⇒
boolean
-
~isArray(input) ⇒
boolean
-
~isObject(input) ⇒
boolean
-
~isNumber(input) ⇒
boolean
-
~isFunction(input) ⇒
boolean
-
~isSimilar(input1, input2) ⇒
boolean
-
~isEmpty(input) ⇒
boolean
-
~exists(value, list) ⇒
boolean
-
~map(func, list) ⇒
array
-
~each(func, list) ⇒
array
-
~filter(func, list) ⇒
array
-
~reverse(list) ⇒
array
-
~insert(value, list) ⇒
array
-
~insertDesc(value, list) ⇒
array
-
~sort(list) ⇒
array
-
~sortDesc(list) ⇒
array
-
~qsort(list) ⇒
array
-
~qsortDesc(list) ⇒
array
-
~foldr(func, value, list) ⇒
*
-
~foldl(func, value, list) ⇒
*
-
~addr(list1, list2) ⇒
array
-
~addl(list1, list2) ⇒
array
-
~rgive(value, list, mutate) ⇒
array
-
~lgive(value, list, mutate) ⇒
array
-
~cplist(list) ⇒
array
-
~indexOf(match, list, index) ⇒
number
-
~genList(size, function) ⇒
array
-
~keys(obj) ⇒
array
-
~lenObj(obj) ⇒
number
-
~cpobj(obj) ⇒
object
-
~lmerge(obj1, obj2) ⇒
object
-
~rmerge(obj1, obj2) ⇒
object
-
~eachObj(func, obj) ⇒
object
-
~mapObj(func, obj) ⇒
object
-
~foldObj(func, start, obj) ⇒
*
-
~times(number, function) ⇒
undefined
-
~isString(input) ⇒
boolean
R~isString(input) ⇒ isString Function
Kind: inner method of R
Param | Type |
---|---|
input | * |
Example
isString('test');
// return true;
boolean
R~isArray(input) ⇒ isArray function
Kind: inner method of R
Param | Type |
---|---|
input | * |
Example
isArray([1,2,3]);
// return true;
boolean
R~isObject(input) ⇒ isObject function
Kind: inner method of R
Param | Type |
---|---|
input | * |
Example
isObject({});
// return true;
boolean
R~isNumber(input) ⇒ isNumber function
Kind: inner method of R
Param | Type |
---|---|
input | * |
Example
isNumber(1);
// return true;
boolean
R~isFunction(input) ⇒ isFunction function
Kind: inner method of R
Param | Type |
---|---|
input | * |
Example
isFunction(function(){});
// return true;
boolean
R~isSimilar(input1, input2) ⇒ isSimilar function
Kind: inner method of R
Param | Type |
---|---|
input1 | * |
input2 | * |
Example
isSimilar({}, []);
// return false;
boolean
R~isEmpty(input) ⇒ isEmpty Function
Kind: inner method of R
Param | Type |
---|---|
input | * |
Example
isEmpty([]);
// return true;
boolean
R~exists(value, list) ⇒ Exists Function
Kind: inner method of R
Param | Type | Description |
---|---|---|
value | * |
Value to see if it exists |
list | array |
List to search |
Example
exists(1, [1,2,3]);
// return true;
Example
exists(1, [2,3]);
// return false;
array
R~map(func, list) ⇒ Recursive Map Function
Kind: inner method of R
Returns: array
- New array
Param | Type | Description |
---|---|---|
func | function |
Function to execute on each element of list |
list | array |
Input array |
Example
map(function(x) { return x * 2; }, [1,2,3]);
// return [2,4,6];
array
R~each(func, list) ⇒ Recursive Each Function
Kind: inner method of R
Returns: array
- Input array
Param | Type | Description |
---|---|---|
func | function |
Function to execute on each element of list |
list | array |
Input array |
Example
var count = 0;
each(function(x) { return count += x; }, [1,2,3]);
// return [1,2,3]
// count => 6;
array
R~filter(func, list) ⇒ Recursive Filter Function
Kind: inner method of R
Returns: array
- New filtered array
Param | Type | Description |
---|---|---|
func | function |
Function to execute on each element of list (must return boolean) |
list | array |
Input array |
Example
filter(function(x) { return x < 3; }, [1,2,3]);
// return [1,2];
array
R~reverse(list) ⇒ Recursive Reverse Function
Kind: inner method of R
Returns: array
- New reversed array
Param | Type | Description |
---|---|---|
list | array |
Input array |
Example
reverse([1,2,3]);
// return [3,2,1];
array
R~insert(value, list) ⇒ Recursive Insert Function
Kind: inner method of R
Returns: array
- New array with inserted value
Param | Type | Description |
---|---|---|
value | * |
Value to insert into list |
list | array |
Input array (presorted ascending) |
Example
insert(2, [1,4,5]);
// return [1,2,4,5];
array
R~insertDesc(value, list) ⇒ Recursive Insert Function
Kind: inner method of R
Returns: array
- New array with inserted value
Param | Type | Description |
---|---|---|
value | * |
Value to insert into list |
list | array |
Input array (presorted descending) |
Example
insertDesc(3, [4,2,1]);
// return [4,3,2,1];
array
R~sort(list) ⇒ Recursive Sort Function (ascending)
Kind: inner method of R
Returns: array
- New sorted array-ascending
Param | Type | Description |
---|---|---|
list | array |
Input array |
Example
sort([1,5,3,3,1,3,5,6]);
// return [1,1,3,3,3,5,5,6];
array
R~sortDesc(list) ⇒ Recursive Sort Function (descending)
Kind: inner method of R
Returns: array
- New sorted array-descending
Param | Type | Description |
---|---|---|
list | array |
Input array |
Example
sortDesc([1,4,2,4,1,3]);
// return [4,4,3,2,1,1];
array
R~qsort(list) ⇒ Quick Sort Function (ascending)
Kind: inner method of R
Returns: array
- New sorted-unique-ascending
Param | Type | Description |
---|---|---|
list | array |
Input array |
Example
qsort([5,4,1,2,4,1,3,4,6]);
// return [1,2,3,4,5,6];
array
R~qsortDesc(list) ⇒ Quick Sort Function (descending)
Kind: inner method of R
Returns: array
- New sorted-unique-descending
Param | Type | Description |
---|---|---|
list | array |
Input array |
Example
qsortDesc([1,5,2,1,3,5,6,7,3]);
// return [7,6,5,3,2,1];
*
R~foldr(func, value, list) ⇒ Recursive Foldr Function
Kind: inner method of R
Returns: *
- Result after collecting
Param | Type | Description |
---|---|---|
func | function |
Function which takes the following arguments (currentValue, elementValue, index) |
value | * |
Starting value |
list | array |
Input array |
Example
foldr(function(x, y) { return x + y; }, 10, [1,2,3]);
// return 16;
*
R~foldl(func, value, list) ⇒ Recursive Foldl Function
Kind: inner method of R
Returns: *
- Result after collecting
Param | Type | Description |
---|---|---|
func | function |
Function which takes the following arguments (currentValue, elementValue, index) |
value | * |
Starting value |
list | array |
Input array |
Example
foldl(function(x, y) { return x + y; }, 10, [1,2,3]);
// return 16;
array
R~addr(list1, list2) ⇒ Recursive Right Concatenation Function
Kind: inner method of R
Returns: array
- New array of list1 + list2
Param | Type |
---|---|
list1 | array |
list2 | array |
Example
addr([1,2,3], [4,5,6]);
// return [1,2,3,4,5,6];
array
R~addl(list1, list2) ⇒ Recursive Left Concatenation Function
Kind: inner method of R
Returns: array
- New array of list2 + list1;
Param | Type |
---|---|
list1 | array |
list2 | array |
Example
addl([1,2,3], [4,5,6]);
// return [4,5,6,1,2,3];
array
R~rgive(value, list, mutate) ⇒ Append value to list
Kind: inner method of R
Returns: array
- New array of appended value
Param | Type | Description |
---|---|---|
value | * |
|
list | array |
|
mutate | boolean |
if true, mutate input list. Default: false |
Example
rgive(1, [4,3,2]);
// return [4,3,2,1];
array
R~lgive(value, list, mutate) ⇒ Prepend value to list
Kind: inner method of R
Returns: array
- New array of prepended value
Param | Type | Description |
---|---|---|
value | * |
|
list | array |
|
mutate | boolean |
if true, mutate input list: Default false |
Example
lgive(1, [2,3,4]);
// return [1,2,3,4];
array
R~cplist(list) ⇒ Copy array (Array.prototype.slice wrapper)
Kind: inner method of R
Param | Type |
---|---|
list | array |
Example
var a = [1,2,3];
var b = cplist(a);
a == b
// return false;
number
R~indexOf(match, list, index) ⇒ IndexOf Function
Kind: inner method of R
Returns: number
- index value if match found. Returns -1 if no match.
Param | Type | Description |
---|---|---|
match | * |
Value to search for |
list | array |
Array to search through |
index | number |
Starting index to search from: Default 0 |
Example
indexOf('l', 'hello');
// return 2;
Example
indexOf('a', 'hello');
// return -1;
array
R~genList(size, function) ⇒ genList Function
Kind: inner method of R
Returns: array
- New array of generated values
Param | Type | Description |
---|---|---|
size | number |
indicate size of array desired |
function | function |
to execute for each element of array. Takes argument (currentIndex) |
Example
genList(5, function(x) { return x * 3; });
// return [0,3,6,9,12];
array
R~keys(obj) ⇒ keys functions
Kind: inner method of R
Param | Type |
---|---|
obj | object |
Example
keys({a: 1, b: 2});
// return ['a', 'b'];
number
R~lenObj(obj) ⇒ lenObj function
Kind: inner method of R
Param | Type |
---|---|
obj | object |
Example
lenObj({a: 1, b: 2});
// return 2;
object
R~cpobj(obj) ⇒ Copy Object Function
Kind: inner method of R
Returns: object
- Copy of object
Param | Type |
---|---|
obj | object |
Example
var a = {hello: 'world'};
var b = cpboj(a);
b === a;
// return false
object
R~lmerge(obj1, obj2) ⇒ Merge Left Function
Kind: inner method of R
Returns: object
- obj1
Param | Type |
---|---|
obj1 | object |
obj2 | object |
Example
var a = {a: 1};
var b = {b: 2};
lmerge(a, b);
// return {a: 1, b:2};
// a => {a: 1,b: 2};
// b => {b: 2};
object
R~rmerge(obj1, obj2) ⇒ Merge Right Function
Kind: inner method of R
Returns: object
- obj2
Param | Type |
---|---|
obj1 | object |
obj2 | object |
Example
var a = {a: 1};
var b = {b: 2};
rmerge(a,b);
// return {a: 1,b: 2};
// a => {a: 1};
// b => {a: 1,b: 2};
object
R~eachObj(func, obj) ⇒ EachObj Function
Kind: inner method of R
Returns: object
- - input object
Param | Type | Description |
---|---|---|
func | function |
function that executes with (value, key) as arguments |
obj | object |
Example
var a = {a: 1, b: 2};
var count = 0;
eachObj(function(value, key) { count += value; }, a);
// return {a: 1, b: 2}
// count => 3;
object
R~mapObj(func, obj) ⇒ MapObj Function
Kind: inner method of R
Returns: object
- - new object
Param | Type | Description |
---|---|---|
func | function |
function that executes with (value, key, {}) as arguments |
obj | object |
Example
var a = {a: 1, b: 2}
mapObj(function(value, key, obj) {
obj[key + value] = value * 3;
}, a);
// return { a1: 3, b2: 6};
*
R~foldObj(func, start, obj) ⇒ FoldObj Function
Kind: inner method of R
Param | Type | Description |
---|---|---|
func | function |
function to execute upon iteration with arguments (result, value, key) |
start | * |
starting value when executing foldObj |
obj | object |
Example
var a = {a: 1, b: 2}
foldObj(function(result, val, key) { return result + val }, 0, a);
// return 3;
undefined
R~times(number, function) ⇒ times Function
Kind: inner method of R
Param | Type | Description |
---|---|---|
number | number |
of times to execute function |
function | function |
to execute: takes 1 argument (currentNumber) |
Example
times(3, function(x) { console.log('Number: ', x); });
// return Number: 3
// return Number: 2
// return Number: 1
// return undefined
Contributing
New functions, issues, and pull requests welcome
- Fork it
- Create your feature branch (git checkout -b feature/my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin feature/my-new-feature)
- Create a new Pull Request