{"id":15706719,"url":"https://github.com/knutwalker/algebras","last_synced_at":"2025-06-12T06:11:28.452Z","repository":{"id":29741301,"uuid":"33284760","full_name":"knutwalker/algebras","owner":"knutwalker","description":"Reusable, composable, reasonably priced algebras for typeful effects and composable applications","archived":false,"fork":false,"pushed_at":"2015-04-02T16:24:45.000Z","size":152,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-05T11:19:59.378Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/knutwalker.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2015-04-02T02:27:01.000Z","updated_at":"2025-02-23T12:38:30.000Z","dependencies_parsed_at":"2022-07-21T04:24:19.230Z","dependency_job_id":null,"html_url":"https://github.com/knutwalker/algebras","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/knutwalker/algebras","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knutwalker%2Falgebras","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knutwalker%2Falgebras/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knutwalker%2Falgebras/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knutwalker%2Falgebras/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/knutwalker","download_url":"https://codeload.github.com/knutwalker/algebras/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/knutwalker%2Falgebras/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259410151,"owners_count":22852971,"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":[],"created_at":"2024-10-03T20:27:16.604Z","updated_at":"2025-06-12T06:11:28.433Z","avatar_url":"https://github.com/knutwalker.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Algebras\n\nReusable, composable, reasonably priced algebras for typeful effects and composable applications.\n\n## Install\n\nFrom maven central\n\n```\nlibraryDependencies += \"de.knutwalker\" %% \"algebra-effect\" % \"0.1.0\"\n```\n\n`Algebras` only depend on `scalaz-core`.\n\n## Available modules\n\n### Effect\n\n`\"de.knutwalker\" %% \"algebra-effect\" % \"0.1.0\"`\n\n`algebras.Effect` is a placeholder for the resulting effect type.\nIt is basically a wrapper around `scalaz.Free` and provides typical combinators like `map` and `flatMap`.\n\n\nThe `algebra-effect` module also contains syntax for arbitrary types that add an `effect[F[_]]` method to\nsome `A` to lift a value into an `Effect`.\n\n### Log\n\n`\"de.knutwalker\" %% \"algebra-log\" % \"0.1.0\"`\n\n`algebras.Log` prodives an algebra for simple logging (`debug`, `info`, `warn`, `error`)\n\n### Random\n\n`\"de.knutwalker\" %% \"algebra-random\" % \"0.1.0\"`\n\n`algebras.Random` prodives an very simple algebra for random number generation.\n\nThe basic method is `nextInt(n)` which returns an int in \\[0, n) – i.e. n is the upper exclusive bound.\nThis is conform with `scala.util.Random.nextInt(Int)`\n\nThere are some additional methods, like `chooseInt(a, b)` which return an in in \\[a, b] and `oneOf` which takes a variable number of `A`s and returns one of these `A`s.\n\n\n## Using Effect\n\nAny method, that produces an effect using an algebra must be paramterized in a type `F[_]`\nwith a context bound for the algebra that this method uses. The return type is `Effect[F, A]` where `A` is the methods actual return type.\n\nAt the end of the universe, you have a composed `Effect[F, A]` that you need to run.\n\nTo do so, first you have to combine all used algebras (their Ops, actually) into a super-algebra using `scalaz.Coproduct`\nThen, you write interpreters for each algebra and combine those into an interpreter for the super-algebra.\nAn interpreter is a `scala.~\u003e` for the algebra op and some monad.\nAll interpreters must use the same result monad.\n\nFinally, run `effect.runM(interpreter)` to turn your `Effect[F, A]` into an `M[A]`.\n\nHere's an example. Suppose we want to roll a dice...\n\n```scala\nimport algebras._, Algebras._\n\nimport scalaz.{~\u003e, Coproduct, Id}, Id.Id\n\nobject pureCore {\n\n  def roll[F[_]: Random]: Effect[F, Int] =\n    Random.chooseInt(1, 6)\n\n  def play[F[_]: Random : Log]: Effect[F, Int] = for {\n    _   ← Log.debug(\"rolling a dice...\")\n    num ← roll\n    _   ← Log.debug(s\"rolled a $num\")\n    _   ← if (num == 6) Log.info(\"Yay! Rolled a 6!\")\n          else          ().effect[F]\n  } yield num\n}\n\nobject edgeOfTheWorld {\n\n  type App[A] = Coproduct[RandomOp, LogOp, A]\n\n  val RandomInterpreter = new (RandomOp ~\u003e Id) {\n    def apply[A](fa: RandomOp[A]): A = fa match {\n      case RandomOp.NextInt(n) ⇒ scala.util.Random.nextInt(n)\n    }\n  }\n\n  val LogInterpreter = new (LogOp ~\u003e Id) {\n    def apply[A](fa: LogOp[A]): A = fa match {\n      case LogOp.Logs(msg, level, _) ⇒ println(s\"[$level] $msg\")\n    }\n  }\n\n  val AppInterpreter: App ~\u003e Id = RandomInterpreter or LogInterpreter\n  val program: Effect[App, Int] = pureCore.play[App]\n\n  def main(args: Array[String]): Unit = {\n    program.runM(AppInterpreter)\n  }\n}\n```\n\n\n## Interpreters\n\nThere are two basic interpreters already available.\n\n`algebra-interpreter-rng` which uses [NICTA/rng](https://github.com/NICTA/rng) for the random number generation.\n`algebra-interpreter-slf4j` which uses slf4j (duh) to log the messages. \n\n\nUsing these interpreters, the example above could be rewritten as\n\n```scala\nimport algebras._, Algebras._\n\nimport scalaz.{~\u003e, Coproduct, effect}, effect.IO\n\nobject pureCore {\n\n  def roll[F[_]: Random]: Effect[F, Int] =\n    Random.chooseInt(1, 6)\n\n  def play[F[_]: Random : Log]: Effect[F, Int] = for {\n    _   ← Log.debug(\"rolling a dice...\")\n    num ← roll\n    _   ← Log.debug(s\"rolled a $num\")\n    _   ← if (num == 6) Log.info(\"Yay! Rolled a 6!\")\n          else          ().effect[F]\n  } yield num\n}\n\nobject edgeOfTheWorld {\n\n  type App[A] = Coproduct[RandomOp, LogOp, A]\n  val AppInterpreter: App ~\u003e IO = random.Interpreter or log.Interpreter\n  val program: Effect[App, Int] = pureCore.play[App]\n\n  def main(args: Array[String]): Unit = {\n    program.runM(AppInterpreter).unsafePerformIO()\n  }\n}\n```\n\n## Acknowledgement\n\nBased on https://www.parleys.com/tutorial/composable-application-architecture-reasonably-priced-monads \n\n## License\n\nApache 2\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknutwalker%2Falgebras","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fknutwalker%2Falgebras","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknutwalker%2Falgebras/lists"}