{"id":20204998,"url":"https://github.com/astridlyre/fp","last_synced_at":"2025-03-03T09:41:13.352Z","repository":{"id":43279678,"uuid":"411551381","full_name":"astridlyre/fp","owner":"astridlyre","description":"fp - My little functional programming library","archived":false,"fork":false,"pushed_at":"2023-02-23T23:55:59.000Z","size":1227,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-02T05:30:07.211Z","etag":null,"topics":["functional-programming","iterable","javascript","multimethods","observable","stream"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/astridlyre.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":"2021-09-29T06:10:06.000Z","updated_at":"2022-01-09T11:23:37.000Z","dependencies_parsed_at":"2022-09-12T23:00:53.474Z","dependency_job_id":null,"html_url":"https://github.com/astridlyre/fp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astridlyre%2Ffp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astridlyre%2Ffp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astridlyre%2Ffp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astridlyre%2Ffp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/astridlyre","download_url":"https://codeload.github.com/astridlyre/fp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241644544,"owners_count":19996177,"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-programming","iterable","javascript","multimethods","observable","stream"],"created_at":"2024-11-14T05:16:08.425Z","updated_at":"2025-03-03T09:41:13.329Z","avatar_url":"https://github.com/astridlyre.png","language":"TypeScript","readme":"# fp\n\nMy little functional programming library. Just a few functions I don't like\nre-writing. I am slowly adding tests, run with `npm run test` and you should see\n272 tests passing.\n\nFeatures:\n\n- Many utility functions, such as `compose`, `pipe`, and `curry`.\n- Some ADTs such as `Maybe`, `Result`, and `IO`\n- A simple reactive library for `Observable`, including methods like `map`,\n  `filter`, and `reduce`.\n\n## Install\n\n```bash\nnpm install @ebflat9/fp\n```\n\n[View the npm package page](https://www.npmjs.com/package/@ebflat9/fp)\n\n## Functional Programming Examples\n\n```javascript\nimport * as fp from '@ebflat9/fp'\n\n// identity x =\u003e x\nfp.identity(1) // 1\n\n// constant x =\u003e _ =\u003e x\nconst one = fp.constant(1) // () =\u003e 1\none() // 1\n\n// flip2, flips the order of arguments\nconst fn = (a, b) =\u003e `${a}${b}`\nfp.flip(fn)('hello', 'world') // 'worldhello'\n\n// unary, convert a function to one taking a single argument\nconst fn = (...args) =\u003e [...args]\nfp.unary(fn)(1, 2, 3) // [1]\n\n// demethodize, convert a method to a regular function\nconst toUpperCase = demethodize(String.prototype.toUpperCase)\ntoUpperCase('hi') // 'HI'\n\n// deepProp, get a prop using a path\nconst obj = {\n  a: {\n    b: {\n      c: {\n        d: 1,\n      },\n    },\n  },\n}\nfp.deepProp('a.b.c.d', obj) // 1\n\n// deepSetProp, set a prop using a path (returns a copy)\nconst obj = {\n  a: {\n    b: 2,\n  },\n}\nfp.deepSetProp('a.b', 3)(obj) // { a: { b: 3 } }\n\n// deepPick, pick only keys from paths in an object\nconst obj = {\n  a: {\n    b: {\n      c: 'hi',\n    },\n    e: 'world',\n  },\n  h: 'sup',\n}\nfp.deepPick(['a.b.c', 'a.e'])(obj) // { a: { b: { c: 'hi' }, e: 'world' } }\n\n// rename, using a keymap, rename the keys in an object\nconst obj = {\n  title: 'My book',\n  publication_date: 1987,\n}\nfp.rename({ publication_date: 'publicationDate' }, obj)\n// {title: 'My book', publicationDate: 1987 }\n\n// aggregateOn, combine all properties from two objects into one, rightmost object\n// wins in case of duplicate properties, keymap properties are combined into an\n// array of unique values.\nconst a = {\n  title: 'my book',\n  author: 'tim',\n  publication_date: 2008,\n}\nconst b = {\n  title: 'my book',\n  publication_date: 1987,\n  author: 'dave',\n}\nfp.aggregateOn({ author: 'authors', publication_date: 'publicationDates' }, a, b)\n// { title: 'my book', authors: ['tim', 'dave'], publicationDates: [2008, 1987] }\n\n// groupBy, partition an array of objects into groups by key\nconst a = [\n  {\n    name: 'tim',\n    age: 15,\n  },\n  {\n    name: 'tim',\n    age: 5,\n  },\n  {\n    name: 'bob',\n    age: 87,\n  },\n]\nfp.groupBy('name', a)\n// [[{name: 'tim', age: 15}, {name: 'tim', age: 5}], [{name: 'bob', age: 87}]]\n\n// keyBy, convert an array to an object\nconst arr = [{ name: 'tim' }, { name: 'bob' }]\nfp.keyBy('name', arr) // { tim: { name: 'tim' }, bob: {name: 'bob' } }\n\n// deepJoin, combine two arrays\nconst a = [\n  {\n    isbn: '978-0812981605',\n    title: '7 Habits of Highly Effective People',\n    available: true,\n  },\n  {\n    isbn: '978-1982137274',\n    title: 'The Power of Habit',\n    available: false,\n  },\n]\nconst b = [\n  {\n    isbn: '978-0812981605',\n    title: '7 Habits of Highly Effective People',\n    subtitle: 'Powerful Lessons in Personal Change',\n    number_of_pages: 432,\n  },\n  {\n    isbn: '978-1982137274',\n    title: 'The Power of Habit',\n    subtitle: 'Why We Do What We Do in Life and Business',\n    subjects: ['Social Aspects', 'Habit', 'Change (Psychology)'],\n  },\n]\nfp.deepJoin('isbn', 'isbn', a, b)\n/* [\n * {\n *   available: true,\n *   isbn: '978-0812981605',\n *   number_of_pages: 432,\n *   subtitle: 'Powerful Lessons in Personal Change',\n *   title: '7 Habits of Highly Effective People',\n * },\n * {\n *   available: false,\n *   isbn: '978-1982137274',\n *   subjects: ['Social Aspects', 'Habit', 'Change (Psychology)'],\n *   subtitle: 'Why We Do What We Do in Life and Business',\n *   title: 'The Power of Habit',\n *  },\n *]\n */\n```\n\n## MultiMethod\n\nA _multimethod_ is a function that decides which handler to call based on its\narguments. It is a way to create polymorphism without classes.\n\n```javascript\n// multiMethod\nconst store = {\n  todos: [],\n  add(todo) {\n    this.todos.push({ text: todo, id: this.todos.length + 1 })\n    return this\n  },\n  remove(id) {\n    this.todos = this.todos.filter(td =\u003e td.id !== id)\n    return this\n  },\n}\nconst dispatch = fp.multi(\n  (action, store) =\u003e action.type,\n  fp.method('ADD_TODO', (action, store) =\u003e store.add(action.text)),\n  fp.method('REMOVE_TODO', (action, store) =\u003e store.remove(action.id))\n)\ndispatch({ type: 'ADD_TODO', text: 'Hello world' }, store)\n// store.todos = [{ text: 'Hello world', id: 1 }]\ndispatch({ type: 'REMOVE_TODO', id: 1 }, store)\n// store.todos = []\n\n// mapping a multiMethod\nconst a = fp.multi(fp.method('a', () =\u003e 'b'))\nconst upper = a.map(s =\u003e s.toUpperCase())\nupper('a') // 'B'\n\n// use functions as keys\nconst router = fp.multi(\n  fp.method(req =\u003e ['GET'].includes(req.method) \u0026\u0026 req.url === '/', 'OK'),\n  fp.method(\n    req =\u003e ['GET', 'POST'].includes(req.method) \u0026\u0026 req.url === '/users',\n    [{ id: 1, name: 'John' }]\n  ),\n  fp.method('Unknown endpoint')\n)\nrouter({ method: 'GET', url: '/' }) // 'OK'\n```\n\n## Observable\n\nAn _Observable_ is a way to abstract asynchronous and synchronous events in\na way that makes it easier to work with, and more consistent.\n\n```javascript\n// Create an Observable\nObservable.from([1, 2, 3]).subscribe(console.log) // 1, 2, 3\n\nObservable.of(1, 2, 3).subscribe(console.log) // 1, 2, 3\n\nObservable.fromPromise(\n  new Promise(resolve =\u003e setTimeout(() =\u003e resolve('hi'), 1))\n).subscribe(console.log) // 'hi'\n```\n\n### Observable Operators\n\nVarious operations are available, such as:\n\n```javascript\n// Map\nObservable.from([1, 2, 3])\n  .map(x =\u003e x * x)\n  .subscribe(console.log) // 1, 4, 9\n\n// Filter\nObservable.from([1, 2, 3])\n  .filter(n =\u003e n % 2 !== 0)\n  .subscribe(console.log) // 1, 3\n\n// Take\nObservable.from([1, 2, 3]).take(2).subscribe(console.log) // 1, 2\n\n// Skip\nObservable.from([1, 2, 3]).skip(2).subscribe(console.log) // 3\n\n// Concat\nObservable.from([1, 2, 3])\n  .concat(Observable.from(['a', 'b', 'c']))\n  .subscribe(console.log) // [1, 2, 3, 'a', 'b', 'c']\n\n// Combine\nObservable.from([1, 2, 3])\n  .combine(Observable.from(['a', 'b', 'c']))\n  .subscribe(console.log) // [3, 'a']\n\n// flatMap\nObservable.from([1, 2, 3])\n  .flatMap(x =\u003e Observable.from([1, 2, 3].map(y =\u003e x + y)))\n  .subscribe(console.log) // [2, 3, 4, 3, 4, 5, 4, 5, 6]\n\n// Pick\nObservable.from([{ name: 'tim' }, { name: 'bob' }])\n  .pick('name')\n  .subscribe(console.log) // ['tim', 'bob']\n\n// Distinct\nObservable.from([1, 2, 2, 3]).distinct().subscribe(console.log) // [1, 2, 3]\n\n// Until\nObservable.from([1, 2, 3, 4, 5])\n  .until(n =\u003e n \u003e 3)\n  .subscribe(console.log) // [1, 2, 3]\n\n// Zip\nObservable.from([1, 2, 3])\n  .zip(Observable.from(['a', 'b', 'c']))\n  .subscribe(console.log) // [1, 'a'], [2, 'b'], [3, 'c']\n```\n\n### Observable Subjects\n\nA subject can act as an observable and an observer:\n\n```javascript\nconst values = []\nconst stream = Observable.subject()\nstream\n  .map(x =\u003e x * x)\n  .filter(x =\u003e x % 2 === 0)\n  .subscribe({\n    next: value =\u003e values.push(value),\n  })\nObservable.from([1, 2, 3, 4, 5, 6]).subscribe(stream)\n// values = [4, 16, 36]\n```\n\n### Observable Sharing\n\nShare an async (hot) or sync (cold) stream:\n\n```javascript\nconst values = []\nconst values2 = []\nconst stream = Observable.from([1, 2, 3, 4]).share()\n\nstream.subscribe({\n  next: value =\u003e values.push(value),\n})\n// values = [1, 2, 3, 4]\n\nstream.subscribe({\n  next: value =\u003e values2.push(value),\n})\n// values2 = [1, 2, 3, 4]\n```\n\n## Store\n\nMy attempt to write a simple Redux clone.\n\n```javascript\nimport { Reducer, createStore } from '@ebflat9/fp'\n\n// Create a reducer\nconst reducer = Reducer.builder()\n  .case('ADD', (state, action) =\u003e ({\n    ...state,\n    value: action.payload,\n  }))\n  .init({ value: null })\n  .build()\n\n// Create a store\nconst store = createStore(reducer)\n\n// Listen to updates\nstore\n  .observe()\n  .map(state =\u003e state.value \u0026\u0026 state.value.toUpperCase())\n  .subscribe(console.log)\n\n// Dispatch an update\nstore.dispatch({ type: 'ADD', payload: 'hello' }) // 'HELLO'\n```\n\n### Creating an Async Thunk\n\n```javascript\nimport { createAsyncThunk } from '@ebflat/fp'\n\nconst myThunk = createAsyncThunk('ADD', arg =\u003e\n  new Promise(resolve) =\u003e setTimeout(() =\u003e resolve(arg), 1)\n)\n\nconst myReducer = Reducer.builder()\n  .case(myThunk.fulfilled.type, (state, action) =\u003e ({\n    ...state,\n    value: action.payload\n  }))\n  .init({value: null})\n  .build()\n\nconst myStore = createConfiguredStore(myReducer)\n\nstore.dispatch(myThunk('hello'))\nstore.observe().subscribe(console.log) // { value: 'hello' }\n```\n\nThere are many more functions available. Check out the tests for further\nclarification.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastridlyre%2Ffp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fastridlyre%2Ffp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastridlyre%2Ffp/lists"}