{"id":13709396,"url":"https://github.com/purescript/purescript-effect","last_synced_at":"2026-02-25T21:05:47.565Z","repository":{"id":28765713,"uuid":"119270415","full_name":"purescript/purescript-effect","owner":"purescript","description":"The Effect monad, for handling native side effects ","archived":false,"fork":false,"pushed_at":"2022-09-17T14:37:51.000Z","size":65,"stargazers_count":52,"open_issues_count":0,"forks_count":20,"subscribers_count":6,"default_branch":"master","last_synced_at":"2026-01-29T04:47:46.822Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PureScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/purescript.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-28T15:27:54.000Z","updated_at":"2025-05-20T04:20:58.000Z","dependencies_parsed_at":"2022-07-27T16:48:54.238Z","dependency_job_id":null,"html_url":"https://github.com/purescript/purescript-effect","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/purescript/purescript-effect","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purescript%2Fpurescript-effect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purescript%2Fpurescript-effect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purescript%2Fpurescript-effect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purescript%2Fpurescript-effect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/purescript","download_url":"https://codeload.github.com/purescript/purescript-effect/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/purescript%2Fpurescript-effect/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29839984,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-25T20:42:33.054Z","status":"ssl_error","status_checked_at":"2026-02-25T20:42:21.322Z","response_time":61,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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-08-02T23:00:38.825Z","updated_at":"2026-02-25T21:05:47.543Z","avatar_url":"https://github.com/purescript.png","language":"PureScript","readme":"# purescript-effect\n\n[![Latest release](http://img.shields.io/github/release/purescript/purescript-effect.svg)](https://github.com/purescript/purescript-effect/releases)\n[![Build status](https://github.com/purescript/purescript-effect/workflows/CI/badge.svg?branch=master)](https://github.com/purescript/purescript-effect/actions?query=workflow%3ACI+branch%3Amaster)\n[![Pursuit](https://pursuit.purescript.org/packages/purescript-effect/badge)](https://pursuit.purescript.org/packages/purescript-effect)\n\nThe `Effect` monad, for handling native side effects.\n\n## Installation\n\n```\nspago install effect\n```\n\n## Documentation\n\nValues in PureScript do not have side-effects by default. This package provides\nthe standard type PureScript uses to handle \"native\" effects, i.e. effects\nwhich are provided by the runtime system, and which cannot be emulated by pure\nfunctions. Some examples of native effects are:\n\n- Console IO\n- Random number generation\n- Exceptions\n- Reading/writing mutable state\n\nAnd in the browser:\n\n- DOM manipulation\n- XMLHttpRequest / AJAX calls\n- Interacting with a websocket\n- Writing/reading to/from local storage\n\nAll of these things may be represented in PureScript by the `Effect` type.\nA value of the type `Effect a` represents a computation which may perform\nsome native effects, and which produces a value of the type `a` once it\nfinishes. For example, the module `Effect.Random` from `purescript-random`\nexports a value `random`, whose type is `Effect Number`. When run, this\neffect produces a random number between 0 and 1. To give another example,\nthe module `Effect.Console` exports a function `log`, whose type is\n`String -\u003e Effect Unit`. This function takes a string and produces an\neffect which, when run, prints the provided string to the console.\n\n```purescript\nmodule RandomExample where\nimport Prelude\nimport Effect (Effect)\nimport Effect.Random (random)\nimport Effect.Console (log)\n\nprintRandom :: Effect Unit\nprintRandom = do\n  n \u003c- random\n  log (show n)\n```\n\nIn this example, `printRandom` is an effect which generates a random\nnumber between 0 and 1 and prints it to the console. You can test it out\nin the repl:\n\n```\n\u003e import RandomExample\n\u003e printRandom\n0.71831842260513870\nunit\n```\n\nThe `unit` is there because all effects must produce a value. When there\nis nothing useful to return, we usually use the type `Unit`, which has\njust one value: `unit`.\n\n### Effects and Purity\n\nNote that using `Effect` does not compromise the purity of your program.\nFunctions which make use of `Effect` are still pure, in the sense that\nall functions will return the same outputs given the same inputs.\n\nThis is enabled by having the `Effect` type denote _values_ which can be\nrun to produce native effects; an `Effect a` is a computation which has\nnot yet necessarily been performed. In fact, there is no way of \"running\"\nan `Effect` manually; we cannot provide a (safe) function of the type\n`forall a. Effect a -\u003e a`, because such a function would violate purity;\nit could produce different outputs given the same input. (Note that there\nis actually such a function in the module `Effect.Unsafe`, but it is best\navoided outside of exceptional circumstances.)\n\nInstead, the recommended way of \"running\" an effect is to include it as\npart of your program's `main` value.\n\nA consequence of being able to represent native effects purely using\n`Effect` is that you can construct and pass `Effect` values around your\nprograms freely. For example, we can write functions which can decide\nwhether to run a given effect just once, many times, or not at all:\n\n```purescript\n-- | Runs an effect three times, provided that the given condition is\n-- | true.\nthriceIf :: Boolean -\u003e Effect Unit -\u003e Effect Unit\nthriceIf cond effect =\n  if cond\n    then do\n      effect\n      effect\n      effect\n    else\n      pure unit\n```\n\n### Using Effects via the Foreign Function Interface\n\nA computation of type `Effect a` is implemented in JavaScript as a\nzero-argument function whose body is expected to perform its side-effects\nbefore finally returning its result. For example, suppose we wanted a\ncomputation which would increment a global counter and return the new\nresult each time it was run. We can implement this as follows:\n\n```purescript\n-- Counter.purs\nforeign import incrCounter :: Effect Int\n```\n\nand in the corresponding JavaScript module:\n\n```javascript\n// Counter.js\nexports.incrCounter = function () {\n  if (!window.globalCounter) {\n    window.globalCounter = 0;\n  }\n  return ++window.globalCounter;\n};\n```\n\nFor more information about the FFI (Foreign Function Interface), see\nthe [documentation repository](https://github.com/purescript/documentation).\nThis package also provides a module\n[Effect.Uncurried](https://pursuit.purescript.org/packages/purescript-effect/docs/Effect.Uncurried)\nto simplify the process of making uncurried effectful JavaScript functions\navailable to PureScript via the FFI.\n\n### The Effect type is Magic\n\nThe PureScript compiler has special support for the `Effect` monad.\nOrdinarily, a chain of monadic binds might result in poor performance when\nexecuted. However, the compiler can generate code for the `Effect` monad\nwithout explicit calls to the monadic bind function `\u003e\u003e=`.\n\nTake the random number generation example from above. When compiled, the\ncompiler produces the following JavaScript:\n\n```javascript\nvar printRandom = function __do() {\n  var $0 = Effect_Random.random();\n  return Effect_Console.log(Data_Show.show(Data_Show.showNumber)($0))();\n};\n```\n\nwhereas a more naive compiler might, for instance, produce:\n\n```javascript\nvar printRandom = Control_Bind.bind(Effect.bindEffect)(Effect_Random.random)(\n  function ($0) {\n    return Effect_Console.log(Data_Show.show(Data_Show.showNumber)($0));\n  }\n);\n```\n\nWhile this is a small improvement, the benefit is greater when using\nmultiple nested calls to `\u003e\u003e=`.\n\n### API reference\n\nModule documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-effect).\n","funding_links":[],"categories":["Libraries and Tools","Essential libraries","Effect Management"],"sub_categories":["Core Libraries"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpurescript%2Fpurescript-effect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpurescript%2Fpurescript-effect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpurescript%2Fpurescript-effect/lists"}