recursify

0.0.14 • Public • Published

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

isString Function

Kind: inner method of R

Param Type
input *

Example

isString('test');
// return true;

R~isArray(input) ⇒ boolean

isArray function

Kind: inner method of R

Param Type
input *

Example

isArray([1,2,3]);
// return true;

R~isObject(input) ⇒ boolean

isObject function

Kind: inner method of R

Param Type
input *

Example

isObject({});
// return true;

R~isNumber(input) ⇒ boolean

isNumber function

Kind: inner method of R

Param Type
input *

Example

isNumber(1);
// return true;

R~isFunction(input) ⇒ boolean

isFunction function

Kind: inner method of R

Param Type
input *

Example

isFunction(function(){});
// return true;

R~isSimilar(input1, input2) ⇒ boolean

isSimilar function

Kind: inner method of R

Param Type
input1 *
input2 *

Example

isSimilar({}, []);
// return false;

R~isEmpty(input) ⇒ boolean

isEmpty Function

Kind: inner method of R

Param Type
input *

Example

isEmpty([]);
// return true;

R~exists(value, list) ⇒ boolean

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;

R~map(func, list) ⇒ array

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];

R~each(func, list) ⇒ array

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;

R~filter(func, list) ⇒ array

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];

R~reverse(list) ⇒ array

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];

R~insert(value, list) ⇒ array

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];

R~insertDesc(value, list) ⇒ array

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];

R~sort(list) ⇒ array

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];

R~sortDesc(list) ⇒ array

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];

R~qsort(list) ⇒ array

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];

R~qsortDesc(list) ⇒ array

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;

R~addr(list1, list2) ⇒ array

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];

R~addl(list1, list2) ⇒ array

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];

R~rgive(value, list, mutate) ⇒ array

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];

R~lgive(value, list, mutate) ⇒ array

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];

R~cplist(list) ⇒ array

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;

R~indexOf(match, list, index) ⇒ number

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;

R~genList(size, function) ⇒ array

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];

R~keys(obj) ⇒ array

keys functions

Kind: inner method of R

Param Type
obj object

Example

keys({a: 1, b: 2});
// return ['a', 'b'];

R~lenObj(obj) ⇒ number

lenObj function

Kind: inner method of R

Param Type
obj object

Example

lenObj({a: 1, b: 2});
// return 2;

R~cpobj(obj) ⇒ object

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

R~lmerge(obj1, obj2) ⇒ object

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};

R~rmerge(obj1, obj2) ⇒ object

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};

R~eachObj(func, obj) ⇒ object

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;

R~mapObj(func, obj) ⇒ object

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;

R~times(number, function) ⇒ undefined

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

  1. Fork it
  2. Create your feature branch (git checkout -b feature/my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin feature/my-new-feature)
  5. Create a new Pull Request

Package Sidebar

Install

npm i recursify

Weekly Downloads

1

Version

0.0.14

License

MIT

Last publish

Collaborators

  • antiokus314