{"id":13494966,"url":"https://github.com/ryanleecode/fp-ts-cheatsheet","last_synced_at":"2025-09-25T09:51:03.903Z","repository":{"id":51457803,"uuid":"280292014","full_name":"ryanleecode/fp-ts-cheatsheet","owner":"ryanleecode","description":"Cheatsheet for https://gcanti.github.io/fp-ts/","archived":false,"fork":false,"pushed_at":"2021-12-29T23:04:38.000Z","size":32,"stargazers_count":54,"open_issues_count":2,"forks_count":10,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-24T13:23:05.112Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ryanleecode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-17T00:57:05.000Z","updated_at":"2023-09-15T10:02:38.000Z","dependencies_parsed_at":"2022-09-21T01:12:10.978Z","dependency_job_id":null,"html_url":"https://github.com/ryanleecode/fp-ts-cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ryanleecode/fp-ts-cheatsheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanleecode%2Ffp-ts-cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanleecode%2Ffp-ts-cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanleecode%2Ffp-ts-cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanleecode%2Ffp-ts-cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryanleecode","download_url":"https://codeload.github.com/ryanleecode/fp-ts-cheatsheet/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanleecode%2Ffp-ts-cheatsheet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276896553,"owners_count":25724047,"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","status":"online","status_checked_at":"2025-09-25T02:00:09.612Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-07-31T19:01:29.963Z","updated_at":"2025-09-25T09:51:03.880Z","avatar_url":"https://github.com/ryanleecode.png","language":null,"funding_links":[],"categories":["Others"],"sub_categories":[],"readme":"# FP-TS Cheatsheet\n\n## Begin, Commit, Rollback\n\n\u003c!-- verifier:include-node-module:fp-ts --\u003e\n```ts\nimport { pipe } from 'fp-ts/lib/function'\nimport * as TE from 'fp-ts/lib/TaskEither'\n\ndeclare function begin(): Promise\u003cvoid\u003e\ndeclare function commit(): Promise\u003cvoid\u003e\ndeclare function rollback(): Promise\u003cvoid\u003e\n\npipe(\n  TE.bracket(\n    TE.tryCatch(\n      () =\u003e begin(),\n      (err) =\u003e new Error(`begin txn failed: ${err}`),\n    ),\n    () =\u003e\n      TE.tryCatch(\n        () =\u003e commit(),\n        (err) =\u003e new Error(`commit txn failed: ${err}`),\n      ),\n    (_, either) =\u003e\n      pipe(\n        either,\n        TE.fromEither,\n        TE.orElse(() =\u003e\n          TE.tryCatch(\n            () =\u003e rollback(),\n            (err) =\u003e new Error(`rollback txn failed: ${err}`),\n          ),\n        ),\n      ),\n  ),\n)\n```\n\n## Convert Multiple Eithers into a Single Either\n\n\u003c!-- verifier:include-node-module:fp-ts --\u003e\n```ts\nimport * as E from 'fp-ts/lib/Either'\nimport { sequenceS, sequenceT } from 'fp-ts/lib/Apply'\n\ndeclare function func1(): E.Either\u003cError, number\u003e\ndeclare function func2(): E.Either\u003cError, string\u003e\ndeclare function func3(): E.Either\u003cError, object\u003e\n\n// Produces an Object { a: number., b: string, c: object }\nsequenceS(E.either)({\n  a: func1(),\n  b: func2(),\n  c: func3(),\n})\n\n// Produces a Tuple [number, string, object]\nsequenceT(E.either)(func1(), func2(), func3())\n```\n\n## Check for Errors in an Array\n\n\u003c!-- verifier:include-node-module:fp-ts --\u003e\n```ts\nimport * as A from 'fp-ts/lib/Array'\nimport * as E from 'fp-ts/lib/Either'\nimport { sequenceS, sequenceT } from 'fp-ts/lib/Apply'\n\nconst arr = [1,2,3]\n\n// Type signature is E.Either\u003cError, number[]\u003e\nA.array.traverse(E.either)(arr, (val) =\u003e {\n  if (val === 2) {\n    return E.left(new Error('2 is invalid'))\n  }\n\n  return E.right(val)\n})\n```\n\n## Handle a TaskEither Error with another TaskEither\n\n\u003c!-- verifier:include-node-module:fp-ts --\u003e\n```ts\nimport { pipe } from 'fp-ts/lib/function'\nimport { IOEither } from 'fp-ts/lib/IOEither'\nimport {\n  swap,\n  TaskEither,\n  chainFirstW,\n  fromIOEither,\n  chain,\n} from 'fp-ts/lib/TaskEither'\n\ndeclare const log: (message: string, err?: Error) =\u003e IOEither\u003cError, void\u003e\n\ndeclare const foo: TaskEither\u003cError, string\u003e\n\nconst handleError = \u003cE, U\u003e(fe: (e: E) =\u003e TaskEither\u003cE, U\u003e) =\u003e \u003cA\u003e(\n  te: TaskEither\u003cE, A\u003e,\n) =\u003e {\n  return pipe(\n    // 1. Put the error on the right side of the railway\n    swap(te),\n    // 2. Perform an action on the error, if there is one\n    chainFirstW(fe),\n    // 3. Put the error back on the left side\n    swap,\n    // 4. Place the original TaskEither back on the railway\n    chain(() =\u003e te),\n  )\n}\n\npipe(\n  foo,\n  handleError((err) =\u003e pipe(log('Execution failed', err), fromIOEither)),\n) // TaskEither\u003cError, string\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanleecode%2Ffp-ts-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryanleecode%2Ffp-ts-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanleecode%2Ffp-ts-cheatsheet/lists"}