0.0.4 • Published 11 years ago

kurry-arrays v0.0.4

Weekly downloads
1
License
-
Repository
github
Last release
11 years ago

Kurry-Arrays

Kurry-Arrays is a JavaScript library providing a small collection of functions for the purpose of creating, transforming, and processing regular JavaScript arrays.

This library explicitly depends on Kurry.js.

All functions are wrapped with Kurry.autopoly.

Documentation (unfinished)

Arrays.all : (a Bool) a Bool

Returns truthy if all elements in an array satisfy some predicate. Falsy otherwise.

Arrays.all(function (v) { return v > 4; }, [])
Arrays.all(function (v) { return v > 4; }, [5, 7, 9])
//= true

Arrays.all(function (v) { return v > 4; }, [6, 1, 5])
//= false

Arrays.append : a a a

Appends two arrays together:

Arrays.append([1, 2, 3], [4, 5, 6])
//= [1, 2, 3, 4, 5, 6]

Arrays.append([1, 2])([3])
//= [1, 2, 3]

Arrays.bind : a (a b) b

Monadic bind:

var sqrt = function (v) {
    var x = Math.abs(v);
    var r = Math.sqrt(x);
    return (
        ( v === 0 ? [0]
        : v > 0 ? [r, -r]
        : [] )
    );
};

Arrays.bind([1, 4, 9, 0, -42], sqrt)
//= [1, -1, 2, -2, 3, -3, 0]

Arrays.bindOn : (a b) a b

Flipped version of Array.bind:

var sqrt = function (v) {
    var x = Math.abs(v);
    var r = Math.sqrt(x);
    return (
        ( v === 0 ? [0]
        : v > 0 ? [r, -r]
        : [] )
    );
};

Arrays.bind(sqrt, [1, 4, 9, 0, -42])
//= [1, -1, 2, -2, 3, -3, 0]

Arrays.concat : [a] a

Appends an array of arrays together:

Arrays.concat([[1, 2], [3, 4], [[5], [6, 7]]])
//= [1, 2, 3, 4, [5], [6, 7]]

Arrays.drop : Num a a

Returns an array that is the array passed in with a number of elements removed from the beginning.

Arrays.drop(2, [1, 2, 3, 4])
//= [3, 4]

Arrays.foldl : (b a b) b a b

Given a binary function bin : b -> a -> b and an initial accumulator value acc : b, from the left of the array fold together each element with the current accumulator, the result is the new accumulator.

var add = function (a, b) { return a + b }
var sum = Arrays.foldl(add, 0)
sum([1, 2, 3, 4, 5])
//= foldl(+, 0, [1,2,3,4,5])
//= foldl(+, (0+1), [2,3,4,5])
//= foldl(+, (0+1+2), [3,4,5])
//= foldl(+, (0+1+2+3), [4,5])
//= foldl(+, (0+1+2+3+4), [5])
//= foldl(+, (0+1+2+3+4+5), [])
//= (0+1+2+3+4+5)
//= 15

Arrays.foldl1

Same as foldl, except the first value of the array is taken to be the initial accumulator value.

var multiply = function (a, b) { return a * b }
var product = Arrays.foldl1(multiply)

product([5, 4, 3])
//=foldl1(*, [5, 4, 3])
//=foldl(*, 5, [4, 3])
//=foldl(*, (5*4), [3])
//=foldl(*, (5*4*3), [])
//=(5*4*3)
//=60

product([])
//=foldl1(*, [])
//=foldl(*, undefined, [])
//=undefined

Arrays.foldlBind

Arrays.foldr : (b a b) b a b

Arrays.foldr1

Arrays.foldrBind

Arrays.lift : (a b) a b

Apply a function to every element in the array. The result is an array of the output of each call:

Arrays.lift(function (v) { return v * 10 }, [0, 1, 2, 3, 4])
//= [0, 10, 20, 30, 40]

Arrays.mapIndexed : ((a, Num) b) a b

Apply a function to every element + index pair in the array. The result is an array of the output of each call:

Arrays.mapIndexed(function (v, i) { return v * i }, [0, 1, 2, 3, 4])
//= [0, 1, 4, 9, 16]

Arrays.range : Num Num Num

Computes a range given a start and an end:

Arrays.range(3, 8)
//= [3, 4, 5, 6, 7]

Arrays.range(10, 1)
Arrays.range(0, Infinity)
Arrays.range(-Infinity, 0)
//= []

Arrays.slice : Num Num a a

Given a starting index s, an ending index e, and an array a, drop s and take e - s:

Arrays.slice(2, 5, [1, 2, 3, 4, 5, 6, 7])
//= [3, 4, 5]

Arrays.some : (a Bool) a Bool

Returns truthy if at least one element in an array satisfies some predicate. Falsy otherwise.

Arrays.some(function (v) { return v > 4; }, [5, 7, 9])
Arrays.some(function (v) { return v > 4; }, [6, 1, 5])
//= true

Arrays.some(function (v) { return v > 4; }, [])
Arrays.some(function (v) { return v > 4; }, [2, 1, 3])
//= false

Arrays.stepRange : Num Num Num Num

Computes a range given an interval step, a start and an end:

Arrays.stepRange(0.5, 3, 8)
//= [3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5]

Arrays.stepRange(0, 1, 10)
Arrays.stepRange(2, 10, 5)
Arrays.stepRange(1, 0, Infinity)
Arrays.stepRange(0, -Infinity, 0)
Arrays.stepRange(-1, 0, 5)
//= []

Arrays.tail : a a

Given an array, drop the head:

Arrays.tail([5, 4, 3, 2, 1])
//= [4, 3, 2, 1]

Arrays.tail(Arrays.tail([3, 2, 1]))
//= [1]

Arrays.take : Num a a

Given a number n and an array xs, returns a new array that is the first n elements in xs:

Arrays.take(3, [5, 4, 3, 2, 1])
//= [5, 4, 3]

Arrays.unit : a a

Monadic unit (return):

Arrays.unit(5)
//= [5]
0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago