{"id":22750390,"url":"https://github.com/ruby2elixir/plumber_girl","last_synced_at":"2025-04-14T12:55:07.701Z","repository":{"id":57535278,"uuid":"49520321","full_name":"ruby2elixir/plumber_girl","owner":"ruby2elixir","description":"PlumberGirl takes care of your Elixir piping issues!","archived":false,"fork":false,"pushed_at":"2016-12-23T13:24:58.000Z","size":118,"stargazers_count":32,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T15:11:36.422Z","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":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-12T18:28:56.000Z","updated_at":"2023-09-01T11:26:25.000Z","dependencies_parsed_at":"2022-09-26T18:30:24.985Z","dependency_job_id":null,"html_url":"https://github.com/ruby2elixir/plumber_girl","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%2Fplumber_girl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby2elixir%2Fplumber_girl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby2elixir%2Fplumber_girl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby2elixir%2Fplumber_girl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ruby2elixir","download_url":"https://codeload.github.com/ruby2elixir/plumber_girl/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:40.130Z","updated_at":"2025-04-14T12:55:07.665Z","avatar_url":"https://github.com/ruby2elixir.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PlumberGirl takes care of your Elixir piping issues!\n\n\n[![Build status](https://travis-ci.org/ruby2elixir/plumber_girl.svg \"Build status\")](https://travis-ci.org/ruby2elixir/plumber_girl)\n[![Hex version](https://img.shields.io/hexpm/v/plumber_girl.svg \"Hex version\")](https://hex.pm/packages/plumber_girl)\n![Hex downloads](https://img.shields.io/hexpm/dt/plumber_girl.svg \"Hex downloads\")\n\n----\n\nPlumberGirl provides some macros to enable [railway-oriented programming](http://fsharpforfunandprofit.com/rop/) in Elixir.\n\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/plumber_girl/blob/master/test/plumber_girl_test.exs)\n\n\n## Sources for inspiration + copying\n- http://www.zohaib.me/railway-programming-pattern-in-elixir/\n- https://github.com/remiq/railway-oriented-programming-elixir\n- https://gist.github.com/zabirauf/17ced02bdf9829b6956e (Railway Oriented Programming macros in Elixir)\n\n\nInstallation\n------\n\n1. Add rop to your list of dependencies in `mix.exs`:\n```elixir\ndef deps do\n  [{:plumber_girl, \"~\u003e 0.9.5\"}]\nend\n```\n\n\n## Usage\nCall\n\n```elixir\nuse PlumberGirl\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 PlumberGirl\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 PlumberGirl\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 PlumberGirl\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 PlumberGirl\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 PlumberGirl\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://www.zohaib.me/railway-programming-pattern-in-elixir/\n- http://www.zohaib.me/monads-in-elixir-2/\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://fsharpforfunandprofit.com/posts/recipe-part2/\n- https://www.reddit.com/r/programming/comments/30coly/railway_oriented_programming_in_elixir/\n\n### Code (Railway 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- https://github.com/slogsdon/elixir-control.git\n- https://github.com/niahoo/monk.git\n- https://github.com/knrz/towel.git\n\n### Why Railway Oriented Programming might be simpler than Monads\n- https://speakerdeck.com/swlaschin/railway-oriented-programming-a-functional-approach-to-error-handling\n\n### Alternatives\n  - https://github.com/batate/elixir-pipes - Macros for more flexible composition with the Elixir Pipe operator\n  - https://github.com/vic/ok_jose - Pipe elixir functions that match {:ok,_}, {:error,_} tuples or custom patterns.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby2elixir%2Fplumber_girl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fruby2elixir%2Fplumber_girl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby2elixir%2Fplumber_girl/lists"}