{"id":21630518,"url":"https://github.com/belsrc/fjp","last_synced_at":"2025-04-11T14:07:28.948Z","repository":{"id":28183820,"uuid":"116609901","full_name":"belsrc/fjp","owner":"belsrc","description":"WIP, ye be warned - Function Js Playground:  A place for me to play with functional coding in Javascript","archived":false,"fork":false,"pushed_at":"2023-03-24T03:18:22.000Z","size":528,"stargazers_count":2,"open_issues_count":26,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-16T11:47:12.908Z","etag":null,"topics":["functional","functional-programming","javascript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/belsrc.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-08T00:23:00.000Z","updated_at":"2022-04-07T02:16:08.000Z","dependencies_parsed_at":"2022-07-27T01:16:42.845Z","dependency_job_id":null,"html_url":"https://github.com/belsrc/fjp","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/belsrc%2Ffjp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/belsrc%2Ffjp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/belsrc%2Ffjp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/belsrc%2Ffjp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/belsrc","download_url":"https://codeload.github.com/belsrc/fjp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226260538,"owners_count":17596495,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functional","functional-programming","javascript"],"created_at":"2024-11-25T02:11:18.001Z","updated_at":"2024-11-25T02:11:18.741Z","avatar_url":"https://github.com/belsrc.png","language":"JavaScript","readme":"# Functional Javascript Playground\n\n[![Maintainability](https://img.shields.io/codeclimate/maintainability/belsrc/fjp.svg?style=flat-square)](https://codeclimate.com/github/belsrc/fjp/maintainability)\n[![Build Status](https://img.shields.io/travis/belsrc/fjp/develop.svg?style=flat-square)](https://travis-ci.org/belsrc/fjp)\n[![Code Coverage](https://img.shields.io/codecov/c/github/belsrc/fjp/develop.svg?style=flat-square)](https://codecov.io/gh/belsrc/fjp/branch/develop)\n[![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)\n[![Last Commit](https://img.shields.io/github/last-commit/belsrc/fjp/develop.svg?style=flat-square)](https://github.com/belsrc/fjp/commits/develop)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/belsrc/fjp/pulls)\n\n\nFunctional 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.\nThese 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.\n\n\n## This is very much a WIP\n\n\n## Functions\n\n\u003cdl\u003e\n\u003cdt\u003e\u003ca href=\"#associate\"\u003eassociate()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eSets the given property and value on the object. Returning a new object.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#average\"\u003eaverage()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eAverages the given array values\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#A\"\u003eA()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eCalls the given function with the given value.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#Fork\"\u003eFork()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eTakes a joiner func, and two other funcs and a value. The value is given to both funcs and the\nresults of each of these is given to the joiner func.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#I\"\u003eI()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eReturns the given value.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#K\"\u003eK()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eTakes two values and returns the given first.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#OR\"\u003eOR()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eGiven two functions that take the same value, returns the first if the result is truthy, otherwise, the second.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#T\"\u003eT()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eCalls the given function with the given value. (Reverse order of apply)\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#compact\"\u003ecompact()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eRemoves falsey values from an array.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#compose\"\u003ecompose()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003ePerforms right-to-left function composition.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#concat\"\u003econcat()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eConcatenates two String|Arrays together.\nReturns empty array if value arent of the same type or not String|Array.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#concatN\"\u003econcatN()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eConcatenates N Arrays together.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#curry\"\u003ecurry()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eWraps the given function, if the number of provided args is sufficient, call the passed function fn.\nOtherwise, return a wrapped function fn that expects the rest of the arguments.\nIf you want to curry a function that accepts a variable number of arguments (a variadic function),\nyou can optionally pass the number of arguments to the second parameter arity.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#difference\"\u003edifference()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eReturns the difference between two arrays.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#distinct\"\u003edistinct()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eReturns all of the distinct values of an array.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#distinctN\"\u003edistinctN()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eReturns all of the distinct values of the given arrays.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#each\"\u003eeach()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eApplies the given func to each element in the array.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#every\"\u003eevery()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eDetermines if all element in an array satisfy the given test function\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#filter\"\u003efilter()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eFilters the array using the given function.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#find\"\u003efind()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eFinds the first element that satisfies the given test func.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#flatten\"\u003eflatten()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eFlattens single nested array.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#isArray\"\u003eisArray()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eDetermines if the given value is an array.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#isFunction\"\u003eisFunction()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eDetermines if the given value is a function.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#isNumber\"\u003eisNumber()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eDetermines if the given value is a number.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#isObject\"\u003eisObject()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eDetermines if the given value is an object.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#isString\"\u003eisString()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eDetermines if the given value is a string.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#not\"\u003enot()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eNegates the given boolean-like value.\u003c/p\u003e\n\u003c/dd\u003e\n\u003cdt\u003e\u003ca href=\"#tap\"\u003etap()\u003c/a\u003e\u003c/dt\u003e\n\u003cdd\u003e\u003cp\u003eCalls the given function with the given value and returns the value.\u003c/p\u003e\n\u003c/dd\u003e\n\u003c/dl\u003e\n\n\u003ca name=\"associate\"\u003e\u003c/a\u003e\n\n## associate()\nSets the given property and value on the object. Returning a new object.\n\n**Kind**: global function  \n**Signature**: associate :: String k -\u003e {} -\u003e v -\u003e {k: v}  \n**Example**  \n```js\nconst obj = associate('c', { a: b }, d)  // { a: b, c: d }\n```\n\u003ca name=\"average\"\u003e\u003c/a\u003e\n\n## average()\nAverages the given array values\n\n**Kind**: global function  \n**Signature**: average :: [Number] -\u003e Number  \n**Example**  \n```js\naverage([ 1, 2, 3 ]); // 2\naverage(1, 2, 3); // 2\naverage(); // 0\n```\n\u003ca name=\"A\"\u003e\u003c/a\u003e\n\n## A()\nCalls the given function with the given value.\n\n**Kind**: global function  \n**Signature**: A :: (a -\u003e b) -\u003e a -\u003e b  \n**Aka**: apply  \n\u003ca name=\"Fork\"\u003e\u003c/a\u003e\n\n## Fork()\nTakes a joiner func, and two other funcs and a value. The value is given to both funcs and the\nresults of each of these is given to the joiner func.\n\n**Kind**: global function  \n**Signature**: Fork :: (b -\u003e c -\u003e d) -\u003e (a -\u003e b) -\u003e (a -\u003e c) -\u003e a -\u003e d  \n**Aka**: join  \n\u003ca name=\"I\"\u003e\u003c/a\u003e\n\n## I()\nReturns the given value.\n\n**Kind**: global function  \n**Signature**: I :: a -\u003e a  \n**Aka**: identity  \n\u003ca name=\"K\"\u003e\u003c/a\u003e\n\n## K()\nTakes two values and returns the given first.\n\n**Kind**: global function  \n**Signature**: K :: a -\u003e b -\u003e a  \n**Aka**: constant  \n\u003ca name=\"OR\"\u003e\u003c/a\u003e\n\n## OR()\nGiven two functions that take the same value, returns the first if the result is truthy, otherwise, the second.\n\n**Kind**: global function  \n**Signature**: OR :: (a -\u003e b) -\u003e (a -\u003e b) -\u003e b  \n**Aka**: alternation  \n\u003ca name=\"T\"\u003e\u003c/a\u003e\n\n## T()\nCalls the given function with the given value. (Reverse order of apply)\n\n**Kind**: global function  \n**Signature**: T :: a -\u003e (a -\u003e b) -\u003e b  \n**Aka**: thrush, applyTo  \n\u003ca name=\"compact\"\u003e\u003c/a\u003e\n\n## compact()\nRemoves falsey values from an array.\n\n**Kind**: global function  \n**Signature**: compact :: [a] -\u003e [a]  \n**Example**  \n```js\ncompact([ 0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34 ]); // [ 1, 2, 3, 'a', 's', 34 ]\ncompact(null) // []\n```\n\u003ca name=\"compose\"\u003e\u003c/a\u003e\n\n## compose()\nPerforms right-to-left function composition.\n\n**Kind**: global function  \n**Signature**: compose :: [(m -\u003e n), ..., (b -\u003e c), (a -\u003e b)] -\u003e a -\u003e n  \n**Example**  \n```js\nconst addOne = x =\u003e x + 1;\nconst timeTen = x =\u003e x * 10;\nconst addOneTimeTen = compose(timeTen, addOne);\nconst result = addOneTimeTen(9); // 100\n```\n\u003ca name=\"concat\"\u003e\u003c/a\u003e\n\n## concat()\nConcatenates two String|Arrays together.\nReturns empty array if value arent of the same type or not String|Array.\n\n**Kind**: global function  \n**Signature**: concat :: a -\u003e b -\u003e c  \n**Example**  \n```js\nconcat('foo', 'bar') // foobar\nconcat([1, 2], [3, 4]) // [1, 2, 3, 4]\n```\n\u003ca name=\"concatN\"\u003e\u003c/a\u003e\n\n## concatN()\nConcatenates N Arrays together.\n\n**Kind**: global function  \n**Signature**: concat :: [a] -\u003e [b] -\u003e ...[n] -\u003e [m]  \n**Example**  \n```js\nconcatN([1, 2], [3, 4], [5, 6]) // [1, 2, 3, 4, 5, 6]\n```\n\u003ca name=\"curry\"\u003e\u003c/a\u003e\n\n## curry()\nWraps the given function, if the number of provided args is sufficient, call the passed function fn.\nOtherwise, return a wrapped function fn that expects the rest of the arguments.\nIf you want to curry a function that accepts a variable number of arguments (a variadic function),\nyou can optionally pass the number of arguments to the second parameter arity.\n\n**Kind**: global function  \n**Signature**: curry :: ((a, b, ..., n) -\u003e m) -\u003e a -\u003e b -\u003e ...n -\u003e m  \n**Example**  \n```js\nconst add = curry((x, y) =\u003e x + y);\nconst addFiveTo = add(5);\naddFiveTo(10); // 15\n```\n\u003ca name=\"difference\"\u003e\u003c/a\u003e\n\n## difference()\nReturns the difference between two arrays.\n\n**Kind**: global function  \n**Signature**: difference :: [a] -\u003e [b] -\u003e [c]  \n**Example**  \n```js\ndifference([ 1, 2, 3 ], [ 1, 2, 4 ]) // [3]\ndifference([], [ 1, 2, 4 ]) // [ 1, 2, 4 ]\ndifference([ 1, 2, 3 ], []) // [ 1, 2, 3 ]\ndifference([ 1, 2, 3 ], null) // [ 1, 2, 3 ]\n```\n\u003ca name=\"distinct\"\u003e\u003c/a\u003e\n\n## distinct()\nReturns all of the distinct values of an array.\n\n**Kind**: global function  \n**Signature**: distinct :: [a] -\u003e [b]  \n**Example**  \n```js\ndistinct([ 1, 2, 2, 3, 4, 4, 5 ]) // [ 1, 2, 3, 4, 5 ]\n```\n\u003ca name=\"distinctN\"\u003e\u003c/a\u003e\n\n## distinctN()\nReturns all of the distinct values of the given arrays.\n\n**Kind**: global function  \n**Signature**: distinctN :: [a] -\u003e [b] -\u003e ...[n] -\u003e [m]  \n**Example**  \n```js\ndistinctN([ 1, 2 ], [ 2, 3, 4 ], [ 4, 5 ]) // [ 1, 2, 3, 4, 5 ]\n```\n\u003ca name=\"each\"\u003e\u003c/a\u003e\n\n## each()\nApplies the given func to each element in the array.\n\n**Kind**: global function  \n**Signature**: each :: (a -\u003e b) -\u003e [c] -\u003e undefined  \n**Example**  \n```js\ndifference(log, [1, 2, 3])\n```\n\u003ca name=\"every\"\u003e\u003c/a\u003e\n\n## every()\nDetermines if all element in an array satisfy the given test function\n\n**Kind**: global function  \n**Signature**: every :: (a -\u003e Bool) -\u003e [a] -\u003e Bool  \n**Aka**: all  \n**Example**  \n```js\nevery(Boolean, [1, 2, 3, 4]) // true\nevery(Boolean, [1, 2, null, 4]) // false\n```\n\u003ca name=\"filter\"\u003e\u003c/a\u003e\n\n## filter()\nFilters the array using the given function.\n\n**Kind**: global function  \n**Signature**: filter :: (a -\u003e Boolean) -\u003e [a] -\u003e [a]  \n**Example**  \n```js\nfilter(x =\u003e x \u003e 5, [1, 2, 3, 5, 6, 7]) // [6, 7]\n```\n\u003ca name=\"find\"\u003e\u003c/a\u003e\n\n## find()\nFinds the first element that satisfies the given test func.\n\n**Kind**: global function  \n**Signature**: find :: (a -\u003e Boolean) -\u003e [a] -\u003e a  \n**Example**  \n```js\nfind(x =\u003e x.score === 5, [{score: 1}, {score: 2}, {score: 5}, {score: 6}, {score: 7}]) // {score: 5}\n```\n\u003ca name=\"flatten\"\u003e\u003c/a\u003e\n\n## flatten()\nFlattens single nested array.\n\n**Kind**: global function  \n**Signature**: flatten :: [a] -\u003e [a]  \n**Example**  \n```js\nflatten([[ 1, 2 ], [ 3, 4 ]];); // [ 1, 2, 3, 4 ]\nflatten(null) // []\n```\n\u003ca name=\"isArray\"\u003e\u003c/a\u003e\n\n## isArray()\nDetermines if the given value is an array.\n\n**Kind**: global function  \n**Signature**: isArray :: a -\u003e Boolean  \n**Example**  \n```js\nisArray([1, 2, 3])  // true\nisArray({ a: 'b' })  // false\n```\n\u003ca name=\"isFunction\"\u003e\u003c/a\u003e\n\n## isFunction()\nDetermines if the given value is a function.\n\n**Kind**: global function  \n**Signature**: isFunction :: a -\u003e Boolean  \n**Example**  \n```js\nisFunction(() =\u003e {})  // true\nisFunction([1, 2, 3])  // false\n```\n\u003ca name=\"isNumber\"\u003e\u003c/a\u003e\n\n## isNumber()\nDetermines if the given value is a number.\n\n**Kind**: global function  \n**Signature**: isNumber :: a -\u003e Boolean  \n**Example**  \n```js\nisNumber(42)  // true\nisNumber(8e5)  // true\nisNumber(0x2F)  // true\nisNumber('foo bar')  // false\n```\n\u003ca name=\"isObject\"\u003e\u003c/a\u003e\n\n## isObject()\nDetermines if the given value is an object.\n\n**Kind**: global function  \n**Signature**: isObject :: a -\u003e Boolean  \n**Example**  \n```js\nisObject({ a: 'b' })  // true\nisObject([1, 2, 3])  // false\n```\n\u003ca name=\"isString\"\u003e\u003c/a\u003e\n\n## isString()\nDetermines if the given value is a string.\n\n**Kind**: global function  \n**Signature**: isString :: a -\u003e Boolean  \n**Example**  \n```js\nisString('foo bar')  // true\nisString({ a: 'b' })  // false\n```\n\u003ca name=\"not\"\u003e\u003c/a\u003e\n\n## not()\nNegates the given boolean-like value.\n\n**Kind**: global function  \n**Signature**: not :: Boolean -\u003e Boolean  \n**Example**  \n```js\nnot(true); // false\nnot(false); // true\nnot(2); // false\n```\n\u003ca name=\"tap\"\u003e\u003c/a\u003e\n\n## tap()\nCalls the given function with the given value and returns the value.\n\n**Kind**: global function  \n**Signature**: tap :: (a -\u003e b) -\u003e a -\u003e a  \n**Example**  \n```js\ntap(console.log, 'foobar') // foobar\n```\n\n\n## License\n\nFJP is licensed under the MIT license.\n\nCopyright © 2018 Bryan Kizer\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbelsrc%2Ffjp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbelsrc%2Ffjp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbelsrc%2Ffjp/lists"}