Utility library for deno with a FP bent.
You should be able to pull all functions straight from mod like import { take } from "https://deno.land/x/pine/mod.ts"
You can get the curried version of any function that takes more than one argument by importing it from mod.curried.ts instead.
I highly recommend looking at the test file for a function to see how to use it.
ex: import { take } from "https://deno.land/x/pine/mod.curried.ts"
Most of the api should be pretty stable, but for now I would pin a version as I am planning to rewrite some of the function implementations, and add things like automatic currying.
Returns the numbers added together
add(2, 3) == 5
Returns whether all items in list
return true when applied to the function
all(x => x > 1, [2, 3, 4]) == true
Returns whether any item in the list
returns true when applied to the function
any(x => x > 1, [0, 1, 2]) == true
Chunks the list by count
excess are thrown away
chunkEvery(2, [1, 2, 3, 4, 5]) == [[1, 2], [3, 4]]
Returns a number
bound between min
and max
clamp(0, 10, 20) == 10
Returns a function that is the same as if you took the output from first
and then applied it to second
const inc = x => x + 1
const double = x => x * 2
const fun = comp(inc, double)
fun(2) === 6
Returns whether an item
is present in the list
contains(1, [1, 2, 3]) == true
Retuns a curried version of the function allowing you to partial apply it !! warning: curried functions lose their type
const curried = curry(add)
curried(1)(2) === 3
Returns one less than n
dec(3) == 2
Returns an array of tuples having the signature [index, item]
enumerate(['a', 'b', 'c']) == [[0, 'a'], [1, 'b'], [2, 'c']]
Returns wether two arguments are equal
eq(2, 2) === true
Returns false
f() == false
Returns all items in the list where T
satisfies pred
filter(isEven, [1, 2, 3, 4]) == [2, 4]
Returns all items in the array except the last one
first([1, 2, 3]) == [1, 2]
Takes a function with two parameters
Returns the function with the arguments reversed
Takes a reducer, an initial accumulator value, and a list of items. Applies the function on the accumulator, and each item of the list as it iterates through and returns the final accumulator.
fold(add, 0, [1, 2, 3]) == 6
Returns a frequency table created from the list, in a Map data structure
frequencies([1, 1, 2, 2, 2], Map{ 1: 2, 2: 3})
Returns wether y
is greater than x
gt(1, 2) === true
Returns wether the object has the given key
hasKey("x", {x: 1}) == true
Returns the first item in a list
head([1, 2, 3]) == 1
Returns the item passed in
identity(5) == 5
Returns one higher than n
inc(1) == 2
Runs a function for every item in the list supplying the index, and the item as arguments
indexMap((i, item) => i + item, [5, 5, 5]) == [5, 6, 7]
Returns wether the given list
is empty
isEmpty([]) == true
Returns wether the given number is even
isEven(2) == true
Returns wether the given number is odd
isOdd(3) == true
Returns the last item in a list
last([1, 3, 2]) == 2
Returns the length of the list
length([1, 1, 1]) == 3
Returns wether y
is less than x
lt(4, 1) === true
Applies a function to each item in a list, returning a new list of the results of each call
Returns the value for the key key
on the map. Throws an error if the key is not found
Returns whether a map has the given key
Returns a new map based off the one passed in with the key key
set to value
Returns the larger of two numbers
max(2, 4) == 4
Returns then larger between x
and y
when applied to fn
const first = { x: 4 };
const second = { x: 5};
maxBy(o => o.x, first, second) == second
Returns the largest item in the list
maximum([1, 2, 3]) = 3
Returns the smaller of two numbers
min(2, 4) == 2
Returns the smaller of originals from applying the function to both items
const first = { x: 4 }
const second = { x: 5}
minBy(o => o.x, first, second) == first
Returns the smallest item in the list
minimum([1, 2, 3]) == 1
Returns the opposite of the applied boolean
not(true) == false
Pipes functions together chaining the output of each function into the next !! warning: currently pipe can't guarantee function typing at compile time
const addOne = (x) => x + 1
const double = (x) => x * 2
const addTwoThenDouble = pipe(addOne, addOne, double)
addTwoThenDouble(1) === 6
Returns the prop
off of the object
prop('x', {x: 1}) === 1
Returns an array of numbers from start
to stop
inclusive
range(1, 3) == [1, 2, 3]
Shorthand for call to fold
where the initial value is the first item of the list, and iterates through the rest of the list
reduce(add, [1, 2, 3]) == 6
Returns an array of count
length with item
repeated
`repeat(3, "apple") == ["apple", "apple", "apple"]
Returns the list
in the reversed order
reverse([1, 2, 3]) == [3, 2, 1]
Iterates over the list
in the same way as fold
, but saves all steps along the way.
scan(add, 0, [1, 2, 3]) == [0, 1, 3, 6]
Returns the sum of all numbers in list
sum([1, 2, 3]) === 6
Returns the sum of applying fn
to all items in list
const first = { x: 1};
const second = { x: 2};
const third = { x: 3 };
const list = [first, second, third];
sumBy(o => o.x, list) === 6
Returns true
t() == true
Returns all items after the first in a list
tail([1, 2, 3]) == [2, 3]
Returns the first count
items from a list
take(2, [1, 2, 3, 4, 5]) == [1, 2]
Returns the last count
items from a list
takeLast(2, [1, 2, 3, 4, 5]) == [4, 5]
Returns elements of the list until it first gets a false value from the predicate. Does not return the value that returned false
takeWhile(x => x < 2, [1, 1, 1, 3]) == [1, 1, 1]
Zips two arrays together into an array of tuples. Extra items from the longer will be ignored
zip([1, 2, 3, 4], ["a", "b", "c"]) == [[1, "a"], [2, "b"], [3, "c"]]
- pipe/compose
- more tests
- automatic currying
- Option/Result types
- more tests
- more tests
- better way to handle type of functions
Want to send a pr? I'm pretty open. If you're adding a new function, it needs to do three things:
- be in a new file
- have a matching test file
- export from mod