Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/belsrc/fjp
WIP, ye be warned - Function Js Playground: A place for me to play with functional coding in Javascript
https://github.com/belsrc/fjp
functional functional-programming javascript
Last synced: about 2 months ago
JSON representation
WIP, ye be warned - Function Js Playground: A place for me to play with functional coding in Javascript
- Host: GitHub
- URL: https://github.com/belsrc/fjp
- Owner: belsrc
- License: other
- Created: 2018-01-08T00:23:00.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-24T03:18:22.000Z (almost 2 years ago)
- Last Synced: 2024-11-16T11:47:12.908Z (about 2 months ago)
- Topics: functional, functional-programming, javascript
- Language: JavaScript
- Homepage:
- Size: 516 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 26
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Functional Javascript Playground
[![Maintainability](https://img.shields.io/codeclimate/maintainability/belsrc/fjp.svg?style=flat-square)](https://codeclimate.com/github/belsrc/fjp/maintainability)
[![Build Status](https://img.shields.io/travis/belsrc/fjp/develop.svg?style=flat-square)](https://travis-ci.org/belsrc/fjp)
[![Code Coverage](https://img.shields.io/codecov/c/github/belsrc/fjp/develop.svg?style=flat-square)](https://codecov.io/gh/belsrc/fjp/branch/develop)
[![NSP Status](https://nodesecurity.io/orgs/brykizer/projects/ed9e95c2-3569-4408-bd71-68b49e2e5014/badge)](https://nodesecurity.io/orgs/brykizer/projects/ed9e95c2-3569-4408-bd71-68b49e2e5014)
[![Last Commit](https://img.shields.io/github/last-commit/belsrc/fjp/develop.svg?style=flat-square)](https://github.com/belsrc/fjp/commits/develop)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/belsrc/fjp/pulls)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**
```js
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**
```js
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**
```js
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**
```js
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**
```js
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**
```js
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**
```js
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**
```js
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**
```js
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**
```js
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**
```js
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**
```js
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**
```js
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**
```js
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**
```js
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**
```js
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**
```js
isFunction(() => {}) // true
isFunction([1, 2, 3]) // false
```
## isNumber()
Determines if the given value is a number.
**Kind**: global function
**Signature**: isNumber :: a -> Boolean
**Example**
```js
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**
```js
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**
```js
isString('foo bar') // true
isString({ a: 'b' }) // false
```
## not()
Negates the given boolean-like value.
**Kind**: global function
**Signature**: not :: Boolean -> Boolean
**Example**
```js
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**
```js
tap(console.log, 'foobar') // foobar
```
## License
FJP is licensed under the MIT license.
Copyright © 2018 Bryan Kizer