• prelude.ls is a functionally oriented utility library.
  • It is powerful and flexible.
  • Almost all of its functions are curried.
  • It is written in, and is the recommended base library for, LiveScript.
[1 to 5] |> map (^2) |> filter even |> fold (+), 0 #=> 20

1.0.0

prelude-browser-min.js View project on GitHub

Install via npm: npm install prelude-ls. MIT License. Changelog.

for updates on prelude.ls releases.

You can report bugs and discuss features on the issue page.

Curried functions

Calling with less than the full number of arguments returns a partially applied function.

fold (+), 0, [1 2 3] #=> 6

sum = fold (+), 0
sum [1 2 3] #=> 6
sum [4 5 6] #=> 15

Installation

Install via npm: npm install prelude-ls.

For use in the browser, just include the prelude-browser-min.js file, and then use prelude = require 'prelude-ls'

If you wish, you may use an underscore to refer to the library, _ = require 'prelude-ls', and then _.map f, xs

However, the preferred way to use the library is to grab what you need from it using object deconstruction: {map, filter, lines} = require 'prelude-ls', and then simply map f, xs


Function Reference

The prelude object has several sub-modules, List, Obj, Str, Func, and Num.

Almost all the functions in these modules are added to the main object which you recieve when you require prelude. However, in some cases, especially where the names conflict, you will have to use the functions through the module, eg. Obj.map conflicts with List.map, so map refers to List.map and you must use Obj.map to use the version which operates on objects.

id
a → a

A function which does nothing: it simply returns its single argument. Useful as a placeholder.

id 5  #=> 5
id {} #=> {}
is-type
String → a → Boolean

Takes a string (type name) and a value, and returns if the value is of that type. Uses LiveScript's typeof! operator.

is-type 'Undefined' void #=> true
is-type 'Boolean' true   #=> true
is-type 'Number' 1       #=> true
is-type 'String' 'hi'    #=> true
is-type 'Object' {}      #=> true
is-type 'Array' []       #=> true
replicate
Number → a → [a]

Takes its second argument, and replicates it n times to create a new list.

replicate 4 3   #=> [3, 3, 3, 3]
replicate 4 'a' #=> ['a', 'a', 'a', 'a']
replicate 0 'a' #=> []
List
each
Function → List → List

Applies a function to each item in the list and returns the original list. Used for side effects.

each (.push \boom), [['a'] ['b'] ['c']]
#=> [['a', 'boom'], ['b', 'boom'], ['c', 'boom']]
map
(a → b) → [a] → [b]

Applies a function to each item in the list, and produces a new list with the results. The length of the result is the same length as the input.

map (* 2), [1 to 5]                 #=> [2, 4, 6, 8, 10]
map (.to-upper-case!), ['ha', 'ma'] #=> ['HA', 'MA']
map (.num), [{num: 3}, {num: 1}]    #=> [3, 1]
compact
[a] → [a]

Returns a new list which contains only the truthy values of the inputted list.

compact [0 1 false true '' 'ha'] #=> [1, true, 'ha']
filter
(a → Boolean) → [a] → [a]

Returns a new list composed of the items which pass the supplied function's test.

filter (< 3), [1 to 5] #=> [1, 2]
filter even, [3, 4, 0] #=> [4, 0]
reject
(a → Boolean) → [a] → [a]

Like filter, but the new list is composed of all the items which fail the function's test.

reject odd, [1 to 5] #=> [2, 4]
partition
(a → Boolean) → [a] → [[a], [a]]

Equivalent to [(filter f, xs), (reject f, xs)], but more efficient, using only one loop.

partition (> 60), [49 58 76 43 88 77 90] #=> [[76 88 77 90],[49 58 43]]
find
(a → Boolean) → [a] → a

Returns the first item in list to pass the function's test.

find odd, [2 4 6 7 8 9 10] #=> 7
tail
[a] → [a]

Everything but the first item of the list.

tail [1 to 5] #=> [2, 3, 4, 5]
last
[a] → a

The last item of the list.

last [1 to 5] #=> 5
initial
[a] → [a]

Everything but the last item of the list.

initial [1 to 5] #=> [1, 2, 3, 4]
empty
List → Boolean

Whether the list is empty.

empty [] #=> true
reverse
[a] → [a]

Returns a new list which is the reverse of the inputted one.

reverse [1 2 3] #=> [3, 2, 1]
unique
[a] → [a]

Returns a new list which contains each value of the inputted list only once.

unique [1 1 1 3 3 6 7 8] #=> [1 3 6 7 8]
fold
alias: foldl
Function → init → List → x

Takes a list of items, and using the binary function supplied, folds them into a single value. Requires an initial value, which will be the starting point, and result in case of an empty list.

fold (+), 0, [1 to 5] #=> 15
product = fold (*), 1
fold1
alias: foldl1
Function → List → x

Like fold, except assumes a non-empty list, and thus doesn't require an initial value.

fold1 (+), [1 to 3] #=> 6
foldr
Function → init → List → x

Like fold, except starting from the right of the list and folding left.

foldr (-), 9, [1 2 3 4] #=> -1
foldr1
Function → List → x

Like foldr, except assumes a non-empty list, and thus doesn't require an initial value.

foldr1 (-), [1 2 3 4 9] #=> -1
unfoldr
Function → seed → List

Unfoldr builds a list from a seed value. It takes a function which either returns null if it is done producing the list, or returns [a, b], a which is prepended to the list, and b is used as the next element in the recursive call.

unfoldr (-> if it == 0 then null else [it, it - 1]), 10
#=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
concat
[[a]] → [a]

Concatenates a list of lists together.

concat [[1] [2 3] [4]]   #=> [1, 2, 3, 4]
concat-map
(a → b) → [[a]] → [b]

Like map, except concatenates the output.

concat-map (-> [1 to it]), [1 2 3] #=> [1, 1, 2, 1, 2, 3]
flatten
List → List

Flattens a list which contains sub lists and elements of arbitrary depth into a list of elements with no sub lists.

flatten [1, [[2], 3], [4, [[5]]]] #=> [1, 2, 3, 4, 5]
difference
([a], [a], ...) → [a]

Returns a new list containing the elements which are present in the first list and not in the others. This function is not curried as it takes a variable number of arguments.

difference [1 2 3] [1]              #=> [1, 2]
difference [1 2 3 4 5] [5 2 10] [9] #=> [1, 3, 4]
intersection
([a], [a], ...) → [a]

Produces a new list containing all the items which are present in all the lists. This function is not curried as it takes a variable number of arguments.

intersection [2 3] [9 8] [12 1] [99]               #=> []
intersection [1 2 3] [101 2 1 10] [2 1] [-1 0 1 2] #=> [1, 2]
intersection [1 2 3] [2 1 3] [3 1 2]               #=> [1, 2, 3]
union
([a], [a], ...) → [a]

Produces a new list containing all the elements of all the inputted lists only once. This function is not curried as it takes a variable number of arguments.

union [1 5 7] [3 5] [] #=> [1 5 7 3]
count-by
(a → b) → [a] → {b: Number}

Takes a list of items, and a function, and produces an object whose keys are the result of applying the function to the inputted list, and whose values are the number of its occurrences.

count-by floor, [4.2, 6.1, 6.4]         #=> {4: 1, 6: 2}
count-by (.length), <[ one two three ]> #=> {3: 2, 5: 1}
group-by
(a → b) → [a] → {b: [a]}

Takes a list of items, and a function, and produces an object whose keys are the result of applying the function to the inputted list, and whose values are a list of all the occurrences.

group-by floor, [4.2, 6.1, 6.4]         #=> {4: [4.2], 6: [6.1, 6.4]}
group-by (.length), <[ one two three ]> #=> {3: ['one', 'two'], 5: ['three']}
and-list
List → Boolean

Returns true if each item in the list is true.

and-list [true, 2 + 2 == 4] #=> true
and-list [true true false ] #=> false
or-list
List → Boolean

Returns true if any item in the list is true.

or-list [false false true false] #=> true
any
(a → Boolean) → [a] → Boolean

Returns true if any of the items in the list are true when applied to the test.

any even, [3, 5, 7, 8, 9] #=> true
all
(a → Boolean) → [a] → Boolean

Returns true if all the items in the list are true when applied to the test.

all (is-type 'String'), <[ ha ma la ]> #=> true
sort
List → List

Sorts a list. Does not modify the input.

sort [3 1 5 2 4 6] #=> [1, 2, 3, 4, 5, 6]
sort-with
(a → a → (-1 | 0 | 1)) → [a] → [a]

Takes a binary function which compares two items and returns either -1, 0, or 1, and sorts the inputted list using that function. The original list is not modified.

f = (x, y) ->
  | x.length > y.length => 1
  | x.length < y.length => -1
  | otherwise           => 0
sort-with f, <[ three one two ]> #=> ['one','two','three']
sort-by
Function → List → List

Sorts a list using the inputted function for making the comparison between the items.

sort-by (.length), arr #=> ['a', 'ha', 'hey', 'there']

table =
  * id: 1
    name: 'george'
  * id: 2
    name: 'mike'
  * id: 3
    name: 'donald'
sort-by (.name), table
#=> [{"id":3,"name":"donald"},{"id":1,"name":"george"},{"id":2,"name":"mike"}]
sum
[Number] → Number

Sums up the values in the list. 0 when the list is empty.

sum [1 to 5] #=> 15
product
[Number] → Number

Gets the product of all the items in the list. 1 when the list is empty.

product [1 2 3] #=> 6
mean
alias: average
[Number] → Number

Gets the mean of the values in the list.

mean [1 to 5] #=> 3
maximum
[a] → a

Takes a list of comparable items, and returns the largest of them.

maximum [4 1 9 3] #=> 9
minimum
[a] → a

Takes a list of comparable items, and returns the smallest of them.

minimum ['c', 'e', 'a', 'd', 'b'] #=> 'a'
scan
alias: scanl
Function → init → List → List

Like fold, except instead of just returning the final value, returns a list composed of the initial value, the intermediate values, and then the final value. Requires an initial value, which is used case of an empty list. The function inputted is binary.

scan (+), 0, [1 to 3] #=> [0, 1, 3, 6]
scan1
alias: scanl1
Function → List → List

Like scan, except assumes non-empty list, and thus doesn't require an initial value.

scan1 (+), [1 to 3] #=> [1, 3, 6]
scanr
Function → init → List → List

Like scan, except from the right.

scanr (+), 0, [1 to 3] #=> [6, 5, 3, 0]
scanr1
Function → List → List

Like scanr, except assumes non-empty list, and thus doesn't require an initial value.

scanr1 (+), [1 to 3] #=> [6, 5, 3]
slice
Number → Number → [a] → [a]

Returns a slice of a list.

slice 2 4 [1 2 3 4 5] #=> [3, 4]
take
Number → [a] → [a]

Returns the first n items in the list.

take 2 [1 to 5] #=> [1, 2]
drop
Number → [a] → [a]

Returns the result of dropping the first n items of the list.

drop 2 [1 to 5] #=> [3, 4, 5]
split-at
Number → [a] → [[a], [a]]

Equivalent to [(take n, xs), (drop n, xs)]

split-at 2 [1 to 5] #=> [[1, 2], [3, 4, 5]]
take-while
(a → Boolean) → [a] → [a]

Takes the first items of the list which pass the test.

take-while odd, [1 3 5 4 8 7 9] #=> [1, 3, 5]
drop-while
(a → Boolean) → [a] → [a]

Drops the first items of the list which pass the test.

drop-while even, [2 4 5 6] #=> [5, 6]
span
(a → Boolean) → [a] → [[a], [a]]

Equivalent to [(take-while f, xs), (drop-while f, xs)]

span even, [2 4 5 6] #=> [[2, 4], [5, 6]]
break-list
(a → Boolean) → [a] → [[a], [a]]

Equivalent to span (not) << f, xs

break-list (== 3), [1 to 5] #=> [[1, 2], [3, 4, 5]]
zip
[a] → [b] → [[a, b]]

Zips together its two arguments into a list of lists.

zip [1 2 3] [4 5 6] #=> [[1, 4], [2, 5], [3, 6]]
zip-with
(a → b → c) → [a] → [b] → [c]

Zips together its two arguments using a function into a list of resulting values.

zip-with (+) [1 2 3] [4 5 6] #=> [5, 7, 9]
zip-all
([a], [b], [c], ...) → [[a, b, c, ...]]

Zips together its arguments into a list of lists. This function is not curried as it takes a variable number of arguments.

zip-all [1 2 3] [4 5 6] [7 8 9] #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
zip-all-with
((a, b, c, ... → d), [a], [b], [c], ...) → [d]

As zip-all, but applies the supplied function to the lists and creates a list of the results. The supplied function must take in as many arguments as there are lists being inputed. This function is not curried as it takes a variable number of arguments.

zip-all-with (-> &0 + &1 + &2), [1 2 3], [3 2 1] [1 1 1] #=> [5, 5, 5]
Obj
keys
{a: b} → [a]

Returns a list of the keys of the object.

keys a: 2, b: 3, c: 9 #=> ['a', 'b', 'c']
values
{a: b} → [b]

Returns a list of the values of the object.

values a: 2, b: 3, c: 9 #=> [2, 3, 9]
pairs-to-obj
[[a, b]] → {a: b}

Takes a list of pairs and turns them into an object.

pairs-to-obj [['a' 'b'] ['c' 'd'] ['e' 1]] #=> {a: 'b', c: 'd', e: 1}
obj-to-pairs
{a: b} → [[a, b]]

Takes an object and returns a list of pairs.

obj-to-pairs {a: 'b', c: 'd', e: 1} #=> [['a', 'b'], ['c', 'd'], ['e', 1]]
lists-to-obj
[a] → [b] → {a: b}

Takes two lists and zips them up into an object.

lists-to-obj <[ a b c ]> [1 2 3] #=> {a: 1, b: 2, c: 3}
obj-to-lists
{a: b} → [[a], [b]]

Takes an object and returns a list with two lists, one of its keys, one with its values.

obj-to-lists {a: 1, b: 2, c: 3} #=> [['a', 'b', 'c'], [1, 2, 3]]
Obj.empty
Object → Boolean

Whether the object is empty.

empty {} #=> true
Obj.each
Function → Object → Object

Applies a function to each value in the object, and returns the original object. Used for side effects.

count = 4
each (-> count += it), {a: 1, b: 2, c: 3}
count #=> 10
Obj.map
(b → c) → {a: b} → {a: c}

Applies a function to each value of the object, and produces a new object with the same keys and the new values. The size of the result is the same size as the input.

map (+ 2), {a: 2, b: 3, c: 4} #=> {a: 4, b: 5, c: 6}
Obj.compact
{a: b} → {a: b}

Returns a new object which contains only the truthy values of the inputted object.

compact {a: 0, b: 1, c: false, d: '', e: 'ha'} #=> {b: 1, e: 'ha'}
Obj.filter
(b → Boolean) → {a: b} → {a: b}

Returns a new object composed of the values which pass the supplied function's test.

filter even, {a: 3, b: 4, c: 0} #=> {b: 4, c: 0}
Obj.reject
(b → Boolean) → {a: b} → {a: b}

Like filter, but the new object is composed of all the values which fail the function's test.

reject (== 2), {a:1, b:2} #=> {a: 1}
Obj.partition
(b → Boolean) → {a: b} → [{a: b}, {a: b}]

Equivalent to [(Obj.filter f, xs), (Obj.reject f, xs)], but more efficient, only using one loop.

partition (== 2), {a:1, b:2, c:3} #=> [{b: 2}, {a: 1, c: 3}]
Obj.find
(b → Boolean) → {a: b} → b

Returns the first value to pass the test.

find even, {a:1, b:2, c:3, d:4} #=> 2
Str
split
String → String → [String]

Splits a string on a separator into a list of strings.

split '|' '1|2|3' #=> ['1', '2', '3']
join
String → [String] → String

Joins a list with the specified separator.

join '|' [1 til 4] #=> '1|2|3'
lines
String → [String]

Splits a string at newlines into a list.

lines '''one
         two
         three'''
#=> ['one', 'two', 'three']
unlines
[String] → String

Joins a list of strings into a single string using newlines.

unlines [\one \two \three]
#=> 'one
#    two
#    three'
words
String → [String]

Splits a string at spaces (one or more), returning a list of strings.

words 'hello, what is that?'
#=> ['hello,', 'what', 'is', 'that?']
unwords
[String] → String

Joins a list of strings into a single string using spaces.

unwords ['one' 'two' 'three'] #=> 'one two three'
chars
String → [String]

Splits a string at every character, returning a list of one character strings.

chars 'hello' #=> ['h', 'e', 'l', 'l', 'o']
unchars
[String] → String

Joins a list of strings into a single string using no separator.

unchars ['t' 'h' 'e' 'r' 'e'] #=> 'there'
unchars ['ma', 'ma']          #=> 'mama'
Str.empty
String → Boolean

Whether the string is empty.

empty '' #=> true
Str.reverse
String → String

Reverses a string.

reverse 'goat'   #=> 'taog'
Str.repeat
Number → String → String

Takes its second argument, and repeats it n times to create a new, single, string.

repeat 4 'a'  #=> 'aaaa'
repeat 2 'ha' #=> 'haha'
Str.slice
Number → List → List

Returns a slice of the inputted string.

slice 2 4 'hello' #=> 'll'
Str.take
Number → String → String

Returns the first n items in string.

take 4 'hello'   #=> 'hell'
Str.drop
Number → String → String

Returns the result of dropping the first n items of the string.

drop 1 'goat'   #=> 'oat'
Str.split-at
Number → String → [String, String]

Equivalent to [(Str.take n, xs), (Str.drop n, xs)]

split-at 4 'hello' #=> ['hell', 'o']
Str.take-while
(String → Boolean) → String → String

Takes the first items of the string which pass the test.

take-while (in [\a to \d]), 'cabdek' #=> 'cabd'
Str.drop-while
(String → Boolean) → String → String

Drops the first items of the string which pass the test.

drop-while (is \m), 'mmmmmhmm' #=> 'hmm'
Str.span
(String → Boolean) → String → [String, String]

Equivalent to [(take-while f, xs), (drop-while f, xs)]

span (is \m), 'mmmmmhmm' #=> ['mmmmm', 'hmm']
Str.break-list
(String → Boolean) → String → [String, String]

Equivalent to span (not) << f, xs

break-str (is \h), 'mmmmmhmm' #=> ['mmmmm', 'hmm']
Func
apply
(a → b → c → ... → d) → [a, b, c, ...] → d

Returns the application of the supplied list as arguments to the supplied function.

apply (+), [2 3] #=> 5
curry
Function → Function

Returns a curried version of the supplied function. Useful for currying functions from non-LiveScript libraries.

add = (x, y) -> x + y
add-curried = curry add
add-four = add-curried 4
add-four 2 #=> 6
flip
(a → b → c) → (b → a → c)

Returns a function with the arguments flipped.

inverted-power = flip (^)
inverted-power 2 3 #=> 9
fix
(Function → Function) → Function

Fix-point function for anonymous recursion, implemented with the Y combinator.

(fix (fib) -> (n) ->
   | n <= 1      => 1
   | otherwise   => fib(n-1) + fib(n-2))(9) #=> 55
Num
max
Comparable → Comparable → Value

Takes two arguments which can be compared using >, returns the larger one.

max 3 1     #=> 3
max 'a' 'c' #=> 'c'
min
Comparable → Comparable → Value

Takes two arguments which can be compared using >, returns the smaller one.

min 3 1     #=> 1
min 'a' 'c' #=> 'a'
negate
Number → Number

The negation of the inputted number.

negate 3  #=> -3
negate -2 #=>  2
abs
Number → Number

Takes a number and returns its absolute value.

abs -2 #=>  2
abs 2  #=> 2
signum
Number → (-1 | 0 | 1)

Takes a number and returns either -1, 0, or 1 depending on the sign of the number.

signum -5 #=> -1
signum  0 #=>  0
signum  9 #=>  1
quot
Number → Number → Number

Division truncated toward 0.

quot -20 3 #=> -6
rem
Number → Number → Number

Remainder, like the % operator.

rem -20 3 #=> -2
div
Number → Number → Number

Division truncated down toward negative infinity.

div -20 3 #=> -7
mod
Number → Number → Number

Remainder, like the %% operator in LiveScript.

mod -20 3 #=> 1
recip
Number → Number

One over the number, ie 1 / x

recip 4 #=> 0.25
pi
Number

π

pi #=> 3.141592653589793
tau
Number

τ (2π)

tau #=> 6.283185307179586
exp
Number → Number

Returns e to the argument.

exp 1 #=> 2.718281828459045
sqrt
Number → Number

Square root.

sqrt 4 #=> 2
ln

Natural log.

Number → Number
ln 10 #=> 2.302585092994046
pow
Number → Number → Number

Power. Equivalent to x ^ y or x ** y in LiveScript.

pow -2 2 #=> 4
sin
Number → Number
sin pi/2 #=> 1
cos
Number → Number
cos pi #=> -1
tan
Number → Number
tan pi/4 #=> 1
asin
Number → Number
asin 0 #=> 0
acos
Number → Number
acos 1 #=> 0
atan
Number → Number
atan 0 #=> 0
atan2
Number → Number → Number
atan2 1 0 #=> 1.5707963267948966
truncate
Number → Number

Number truncated toward 0.

truncate -1.5 #=> -1
truncate  1.5 #=>  1
round
Number → Number

Number rounded to nearest whole number.

round 0.6 #=> 1
round 0.5 #=> 1
round 0.4 #=> 0
ceiling
Number → Number

Number rounded up.

ceiling 0.1 #=> 1
floor
Number → Number

Number rounded down.

floor 0.9 #=> 0
is-it-NaN
Number → Boolean

Is it NaN (not a number)? More accurate than the native isNaN function.

is-it-NaN sqrt -1 #=> true
even
Number → Boolean

Is the number even?

even 4 #=> true
even 0 #=> true
odd
Number → Boolean

Is the number odd?

odd 3 #=> true
gcd
Number → Number → Number

Greatest common denominator.

gcd 12 18 #=> 6
lcm
Number → Number → Number

Least common multiple.

lcm 12 18 #=> 36