Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Full Version | Homepage
Lists is a library of JavaScript higher-order functions for working with arrays and strings. The library was inspired by Haskell's Data.List module by the powerful people over at The University of Glasgow. You can view the original source here. You could then scroll through each function closely and see a purposefully similar correlation between the Haskell implementation and this implementation.
Pass functions to functions to functions to solve complex problems. Most of the functions featured in Lists produce new arrays, to reinforce the paradigm of stateless programming.
Disclaimer: Almost all of these functions are naive recursive definitions. The idea behind this library is to maximize problem-solving expressivity. This library provides an alternative toolbox by which to solve problems. This library should be sufficient for most projects but remember to monitor your heap memory space in the case of excessive function calls.
To import the entire library:
npm install --save lists
Using in your Node.js project:
var lists = require('lists');
Using in your browser:
Download the script source file lists.min.js
and include it in your HTML. Lists is exported as a global object attached to window
. Access functions by calling lists
or window.lists
.
<script src='lists.min.js'></script>
<script>
lists.head(["Hey!"]); // "Hey!"
</script>
Lets complete an easy task from /r/dailyprogrammer. Here is the task:
Given a NxN size 2D array of numbers. Develop a way to rotate the data as if you rotated the data by 90 degrees clockwise.
The challenge input is a 10x10 matrix that looks like this:
var matrix =
[[1,2,3,4,5,6,7,8,9,0],
[0,9,8,7,6,5,4,3,2,1],
[1,3,5,7,9,2,4,6,8,0],
[0,8,6,4,2,9,7,5,3,1],
[0,1,2,3,4,5,4,3,2,1],
[9,8,7,6,5,6,7,8,9,0],
[1,1,1,1,1,1,1,1,1,1],
[2,2,2,2,2,2,2,2,2,2],
[9,8,7,6,7,8,9,8,7,6],
[0,0,0,0,0,0,0,0,0,0]];
Using Lists, we can reverse and then transpose this matrix to achieve the desired output:
var l = require('lists');
l.transpose(l.rev(matrix));
// or using pipe (composing left)
var rotate90 = l.pipe(l.transpose, l.rev);
rotate90(matrix);
/* both of these functions yield
[[0,9,2,1,9,0,0,1,0,1],
[0,8,2,1,8,1,8,3,9,2],
[0,7,2,1,7,2,6,5,8,3],
[0,6,2,1,6,3,4,7,7,4],
[0,7,2,1,5,4,2,9,6,5],
[0,8,2,1,6,5,9,2,5,6],
[0,9,2,1,7,4,7,4,4,7],
[0,8,2,1,8,3,5,6,3,8],
[0,7,2,1,9,2,3,8,2,9],
[0,6,2,1,0,1,1,0,1,0]];
/*
A{ 1. } Function number
B{ tail } Function name
C{ [x]|str -> [x] } Pseudo type signature
tail : [x]|str -> [x]
map : [x] -> f -> [x]
Basic Functions
append
: [x]|str -> [x]|str|num -> [x]|strhead
: [x]|str -> xlast
: [x]|str -> xinit
: [x]|str -> [x]tail
: [x]|str -> [x]nil
: [x]|str -> booleanList Transformations
map
: [x]|str -> f -> [x]rev
: [x]|str -> [x]intersperse
: x -> [x]|str -> [x]|strintercalate
: [x] -> [[x]] -> [x]transpose
: [[x]] -> [[x]]subsequences
: [x]|str -> [[x]]permutations
: [x]|str -> [[x]]|[str]Reducing Lists (folds)
foldl
: x -> [x]|str -> f -> xfoldl1
: [x]|str -> f -> xfoldr
: x -> [x]|str -> f -> xfoldr1
: [x]|str -> f -> xflatten
|| concat
: [[x]]|[str] -> [x]|strconcatMap
: [x]|str -> f -> [x]|strand
: [boolean] -> booleanor
: [boolean] -> booleanany
: [x]|str -> f -> booleanall
: [x]|str -> f -> booleansum
: [num] -> numproduct
: [num] -> nummaximum
: [num] -> numminimum
: [num] -> nummaxList
: [[x]] -> [x]minList
: [[x]] -> [x]Building Lists
scanl
: x -> [x]|str -> f -> [x]scanr
: x -> [x]|str -> f -> [x]mapAccumL
: x -> [x]|str -> f -> [x, [x]]mapAccumR
: x -> [x]|str -> f -> [x, [x]]iterate
: x -> num -> f -> [x]replicate
: x -> num -> [x]cycle
: [x]|str -> num -> [x]|strunfold
: f -> f -> f -> x -> [x]|strSublists
take
: num -> [x]|str -> [x]drop
: num -> [x]|str -> [x]splitAt
: num -> [x]|str -> [[x],[x]]takeWhile
: f -> [x]|str -> [x]dropWhile
: f -> [x]|str -> [x]span
: [x]|str -> f -> [[x], [x]|str]break
: [x]|str -> f -> [[x], [x]|str]stripPrefix
: [num]|str -> [num]|str -> [num]|[str]group
: [num]|str -> [[num]]|[[str]]inits
: [x]|str -> [[x]]tails
: [x]|str -> [[x]]Predicates
isPrefixOf
: [num]|str -> [num]|str -> booleanisSuffixOf
: [num]|str -> [num]|str -> booleanisInfixOf
: [num]|str -> [num]|str -> booleanisSubsequenceOf
: [num]|str -> [num]|str -> booleanSearching Lists
elem
: num|str -> [num]|str -> booleannotElem
: num|str -> [num]|str -> booleanlookup
: num|str -> [[num|str,x]] -> booleanfind
: [x]|str -> f -> xfilter
: [x]|str -> f -> [x]partition
: [x]|str -> f -> [[x],[x]]Indexing Lists
bang
: num -> [x] -> xelemIndex
|| indexOf
: num|str -> [num]|str -> numelemIndices
: num|str -> [num]|str -> [num]findIndex
: [x]|str -> f -> numfindIndices
: [x]|str -> f -> [num]Zipping and Unzipping Lists
zip
|| unzip
: [[a,b,..,n],[c,d,..,n],..,n] -> [[a,c,..,n],[b,d,..,n],..,n]zipWith
: [[a,b,..,n],[c,d,..,n],..,n] -> f -> [x]Special Lists
lines
: str -> [str]words
: str -> [str]unlines
: [str] -> strunwords
: [str] -> strnub
|| uniq
|| unique
: [num|str]|str -> [num|str]delete
: x -> [x]|str -> [x]difference
|| diff
: [num|str]|str -> [num|str]|str -> [x]union
: [num|str]|str -> [num|str]|str -> [x]|strintersect
: [num|str]|str -> [num|str]|str -> [x]sort
: [x]|str -> [x]insert
: x -> [x] -> [x]Generalized Functions
nubBy
: [x]|str -> f -> [x]deleteBy
: x -> [x]|str -> f -> [x]deleteFirstsBy
: [x]|str -> [x]|str -> f -> [x]unionBy
: [x] -> [x] -> f -> [x]intersectBy
: [x]|str -> [x]|str -> f -> [x]groupBy
: [x]|str -> f -> [[x]]sortBy
: [x]|str -> f -> [x]insertBy
: x -> [x]|str -> f -> [x]maximumBy
: [x]|str -> f -> xminimumBy
: [x]|str -> f -> xExtra Functional Utilities
genericExcludeChar
: str -> str -> [str]forEach
|| each
: x -> f -> xkeys
: obj -> [str]enumeration
|| enum
: num -> num -> [num]composeL
|| pipe
: f[,..,f] -> fcomposeR
|| sequence
: f[,..,f] -> fpartial
|| part
: f -> x[,..,x] -> fflip
: f -> fcompare
: x -> x -> strbubbleSort
: [num] -> [num]mergeSort
: [num] -> [num]Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 an Array of Variables or a String or a Number. Get an Array of Variables or a String.
Example Usage:
lists.append([1],[2]); /* [1,2] */
lists.append([1],2); /* [1,2] */
lists.append('a','b'); /* 'ab' */
Signature Definition: Give arg 1 an Array of Variables or a String. Get a Variable.
Example Usage:
lists.head([1,2]); /* 1 */
lists.head([[1,2],[3,4]]); /* [1,2] */
lists.head('ab'); /* 'a' */
Signature Definition: Give arg 1 an Array of Variables or a String. Get a Variable.
Example Usage:
lists.last([1,2]); /* 2 */
lists.last([[1,2],[3,4]]); /* [3,4] */
lists.last('ab'); /* 'b' */
Signature Definition: Give arg 1 an Array of Variables or a String. Get an Array of Variables.
Example Usage:
lists.init([1,2,3]); /* [1,2] */
lists.init([[1,2],[3,4],[5,6]]); /* [[1,2],[3,4]] */
lists.init('abc'); /* ['a','b'] */
Signature Definition: Give arg 1 an Array of Variables or a String. Get an Array of Variables.
Example Usage:
lists.tail([1,2,3]); /* [2,3] */
lists.tail([[1,2],[3,4],[5,6]]); /* [[3,4],[5,6]] */
lists.tail('abc'); /* ['b','c'] */
Signature Definition: Give arg 1 an Array of Variables or a String; Get a boolean.
Example Usage:
lists.nil(null); /* true */
lists.nil([]); /* true */
lists.nil(''); /* true */
lists.nil('a'); /* false */
lists.nil([1]); /* false */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Function. Get an Array of Variables.
Example Usage:
lists.map([1,2,3], function(x) { return x*2 }); /* [2,4,6] */
lists.map('abc', function(str) { return str.toUpperCase() }); /* ["A","B","C"] */
lists.map([{'S': 1}, {'u': 2}, {'p': 3}], function(obj) {
return lists.flatten(lists.keys(obj))
}); /* ["S", "u", "p"] */
Signature Definition: Give arg 1 an Array of Variables or a String. Get an Array of Variables.
Example Usage:
lists.rev([1,2,3]); /* [3,2,1] */
lists.rev('abc'); /* ['c','b','a'] */
lists.rev([[2],[3]]); /* [[3],[2]] */
Signature Definition: Give arg 1 a Variable. Give arg 2 an Array of Variables or a String. Get an Array of Variables or a String.
Example Usage:
lists.intersperse(1,[5,5,5]); /* [5,1,5,1,5] */
lists.intersperse([1,2],[[6],[6]]); /* [[6],[1,2],[6]] */
lists.intersperse('b','ac'); /* 'abc' */
lists.intersperse({b:2},[{a:1},{b:3}]); /* [{a:1},{b:2},{c:3}] */
Signature Definition: Give arg 1 an Array of Variables. Give arg 2 an Array of an Array of Variables. Get an Array of Variables.
Example Usage:
lists.intercalate([1],[[5],[5],[5]]); /* = lists.flatten(lists.intersperse([1],[[5],[5],[5]])) // [5,1,5,1,5] */
lists.intercalate(["abc"],[["efg"],["qrs"]]); /* ["efg", "abc", "qrs"] */
lists.intercalate([{a:1}],[[{b:1}],[{c:2}]]); /* [{b:1},{a:1},{c:2}] */
Signature Definition: Give arg 1 an Array of an Array of Variables. Get an Array of an Array of Variables.
Example Usage:
lists.transpose([[1,2,4],[3,4,4]]); /* [[1,3],[2,4],[4,4]] */
lists.transpose([["a","b","c"],["a","b","c"]]); /* [["a","a"],["b","b"],["c","c"]] */
Signature Definition: Give arg 1 an Array of Variables or a String. Get an Array of an Array of Variables.
Example Usage:
lists.subsequences('ab'); /* [[],['a'],['b'],['ab']] */
lists.subsequences([1,2,3]); /* [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] */
Signature Definition: Give arg 1 an Array of Variables or a String. Get an Array of an Array of Variables or an Array of Strings respectively.
Example Usage:
lists.permutations('abc'); /* ["abc","acb","bac","bca","cab","cba"] */
lists.permutations([1,2,3]); /* [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] */
Signature Definition: Give arg 1 a starting Variable (usually a left identity of the binary operator). Give arg 2 an Array of Variables or a String. Give arg 3 a Function (binary operator). Get a Variable.
Example Usage:
reverse = lists.foldl('','abc',function(x,y){ return y.concat(x); }); /* "cba" */
sum = lists.foldl(0,[1,2,3],function(x,y){ return x+y; }); /* 6 */
lists.foldl([], [[1,2],[3,4]], function(x,y) {return x.concat(y) }); /* [1,2,3,4] */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Function (binary operator). Get a Variable.
Example Usage:
lists.foldl1('abc',function(x,y){ return x.concat(y).toUpperCase() }); /* "ABC" */
lists.foldl1([1,2,3],function(x,y){ return x+y }); /* 6 */
Signature Definition: Give arg 1 a starting Variable (usually a right identity of the binary operator). Give arg 2 an Array of Variables or a String. Give arg 3 a Function (binary operator). Get a Variable.
Example Usage:
lists.foldr(0,[1,2,3,4],function(x,y){ return x-y; }); /* -2 */
lists.foldr([],[[1,2],[3,4],[5,6]],function(x,y){
return lists.rev(x).concat(y);
}); /* [4,3,1,2,5,6] */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Function (binary operator). Get a Variable.
Example Usage:
lists.foldr1([1,2,3],function(x,y){ return x - y }); /* 3 */
lists.foldr1('aabbcc',function(x,y){ return x=='a'? x=y : x.concat(y)}); /* "bbcc" */
Signature Definition: Give arg 1 an Array of an Array of Variables or an Array of Strings. Get an Array of Variables or a String.
Example Usage:
lists.flatten(['abc']); /* 'abc' */
lists.flatten([[1,2],[3,4]]); /* [1,2,3,4] */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Function that produces an Array of Variables or a String. Get an Array of Variables or a String.
Example Usage:
lists.concatMap(['bang','bang'], function(x){ return x+'!'}); /* "bang!bang!" */
lists.concatMap([1,2,3],function(x){ return [[x*2,x/2]] }); /* [[2,0.5],[4,1],[6,1.5]] */
lists.concatMap([{a:1},{b:2}], function(x){
x.prop = 'hi';
return [x]
}); /* [{a:1,prop:"hi"},{b:2,prop:"hi"}]*/
Signature Definition: Give arg 1 an Array of booleans. Get a boolean.
Example Usage:
lists.and([5>1,5>2,5>3]); /* true */
lists.and([5>1,false,5>3]); /* false */
Signature Definition: Give arg 1 an Array of booleans. Get a boolean.
Example Usage:
lists.or([5<1,5<2,5>3]); /* true */
lists.or([5<1,5<2,5<3]); /* false */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a predicate Function to be applied to each element of arg 1. Get a boolean.
Example Usage:
lists.any([1,2,3],function(x) { return x < .5}); /* false */
lists.any('abc',function(x) { return x == 'b'}); /* true */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a predicate Function to be applied to each element of arg 1. Get a boolean.
Example Usage:
lists.all('abc', function(x){ return x==x.toUpperCase() }); /* false */
lists.all([2,4], function(x) { return x > 3 }); /* false */
lists.all([2,4], function(x) { return x%2==0 }); /* true */
Signature Definition: Give arg 1 an Array of Numbers. Get a Number.
Example Usage:
lists.sum([2,4,6]); /* 12 */
lists.sum([.2,.4,.6]); /* 1.2000000000000002 */
Signature Definition: Give arg 1 an Array of Numbers. Get a Number.
Example Usage:
lists.product([2,4,6]); /* 48 */
lists.product([.2,.4,.6]); /* 0.04800000000000001 */
Signature Definition: Give arg 1 an Array of Numbers. Get a Number.
Example Usage:
lists.maximum([2,4,6]); /* 6 */
lists.maximum([.2,.4,.6]); /* .6 */
Signature Definition: Give arg 1 an Array of Numbers. Get a Number.
Example Usage:
lists.minimum([2,4,6]); /* 2 */
lists.minimum([.2,.4,.6]); /* .2 */
Signature Definition: Give arg 1 an Array of an Array of Variables. Get an Array of Variables.
Example Usage:
lists.maxList([[1],[2,3]]); /* [2,3] */
lists.maxList([[1,2],[3]]); /* [1,2] */
Signature Definition: Give arg 1 an Array of an Array of Variables. Get an Array of Variables.
Example Usage:
lists.minList([[],[1]]); /* [] */
lists.minList([[1,2],[3]]); /* [3] */
Signature Definition: Give arg 1 a starting Variable. Give arg 2 an Array of Variables or a String. Give arg 3 a Function (binary operator). Get an Array of Variables.
Example Usage:
lists.scanl('.','abc',function(x,y){return x + y}); /* [".",".a",".ab",".abc"] */
lists.scanl(0,[1,2,3],function(x,y){return x + y}); /* [0,1,3,6] */
Signature Definition: Give arg 1 a starting Variable. Give arg 2 an Array of Variables or a String. Give arg 3 a Function (binary operator). Get an Array of Variables.
Example Usage:
lists.scanr('.','abc',function(x,y){return x + y}); /* ["abc.","bc.","c.","."] */
lists.scanr(0,[1,2,3],function(x,y){return x + y}); /* [6,5,3,0] */
Signature Definition: Give arg 1 a starting Variable (accumulator). Give arg 2 an Array of Variables or a String. Give arg 3 a Function. Get an Array of Variable followed by Array of Variables.
Example Usage:
lists.mapAccumL(5, [2,4,8], function(x,y){ return [x+y,x*y]}); /* [19, [10,28,88]]*/
lists.mapAccumL(5, [2,4,8], function(x,y){ return [y,y]}); /* [8, [2,4,8]] */
lists.mapAccumL(5, [5,5,5], function(x,y){ return [x,x]}); /* [5, [5,5,5]] */
Signature Definition: Give arg 1 a starting Variable (accumulator). Give arg 2 an Array of Variables or a String. Give arg 3 a Function. Get an Array of Variable followed by Array of Variables.
Example Usage:
lists.mapAccumR(5, [2,4,8], function(x,y){ return [x+y,x*y]}); /* [19, [34,52,40]]*/
lists.mapAccumR(5, [2,4,8], function(x,y){ return [y,y]}); /* [2, [2,4,8]] */
lists.mapAccumR(5, [5,5,5], function(x,y){ return [x,x]}); /* [5, [5,5,5]] */
Signature Definition: Give arg 1 a Variable. Give arg 2 a Number. Give arg 3 a Function. Get an Array of Variables.
Example Usage:
lists.iterate('a',3,function(ch){return ch+'b'}); /* ["a","ab","abb"] */
lists.iterate([1,2],3,function(xs){return lists.intersperse(6,xs)}); /* [[1,2],[1,6,2],[1,6,6,6,2]] */
lists.iterate(2,4,function(x){ return x*x }); /* [2,4,16,256] */
Signature Definition: Give arg 1 a Variable. Give arg 2 a Number. Get an Array of Variables.
Example Usage:
lists.replicate(5,5); /* [5,5,5,5,5] */
lists.replicate([1,2],2); /* [[1,2],[1,2]] */
lists.replicate({a:1},2); /* [{a:1},{a:2}] */
Signature Definition: Give arg 1 an Array of Variables. Give arg 2 a Number. Get an Array of Variables.
Example Usage:
lists.cycle('abc',3); /* "abcabcabc" */
lists.cycle([1,2],2); /* [1,2,1,2] */
Signature Definition: Give arg 1 an Function (predicate). Give arg 2 a Function. Give arg 3 a Function. Give arg 4 a Variable (seed).
Example Usage:
function chop8(xs){
return lists.unfold(lists.nil,lists.part(lists.take,8,undefined),lists.part(lists.drop,8,undefined),xs)
}
chop8([1,2,3,4,5,6,7,8,9]); /* [[1,2,3,4,5,6,7,8],[9]] */
function unfoldMap(xs,f) {
return lists.unfold(
lists.nil,
lists.part(lists.pipe(f,lists.head),undefined),
lists.part(lists.tail,undefined),
xs)
}
unfoldMap([1,2],function(x){ return x * 2 }); /* [2,4] */
Signature Definition: Give arg 1 a Number. Give arg 2 an Array of Variables or a String. Get an Array of Variables.
Example Usage:
lists.take(2,[1,2,3]); /* [1,2] */
lists.take(2,'abc'); /* ["a","b"] */
Signature Definition: Give arg 1 a Number. Give arg 2 an Array of Variables or a String. Get an Array of Variables.
Example Usage:
lists.drop(2,[1,2,3]); /* [3] */
lists.drop(2,'abc'); /* ["c"] */
Signature Definition: Give arg 1 a Number. Give arg 2 an Array of Variables or a String. Get an Array of two Arrays of Variables.
Example Usage:
lists.splitAt(2,'abc') /* [['a','b'],['c']] */
lists.splitAt(2,[1,2,3]) /* [[1,2],[3]] */
Signature Definition: Give arg 1 a Function. Give arg 2 an Array of Variables or a String. Get an Array of Variables.
Example Usage:
lists.takeWhile([1,2,3,1], function(x){ return x < 3 }); /* [1,2] */
lists.takeWhile([1], function(x){ return x < 2 }); /* [1] */
lists.takeWhile('aabc', function(x){ return x =='a' }); /* ["a","a"] */
Signature Definition: Give arg 1 a Function. Give arg 2 an Array of Variables or a String. Get an Array of Variables.
Example Usage:
lists.dropWhile([1,2,3,4], function(x){ return x < 3 }); /* [3,4] */
lists.dropWhile([1], function(x){ return x < 2 }); /* [2] */
lists.dropWhile('aabc', function(x){ return x =='a' }); /* ["b","c"] */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Function. Get an Array of an Array of Variables and Array of Variables or a String (psuedo-tuple).
Example Usage:
lists.span([1,2,3,4], function(x){return x < 3}); /* [[1,2],[3,4]] */
lists.span("abcde", function(x){return x == "a"}); /* [["a"],["b","c","d","e"]] */
lists.span([{a:2},{a:2},{b:2}], function(x){return x.a==2}); /* [[{a:2},{a:2}],[{b:2}]] */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Function. Get an Array of an Array of Variables and Array of Variables or a String (psuedo-tuple).
Example Usage:
lists.break([1,2,3,4], function(x){return x < 3}); /* [[],[1,2,3,4]] */
lists.break("abcde", function(x){return x == "a"}); /* [[],"abcde"] */
lists.break([{a:2},{a:2},{b:2}], function(x){return x.a==2}); /* [[],[{a:2},{a:2},{b:2}]] */
Signature Definition: Give arg 1 an Array of Numbers or a String. Give arg 2 an Array of Numbers or a String. Get an Array of Numbers or an Array of Strings.
Example Usage:
lists.stripPrefix("abc","abcabce"); /* ["a","b","c","e"] */
lists.stripPrefix([1,2],[1,2,3,4]); /* [3,4] */
Signature Definition: Give arg 1 an Array of Numbers or a String. Get an Array of an Array of Numbers or Strings.
Example Usage:
lists.group([1,1,2,3,3]); /* [[1,1],[2],[3,3]] */
lists.group("mississippi"); /* [["m"],["i"],["s","s"],["i"],["s","s"],["i"],["p","p"],["i"]] */
Signature Definition: Give arg 1 an Array of Variables or a String. Get an Array of an Array of Variables.
Example Usage:
lists.inits([1,2,3]); /* [[],[1],[1,2],[1,2,3]] */
lists.inits("abc"); /* [[],["a"],["a","b"],["a","b","c"]] */
lists.inits([{a:2},{b:3}]); /* [[],[{a:2}],[{a:2},{b:3}]] */
Signature Definition: Give arg 1 an Array of Variables or a String. Get an Array of an Array of Variables.
Example Usage:
lists.tails([1,2,3]); /* [[1,2,3],[1,2],[1],[]] */
lists.tails("abc"); /* [["a","b","c"],["a","b"],["a"],[]] */
lists.tails([{a:2},{b:3}]); /* [[{a:2},{b:3}],[{a:2}],[]] */
Signature Definition: Give arg 1 an Array of Numbers or a String. Give arg 2 an Array of Numbers or a String. Get a boolean.
Example Usage:
lists.isPrefixOf([1,2],[1,2,3]); /* true */
lists.isPrefixOf("abc","acd"); /* false */
lists.isPrefixOf("ab","abcd"); /* true */
Signature Definition: Give arg 1 an Array of Numbers or a String. Give arg 2 an Array of Numbers or a String. Get a boolean.
Example Usage:
lists.isSuffixOf([1,2],[1,2,3]); /* false */
lists.isSuffixOf("cd","acd"); /* true */
lists.isSuffixOf("d","abcd"); /* true */
Signature Definition: Give arg 1 an Array of Numbers or a String. Give arg 2 an Array of Numbers or a String. Get a boolean.
Example Usage:
lists.isInfixOf([1,2],[1,3,2]); /* false */
lists.isInfixOf("cd","acde"); /* true */
lists.isInfixOf("a","abcd"); /* true */
Signature Definition: Give arg 1 an Array of Numbers or a String. Give arg 2 an Array of Numbers or a String. Get a boolean.
Example Usage:
lists.isSubsequenceOf([1,2],[1,3,2]); /* true */
lists.isSubsequenceOf("abc","123 abc"); /* true */
lists.isSubsequenceOf("Lol","Laugh out loud"); /* true */
Signature Definition: Give arg 1 a Number or a String. Give arg 2 an Array of Numbers or a String. Get a boolean.
Example Usage:
lists.elem(2,[1,3,2]); /* true */
lists.elem("2","123 abc"); /* true */
Signature Definition: Give arg 1 a Number or a String. Give arg 2 an Array of Numbers or a String. Get a boolean.
Example Usage:
lists.notElem(0,[1,3,2]); /* true */
lists.notElem("a","abc"); /* false */
Signature Definition: Give arg 1 a Number or a String. Give arg 2 an Array of an Array containing a Number or a String as the key and a Variable as the value. Get a Variable (value).
Example Usage:
lists.lookup("a",[["a",2],["b",3]]); /* 2 */
lists.lookup("ab",[["ab",2],["b",3]]); /* 2 */
lists.lookup(1,[[1,{a:2}],["b",3]]); /* {a:2} */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Function. Get a Variable.
Example Usage:
lists.find([{a:2}], function(x) { return x.a == 2 }); /* {a:2} */
lists.find([[1,2]], function(x) { return x[0] == 1}); /* [1,2] */
lists.find([1,2,3], function(x) { return x == 0 }); /* "Nothing" */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Function. Get an Array of Variables.
Example Usage:
lists.filter([{a:1},{b:2}], function(obj) { return Object.keys(obj).length > 0 }); /* [{a:1},{b:2}] */
lists.filter("abc", function(char) { return char == "a" }); /* ["a"] */
lists.filter([[1],[1,2]], function(arr) { return arr.length > 1 }); /* [[1,2]] */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Function. Get an Array of two Arrays of Variables.
Example Usage:
lists.partition("Abc", function(char) { return char.startsWith("b") }); /* [["b"],["A","c"]] */
lists.partition([1,2,3,4], function(num) { return num % 2 == 0 }); /* [[2,4],[1,3]] */
lists.partition([{a:1},{b:2,a:2}], function(obj) { return obj.a == 2 }); /* [[{b:2,a:2}],[{a:1}]] */
Signature Definition: Give arg 1 a Number. Give arg 2 an Array of Variables. Get a Variable.
Example Usage:
lists.bang(1,[0,{a:2},1]); /* {a:2} */
lists.bang(0,[[1,2],4]); /* [1,2] */
lists.bang(3,[1,2]); /* -1 */
Signature Definition: Give arg 1 a Number or a String. Give arg 2 an Array of Numbers or a String. Get a Number.
Example Usage:
lists.elemIndex(1,[1,2,1]); /* 0 */
lists.elemIndex("b","abc"); /* 1 */
lists.elemIndex(2,[0,1]); /* -1 */
Signature Definition: Give arg 1 a Number or a String. Give arg 2 an Array of Numbers or a String. Get an Array of Numbers.
Example Usage:
lists.elemIndices(1,[1,2,1]); /* [0,2] */
lists.elemIndices("a","aba"); /* [0,2] */
lists.elemIndices(2,[0,1]); /* [] */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Function. Get a Number.
Example Usage:
lists.findIndex([1,2,3], function(num){ return num % 2 == 0 }); /* 1 */
lists.findIndex([{a:2}], function(obj){ return obj.a == 2 }); /* 0 */
lists.findIndex("ab%c", function(char){ return char == "%" }); /* 2 */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Function. Get an Array of Numbers.
Example Usage:
lists.findIndices([1,2,3,4], function(num) { return num % 2 == 0 }); /* [1,3] */
lists.findIndices([{a:2},{b:3},{a:2}], function(obj) { return obj.a == 2 }); /* [0,2] */
lists.findIndices("AbAbA", function(char) { return char == "A" }); /* [0,2,4] */
Signature Definition: Give arg 1 an Array of an Array of Variables. Get an Array of Variables.
Example Usage:
lists.zip([[1,2],[3,4],[5,6]]); /* [[1,3,5],[2,4,6]] */
lists.zip(lists.zip([[1,2],[3,4],[5,6]])); /* unzip example [[1,2],[3,4],[5,6]] */
lists.zip([[1,'l'],[3,'o'],[5,'l']]); /* [[1,3,5],["l","o","l"]] */
Signature Definition: Give arg 1 an Array of an Array of Variables. Give arg 2 a Function. Get an Array of Variables.
Example Usage:
lists.zipWith([[1,3],[1,2]],lists.sum); /* [2,5] */
function addThenMultiply(arr) {
var res = 0;
lists.each(arr, function(num, i) {
i != 2 ? res += num : res *= num
});
return res;
}
lists.zipWith([[1,3],[1,2],[4,5]],addThenMultiply); /* [8, 25] */
Signature Definition: Give arg 1 a String. Get an Array of Strings.
Example Usage:
lists.lines("l\no\nl"); /* ["l","o","l"] */
lists.lines("Hey\nthere"); /* ["Hey","there"] */
Signature Definition: Give arg 1 a String. Get an Array of Strings.
Example Usage:
lists.words("break it up"); /* ["break","it","up"] */
Signature Definition: Give arg 1 an Array of Strings. Get a String.
Example Usage:
lists.unlines(["break","it","up"]); /* "break\nit\nup\n" */
Signature Definition: Give arg 1 an Array of Strings. Get a String.
Example Usage:
lists.unwords(["break","it","up"]); /* "break it up " */
Signature Definition: Give arg 1 a String or an Array of Numbers and/or Strings. Get an Array of Numbers and/or Strings.
Example Usage:
lists.nub("abbc"); /* ["a","b","c"] */
lists.nub([1,2,"a","b","a",1]); /* [1,2,"a","b"] */
lists.nub([1,2,2,3]); /* [1,2,3] */
Signature Definition: Give arg 1 a Variable. Give arg 2 an Array of Variables or a String. Get an Array of Variables.
Example Usage:
lists.delete('a','bab'); /* ["b","b"] */
lists.delete([1],[[1,2],[1]]); /* [[1,2]] */
lists.delete(1,[2,1,2,1]); /* [2,2,1] */
Signature Definition: Give arg 1 a String or an Array of Numbers and/or Strings. Give arg 2 a String or an Array of Numbers and/or Strings. Get an Array of Variables.
Example Usage:
lists.difference(['acd',1],['acd']); /* [1] */
lists.difference('acd','ab'); /* ["c","d"] */
lists.difference([1,2],[1,2]); /* [] */
Signature Definition: Give arg 1 a String or an Array of Numbers and/or Strings. Give arg 2 a String or an Array of Numbers and/or Strings. Get an Array of Variables or a String.
Example Usage:
lists.union("a","b"); /* "ab" */
lists.union([1,4,'a'],[1,3,'b']); /* [1,4,"a",3,"b"] */
Signature Definition: Give arg 1 a String or an Array of Numbers and/or Strings. Give arg 2 a String or an Array of Numbers and/or Strings. Get an Array of Variables.
Example Usage:
lists.intersect('a','ba'); /* ["a"] */
lists.intersect([1,4,'a'],[1,3,'b']); /* [1] */
Signature Definition: Give arg 1 an Array of Variables or a String. Get an Array of Variables.
Example Usage:
lists.sort('acb'); /* ["a","b","c"] */
lists.sort([[3,4],[1,2]]); /* [[1,2],[3,4]] */
lists.sort([2,1,-1,1]); /* [-1,1,1,2] */
Signature Definition: Give arg 1 a Variable. Give arg 2 an Array of Variables. Get an Array of Variables.
Example Usage:
lists.insert(1,[2,1,2]); /* [1,2,1,2] */
lists.insert(4,[1,3,5,7,9]); /* [1,3,4,5,7,9] */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Function. Get an Array of Variables.
Example Usage:
lists.nubBy([8,7,5,3,2], function(x,y) { return x + y == 10 }); /* [8,7,5] */
lists.nubBy(["query=string, another=string","query=string, hey=man"], function(x,y) {
return x.split(',')[0] == y.split(',')[0]; // define equality on query=string
}); /* ["query=string, another=string"] */
lists.nubBy([{a:1},{a:1, b:2},{b:2}], function(obj1, obj2) {
return obj1.b == obj2.b; // define equality on the b property
}); /* [{a:1},{a:1, b:2}] */
Signature Definition: Give arg 1 a Variable. Give arg 2 an Array of Variables. Give arg 3 a Fucntion. Get an Array of Variables.
Example Usage:
lists.deleteBy(4, [6,8,10,12], function(arg1,y) { return y % arg1 == 0 }); /* [6,10,12] */
lists.deleteBy(2, [{a:2},{b:2,e:2},{c:2}], function(arg1, obj) { return lists.keys(obj).length == arg1 }); /* [{a:2},{c:2}] */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 an Array of Variables or a String. Give arg 3 a Fucntion. Get an Array of Variables.
Example Usage:
lists.deleteFirstsBy([1,2,3,4,3],[3], function(x,y) { return x==y }); /* [1,2,4,3] */
lists.deleteFirstsBy("baba","a", function(x,y) { return x==y }); /* ["b","b","a"] */
Signature Definition: Give arg 1 an Array of Variables. Give arg 2 an Array of Variables. Give arg 3 a Fucntion. Get an Array of Variables.
Example Usage:
lists.unionBy([1,2,3,4],[4,6,9,10], function(x,y) { return x * 3 == y }); /* [1,2,3,4,4,10] */
lists.unionBy(["a","b","c"], ["d","e","f"], function(x,y) {
var same = y == 'd' ? 'a' : y;
return x == same; // "d" is the same as "a"
}); /* ["a","b","c","e","f"] */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 an Array of Variables or a String. Give arg 3 a Fucntion. Get an Array of Variables.
Example Usage:
lists.intersectBy([1,2,3,4],[4,8,12,16,20], function(x,y) { return x * x == y }); /* [2,4] */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Fucntion. Get an Array of an Array of Variables.
Example Usage:
lists.groupBy([1,2,3,4,5,6,7,8,9], function(x,y){ return ((x*y) % 3) == 0}); /* [[1],[2,3],[4],[5,6],[7],[8,9]] */
lists.groupBy([1,2,3,4,5,6,7,8,9], function(x,y){ return x + 1 == y}); /* [[1,2],[3,4],[5,6],[7,8],[9]] */
Signature Definition: Give arg 1 an Array of Variables or a String. Give arg 2 a Function. Get an Array of Variables.
Example Usage:
function oddsFirst(x,y) {
return x % 2 == 1 ? 'LT' : 'GT'
}
lists.sortBy([1,2,3,4,5,6,7], oddsFirst); /* [1,3,5,7,6,4,2] */
Signature Definition: Give arg 1 a Variable. Give arg 2 an Array of Variables or a String. Give arg 3 a Function. Get an Array of Variables.
Example Usage:
function fn(x,y) {
return (x+y) < (x*y) ? 'LT' : 'GT'
}
lists.insertBy(4, [0,1,3,5,7,9], fn); /* [0,1,4,3,5,7,9] */
Signature Definition: Give arg 1 a Variable. Give arg 2 an Array of Variables or a String. Give arg 3 a Function. Get an Array of Variables.
Example Usage:
lists.maximumBy([[1,3],[2]], lists.compare); /* [2] */
function compareLength(x,y) {
return x.length > y.length ? 'GT' : 'LT'
}
lists.maximumBy([[1,3],[2]], compareLength); /* [1,3] */
Signature Definition: Give arg 1 a Variable. Give arg 2 an Array of Variables or a String. Give arg 3 a Function. Get an Array of Variables.
Example Usage:
lists.minimumBy([[1,3],[2]], lists.compare); /* [1,3] */
function compareLength(x,y) {
return x.length > y.length ? 'GT' : 'LT'
}
lists.minimumBy([[1,3],[2],[]], compareLength); /* [] */
Signature Definition: Give arg 1 a String. Give arg 2 a String. Get a an Array of Strings.
Example Usage:
lists.genericExcludeChar("a","abc"); /* ["bc"] */
function myLines(str) { return lists.genericExcludeChar('\u000A', str); };
myLines("abc\nd"); /* ["abc","d"] */
each's arguments are:
Argument 1: Iterable Variable. Argument 2: Function. Argument 3: (optional) Caller (or context).
When called, each applies the Function to each iterable Variable with its optional caller.
The Function's arguments are:
Argument 1: The Variable of the iteration at the specific index. Argument 2: The index at that specific iteration. Argumnet 3: The original iterable Variable.
The original (unchanged) iterable Variable is returned.
Signature Definition: Give arg 1 a Variable. Give arg 2 a Function. (optionally) Give arg 3 a Caller (this, self, etc.). Get a Variable.
Example Usage:
var arr = [];
lists.forEach([1,2,3], function(num, index, array123) {
arr.push(num * 2);
}); /* [1,2,3] */
console.log(arr); /* [2,4,6] */
Signature Definition: Give arg 1 an Object. Get an Array of Strings.
Example Usage:
lists.keys({a:2, b:1}); /* ["a","b"] */
Signature Definition: Give arg 1 a Number. Give arg 2 a Number. Get an Array of Numbers.
Example Usage:
lists.enum(0,5); /* [0,1,2,3,4,5] */
Signature Definition: Give arg 1 N number of Functions. Get a Function.
Example Usage:
var sumThenSqrt = lists.pipe(Math.sqrt, lists.sum);
sumThenSqrt([4,4,4,4]); /* 4 */
Signature Definition: Give arg 1 N number of Functions. Get a Function.
Example Usage:
var sumThenSqrt = lists.sequence(lists.sum, Math.sqrt);
sumThenSqrt([4,4,4,4]); /* 4 */
Signature Definition: Give arg 1 a Function. Give arg 2,..,n a filled or unfilled Variable. Get a Function.
Example Usage:
var myAny = lists.part(lists.any, undefined, function(x) { return x == 1 });
myAny([1,2]); /* true */
myAny([2]); /* false */
Signature Definition: Give arg 1 a Function. Get a Function.
Example Usage:
var flippedAny = lists.flip(lists.any);
flippedAny(function(x){return x == 1}, [1,2]); /* true */
Signature Definition: Give arg 1 a Variable. Give arg 2 a Variable. Get a String.
Example Usage:
lists.compare(1,2); /* "LT" */
lists.compare(2,1); /* "GT" */
lists.compare("b","b"); /* "EQ" */
Signature Definition: Give arg 1 an Array of Numbers. Get an Array of Numbers.
Example Usage:
lists.bubbleSort([3,2,5,1]); /* [1,2,3,5] */
Signature Definition: Give arg 1 an Array of Numbers. Get an Array of Numbers.
Example Usage:
lists.mergeSort([3,2,5,1]); /* [1,2,3,5] */
FAQs
A library of higher-order functions modeled after Haskell's Data.List module
The npm package lists receives a total of 11 weekly downloads. As such, lists popularity was classified as not popular.
We found that lists demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.