{"id":22750386,"url":"https://github.com/ruby2elixir/rop","last_synced_at":"2025-04-14T12:55:07.722Z","repository":{"id":57544817,"uuid":"49361782","full_name":"ruby2elixir/rop","owner":"ruby2elixir","description":"Railway Oriented Programming in Elixir","archived":false,"fork":false,"pushed_at":"2019-10-19T04:08:52.000Z","size":22,"stargazers_count":20,"open_issues_count":2,"forks_count":3,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-12T03:46:55.475Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","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/ruby2elixir.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":"2016-01-10T10:04:11.000Z","updated_at":"2023-10-02T09:43:47.000Z","dependencies_parsed_at":"2022-08-27T05:20:30.899Z","dependency_job_id":null,"html_url":"https://github.com/ruby2elixir/rop","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/ruby2elixir%2Frop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby2elixir%2Frop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby2elixir%2Frop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby2elixir%2Frop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ruby2elixir","download_url":"https://codeload.github.com/ruby2elixir/rop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248885677,"owners_count":21177636,"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-12-11T04:14:38.329Z","updated_at":"2025-04-14T12:55:07.689Z","avatar_url":"https://github.com/ruby2elixir.png","language":"Elixir","readme":"# ROP - Railway Oriented Programming in Elixir\n\n\n[![Build status](https://travis-ci.org/ruby2elixir/rop.svg \"Build status\")](https://travis-ci.org/ruby2elixir/rop)\n[![Hex version](https://img.shields.io/hexpm/v/rop.svg \"Hex version\")](https://hex.pm/packages/rop)\n![Hex downloads](https://img.shields.io/hexpm/dt/rop.svg \"Hex downloads\")\n\n----\n\nSome macros to enable railsway-oriented programming in Elixir.\nCollected from code snippets and wrapped into a simple library for your convenience.\n\n\nFor more examples please check the tests here:\n- [Examples](https://github.com/ruby2elixir/rop/blob/master/test/rop_test.exs)\n\n\n## Sources for inspiration + copying\n- https://github.com/remiq/railway-oriented-programming-elixir\n- https://gist.github.com/zabirauf/17ced02bdf9829b6956e (Railway Oriented Programming macros in Elixir)\n\n\n## Installation\nIf [available in Hex](https://hex.pm/docs/publish), the package can be installed as:\n\n1. Add rop to your list of dependencies in `mix.exs`:\n```elixir\ndef deps do\n  [{:rop, \"~\u003e 0.5\"}]\nend\n```\n\n\n\n## Usage\nCall\n\n```elixir\nuse Rop\n```\n\nin your module. That will give you access to following macros/functions:\n\n### `\u003e\u003e\u003e`\nNo need to stop pipelining in case of an error somewhere in the middle\n\n\nused like: `1 |\u003e fn1 \u003e\u003e\u003e fn2 \u003e\u003e\u003e fn3 \u003e\u003e\u003e fn4`\n\n```elixir\ndefmodule TripleArrowExample do\n  use Rop\n  def tagged_inc(v) do\n    IO.puts \"inc for #{v}\" # sideeffect for demonstration\n    {:ok, v + 1}\n  end\n\n  def error_fn(_) do\n    {:error, \"I'm a bad fn!\"}\n  end\n\n  def raising_fn(_) do\n    raise \"I'm raising!\"\n  end\n\n  def result do\n    1 |\u003e tagged_inc \u003e\u003e\u003e tagged_inc \u003e\u003e\u003e tagged_inc\n  end\n\n  def error_result do\n    1 |\u003e tagged_inc \u003e\u003e\u003e tagged_inc \u003e\u003e\u003e error_fn \u003e\u003e\u003e tagged_inc\n  end\n\n  def raising_result do\n    1 |\u003e tagged_inc \u003e\u003e\u003e tagged_inc \u003e\u003e\u003e raising_fn \u003e\u003e\u003e tagged_inc\n  end\nend\n\niex\u003e TripleArrowExample.result\ninc for 1\ninc for 2\ninc for 3\n{:ok, 4}\n\n### increases twice, errors and tries to increase again\n### notice that after errored result we don't execute any function anymore in the pipeline,\n### e.g. only tagged_inc before error_fn were executed.\niex\u003e TripleArrowExample.error_result\ninc for 1\ninc for 2\n{:error, \"I'm a bad fn!\"}\n\n### raises... We'll fix it in a later example for try_catch!\niex\u003e TripleArrowExample.raising_result\ninc for 1\ninc for 2\n** (RuntimeError) I'm raising!\n```\n\n\n### `bind`\nWraps a simple function to return a tagged tuple with `:ok` to comply to the protocol `{:ok, result}`: e.g.\n\n```elixir\ndefmodule BindExample do\n  use Rop\n  def inc(v) do\n    v + 1\n  end\n\n  def only_last_pipe_tagged_result(v) do\n    v |\u003e inc |\u003e bind(inc)\n  end\n\n  def result_fully_tagged(v) do\n    v |\u003e bind(inc) \u003e\u003e\u003e bind(inc) \u003e\u003e\u003e bind(inc)\n  end\nend\niex\u003e BindExample.only_last_pipe_tagged_result(2)\n{:ok, 4}\n\niex\u003e BindExample.result_fully_tagged(2)\n{:ok, 5}\n```\n\n\n### `try_catch`\n\nWraps raising functions to return a tagged tuple `{:error, ErrorMessage}`  to comply with the protocol\n\n```elixir\n# modified example from TripleArrowExample to handle raising functions\ndefmodule TryCatchExample do\n  use Rop\n  def tagged_inc(v) do\n    IO.puts \"inc for #{v}\" # sideeffect for demonstration\n    {:ok, v + 1}\n  end\n\n  def raising_fn(_) do\n    raise \"I'm raising!\"\n  end\n\n  def result do\n    1 |\u003e tagged_inc \u003e\u003e\u003e tagged_inc \u003e\u003e\u003e tagged_inc\n  end\n\n  def raising_result_wrapped(v) do\n    v |\u003e tagged_inc \u003e\u003e\u003e tagged_inc \u003e\u003e\u003e try_catch(raising_fn) \u003e\u003e\u003e tagged_inc\n  end\nend\n\niex\u003e TryCatchExample.raising_result_wrapped(1)\ninc for 1\ninc for 2\n{:error, %RuntimeError{message: \"I'm raising!\"}}\n```\n\n\n#### `tee`\n\nLike a similar Unix utility it does some work and returns the input. See [tee (command), Unix](https://en.wikipedia.org/wiki/Tee_(command)).\n\n\n\n```elixir\ndefmodule TeeExample do\n  use Rop\n  def tagged_inc(v) do\n    IO.puts \"inc for #{v}\" # sideeffect for demonstration\n    {:ok, v + 1}\n  end\n\n  def calc(v) do\n    v |\u003e tee(tagged_inc) \u003e\u003e\u003e tee(tagged_inc) \u003e\u003e\u003e tee(tagged_inc)\n  end\nend\n\n# notice how the incremented value is not passed through the pipeline,\n# but just the original argument `1`\niex\u003e TeeExample.calc(1)\ninc for 1\ninc for 1\ninc for 1\n_{:ok, 1}\n```\n\n\n### `ok`\n\nA simple utility function to extract the value from `{:ok, result}` tuple and to raise the error in `{:error, ErrorStruct}`.\n\n```elixir\ndefmodule OkExample do\n  use Rop\n  def ok_result do\n    {:ok, 1} |\u003e ok\n  end\n\n  def error_result do\n    {:error, %ArithmeticError{}} |\u003e ok\n  end\n\n  def any_value_result do\n    \"bad value\" |\u003e ok\n  end\nend\n\niex\u003e OkExample.ok_result\n1\n\niex\u003e OkExample.error_result\n** (ArithmeticError) bad argument in arithmetic expression\n    (rop) lib/rop.ex:70: Rop.ok/1\n\n\niex\u003e OkExample.any_value_result\n** (RuntimeError) bad value\n    (rop) lib/rop.ex:71: Rop.ok/1\n```\n\n\n\n\nBackground information\n----------------------\n\n### Some discussions about Railsway programming:\n- http://insights.workshop14.io/2015/10/18/handling-errors-in-elixir-no-one-say-monad.html\n- http://blog.danielberkompas.com/2015/09/03/better-pipelines-with-monadex.html\n- http://onor.io/2015/08/27/railway-oriented-programming-in-elixir/\n- http://www.zohaib.me/railway-programming-pattern-in-elixir/\n- http://www.zohaib.me/monads-in-elixir-2/\n- http://fsharpforfunandprofit.com/posts/recipe-part2/\n- https://www.reddit.com/r/programming/comments/30coly/railway_oriented_programming_in_elixir/\n\n### Code (Railsway Programming)\n- https://github.com/remiq/railway-oriented-programming-elixir/blob/master/lib/rop.ex\n- https://gist.github.com/zabirauf/17ced02bdf9829b6956e (Railway Oriented Programming macros in Elixir) -\u003e Rop\n- https://github.com/CrowdHailer/OK/blob/master/lib/ok.ex\n- https://gist.github.com/danielberkompas/52216db76d764a68dfa3 -\u003e pipeline.ex\n\n### Code (Monads)\n- https://github.com/rmies/monad.git\n- https://github.com/rob-brown/MonadEx.git\n\n\n### Altenatives\n  - https://github.com/batate/elixir-pipes - Macros for more flexible composition with the Elixir Pipe operator\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby2elixir%2Frop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fruby2elixir%2Frop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby2elixir%2Frop/lists"}