November's Paddleball Marathon

    fjp

    0.2.4 • Public • Published

    Functional Javascript Playground

    Maintainability Build Status Code Coverage NSP Status Last Commit PRs Welcome

    Functional Programming was always a weird "why would you use this" way of coding as I've primarily been OOP. I set out to change my view on that and learn more about it and when to use it. These are some of the functions I collected/made along the way. They are all ES6/ESM, the parameter order for most lends itself to currying + composing. Still a little iffy on the Hindley-milner signatures so they may be slightly off.

    This is very much a WIP

    Functions

    associate()

    Sets the given property and value on the object. Returning a new object.

    average()

    Averages the given array values

    A()

    Calls the given function with the given value.

    Fork()

    Takes a joiner func, and two other funcs and a value. The value is given to both funcs and the results of each of these is given to the joiner func.

    I()

    Returns the given value.

    K()

    Takes two values and returns the given first.

    OR()

    Given two functions that take the same value, returns the first if the result is truthy, otherwise, the second.

    T()

    Calls the given function with the given value. (Reverse order of apply)

    compact()

    Removes falsey values from an array.

    compose()

    Performs right-to-left function composition.

    concat()

    Concatenates two String|Arrays together. Returns empty array if value arent of the same type or not String|Array.

    concatN()

    Concatenates N Arrays together.

    curry()

    Wraps the given function, if the number of provided args is sufficient, call the passed function fn. Otherwise, return a wrapped function fn that expects the rest of the arguments. If you want to curry a function that accepts a variable number of arguments (a variadic function), you can optionally pass the number of arguments to the second parameter arity.

    difference()

    Returns the difference between two arrays.

    distinct()

    Returns all of the distinct values of an array.

    distinctN()

    Returns all of the distinct values of the given arrays.

    each()

    Applies the given func to each element in the array.

    every()

    Determines if all element in an array satisfy the given test function

    filter()

    Filters the array using the given function.

    find()

    Finds the first element that satisfies the given test func.

    flatten()

    Flattens single nested array.

    isArray()

    Determines if the given value is an array.

    isFunction()

    Determines if the given value is a function.

    isNumber()

    Determines if the given value is a number.

    isObject()

    Determines if the given value is an object.

    isString()

    Determines if the given value is a string.

    not()

    Negates the given boolean-like value.

    tap()

    Calls the given function with the given value and returns the value.

    associate()

    Sets the given property and value on the object. Returning a new object.

    Kind: global function
    Signature: associate :: String k -> {} -> v -> {k: v}
    Example

    const obj = associate('c', { a: b }, d)  // { a: b, c: d }

    average()

    Averages the given array values

    Kind: global function
    Signature: average :: [Number] -> Number
    Example

    average([ 1, 2, 3 ]); // 2
    average(1, 2, 3); // 2
    average(); // 0

    A()

    Calls the given function with the given value.

    Kind: global function
    Signature: A :: (a -> b) -> a -> b
    Aka: apply

    Fork()

    Takes a joiner func, and two other funcs and a value. The value is given to both funcs and the results of each of these is given to the joiner func.

    Kind: global function
    Signature: Fork :: (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d
    Aka: join

    I()

    Returns the given value.

    Kind: global function
    Signature: I :: a -> a
    Aka: identity

    K()

    Takes two values and returns the given first.

    Kind: global function
    Signature: K :: a -> b -> a
    Aka: constant

    OR()

    Given two functions that take the same value, returns the first if the result is truthy, otherwise, the second.

    Kind: global function
    Signature: OR :: (a -> b) -> (a -> b) -> b
    Aka: alternation

    T()

    Calls the given function with the given value. (Reverse order of apply)

    Kind: global function
    Signature: T :: a -> (a -> b) -> b
    Aka: thrush, applyTo

    compact()

    Removes falsey values from an array.

    Kind: global function
    Signature: compact :: [a] -> [a]
    Example

    compact([ 0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34 ]); // [ 1, 2, 3, 'a', 's', 34 ]
    compact(null) // []

    compose()

    Performs right-to-left function composition.

    Kind: global function
    Signature: compose :: [(m -> n), ..., (b -> c), (a -> b)] -> a -> n
    Example

    const addOne = x => x + 1;
    const timeTen = x => x * 10;
    const addOneTimeTen = compose(timeTen, addOne);
    const result = addOneTimeTen(9); // 100

    concat()

    Concatenates two String|Arrays together. Returns empty array if value arent of the same type or not String|Array.

    Kind: global function
    Signature: concat :: a -> b -> c
    Example

    concat('foo', 'bar') // foobar
    concat([1, 2], [3, 4]) // [1, 2, 3, 4]

    concatN()

    Concatenates N Arrays together.

    Kind: global function
    Signature: concat :: [a] -> [b] -> ...[n] -> [m]
    Example

    concatN([1, 2], [3, 4], [5, 6]) // [1, 2, 3, 4, 5, 6]

    curry()

    Wraps the given function, if the number of provided args is sufficient, call the passed function fn. Otherwise, return a wrapped function fn that expects the rest of the arguments. If you want to curry a function that accepts a variable number of arguments (a variadic function), you can optionally pass the number of arguments to the second parameter arity.

    Kind: global function
    Signature: curry :: ((a, b, ..., n) -> m) -> a -> b -> ...n -> m
    Example

    const add = curry((x, y) => x + y);
    const addFiveTo = add(5);
    addFiveTo(10); // 15

    difference()

    Returns the difference between two arrays.

    Kind: global function
    Signature: difference :: [a] -> [b] -> [c]
    Example

    difference([ 1, 2, 3 ], [ 1, 2, 4 ]) // [3]
    difference([], [ 1, 2, 4 ]) // [ 1, 2, 4 ]
    difference([ 1, 2, 3 ], []) // [ 1, 2, 3 ]
    difference([ 1, 2, 3 ], null) // [ 1, 2, 3 ]

    distinct()

    Returns all of the distinct values of an array.

    Kind: global function
    Signature: distinct :: [a] -> [b]
    Example

    distinct([ 1, 2, 2, 3, 4, 4, 5 ]) // [ 1, 2, 3, 4, 5 ]

    distinctN()

    Returns all of the distinct values of the given arrays.

    Kind: global function
    Signature: distinctN :: [a] -> [b] -> ...[n] -> [m]
    Example

    distinctN([ 1, 2 ], [ 2, 3, 4 ], [ 4, 5 ]) // [ 1, 2, 3, 4, 5 ]

    each()

    Applies the given func to each element in the array.

    Kind: global function
    Signature: each :: (a -> b) -> [c] -> undefined
    Example

    difference(log, [1, 2, 3])

    every()

    Determines if all element in an array satisfy the given test function

    Kind: global function
    Signature: every :: (a -> Bool) -> [a] -> Bool
    Aka: all
    Example

    every(Boolean, [1, 2, 3, 4]) // true
    every(Boolean, [1, 2, null, 4]) // false

    filter()

    Filters the array using the given function.

    Kind: global function
    Signature: filter :: (a -> Boolean) -> [a] -> [a]
    Example

    filter(x => x > 5, [1, 2, 3, 5, 6, 7]) // [6, 7]

    find()

    Finds the first element that satisfies the given test func.

    Kind: global function
    Signature: find :: (a -> Boolean) -> [a] -> a
    Example

    find(x => x.score === 5, [{score: 1}, {score: 2}, {score: 5}, {score: 6}, {score: 7}]) // {score: 5}

    flatten()

    Flattens single nested array.

    Kind: global function
    Signature: flatten :: [a] -> [a]
    Example

    flatten([[ 1, 2 ], [ 3, 4 ]];); // [ 1, 2, 3, 4 ]
    flatten(null) // []

    isArray()

    Determines if the given value is an array.

    Kind: global function
    Signature: isArray :: a -> Boolean
    Example

    isArray([1, 2, 3])  // true
    isArray({ a: 'b' })  // false

    isFunction()

    Determines if the given value is a function.

    Kind: global function
    Signature: isFunction :: a -> Boolean
    Example

    isFunction(() => {})  // true
    isFunction([1, 2, 3])  // false

    isNumber()

    Determines if the given value is a number.

    Kind: global function
    Signature: isNumber :: a -> Boolean
    Example

    isNumber(42)  // true
    isNumber(8e5)  // true
    isNumber(0x2F)  // true
    isNumber('foo bar')  // false

    isObject()

    Determines if the given value is an object.

    Kind: global function
    Signature: isObject :: a -> Boolean
    Example

    isObject({ a: 'b' })  // true
    isObject([1, 2, 3])  // false

    isString()

    Determines if the given value is a string.

    Kind: global function
    Signature: isString :: a -> Boolean
    Example

    isString('foo bar')  // true
    isString({ a: 'b' })  // false

    not()

    Negates the given boolean-like value.

    Kind: global function
    Signature: not :: Boolean -> Boolean
    Example

    not(true); // false
    not(false); // true
    not(2); // false

    tap()

    Calls the given function with the given value and returns the value.

    Kind: global function
    Signature: tap :: (a -> b) -> a -> a
    Example

    tap(console.log, 'foobar') // foobar

    License

    FJP is licensed under the MIT license.

    Copyright © 2018 Bryan Kizer

    Keywords

    none

    Install

    npm i fjp

    DownloadsWeekly Downloads

    1,243

    Version

    0.2.4

    License

    MIT

    Unpacked Size

    120 kB

    Total Files

    144

    Last publish

    Collaborators

    • belsrc