Skip to content

Commit 56264c9

Browse files
committed
some docs in the readme
1 parent 487ee83 commit 56264c9

File tree

1 file changed

+97
-14
lines changed

1 file changed

+97
-14
lines changed

README.md

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,103 @@
11
# ROP - Railway Oriented Programming in Elixir
22

3-
43
[![Build Status](https://travis-ci.org/ruby2elixir/rop.png)](https://travis-ci.org/ruby2elixir/rop)
54

65

6+
Some macros to enable railsway-oriented programming in Elixir.
7+
Collected from code snippets and wrapped into a simple library for your convenience.
8+
9+
10+
For more examples please check the tests here:
11+
- [Examples](https://github.com/ruby2elixir/rop/blob/master/test/rop_test.exs)
12+
13+
14+
### Sources for inspiration + copying
15+
- https://github.com/remiq/railway-oriented-programming-elixir
16+
- https://gist.github.com/zabirauf/17ced02bdf9829b6956e (Railway Oriented Programming macros in Elixir)
17+
18+
19+
### Usage
20+
21+
Call `use Rop` into your module. That will give you access to following macros/functions:
22+
23+
#### `>>>` - No need to stop pipelining in case of an error somewhere in the middle
24+
25+
26+
used like: `1 |> fn1 >>> fn2 >>> fn3 >>> fn4`
27+
28+
```elixir
29+
defmodule A do
30+
use Rop
31+
def tagged_inc(v) do
32+
IO.puts "inc for #{v}" # sideeffect for demonstration
33+
{:ok, v + 1}
34+
end
35+
36+
def error_fn(_) do
37+
{:error, "I'm a bad fn!"}
38+
end
39+
40+
def raising_fn(_) do
41+
raise "I'm raising!"
42+
end
43+
44+
def result do
45+
1 |> tagged_inc >>> tagged_inc >>> tagged_inc
46+
end
47+
48+
def error_result do
49+
1 |> tagged_inc >>> tagged_inc >>> error_fn >>> tagged_inc
50+
end
51+
52+
def raising_result do
53+
1 |> tagged_inc >>> tagged_inc >>> raising_fn >>> tagged_inc
54+
end
55+
end
56+
57+
iex> A.result
58+
inc for 1
59+
inc for 2
60+
inc for 3
61+
{:ok, 4}
62+
63+
### increases twice, errors and tries to increase again
64+
### notice that after errored result we don't execute any function anymore in the pipeline, e.g. only tagged_inc before error_fn were executed.
65+
iex> A.error_result
66+
inc for 1
67+
inc for 2
68+
{:error, "I'm a bad fn!"}
69+
70+
iex> A.raising_result
71+
** (RuntimeError) I'm raising!
72+
```
73+
74+
75+
76+
#### `bind`
77+
Wraps a simple function to return a tagged tuple with `:ok` to comply to the protocol {:ok, result}: e.g.
78+
79+
```elixir
80+
defmodule B do
81+
use Rop
82+
def inc(v) do
83+
v + 1
84+
end
85+
86+
def only_last_pipe_tagged_result do
87+
2 |> inc |> bind(inc)
88+
end
89+
90+
def result_fully_tagged do
91+
2 |> bind(inc) >>> bind(inc) >>> bind(inc)
92+
end
93+
end
94+
iex> B.only_last_pipe_tagged_result
95+
{:ok, 4}
96+
97+
iex> B.result_fully_tagged
98+
{:ok, 5}
99+
´´´
100+
7101
## Installation
8102
9103
If [available in Hex](https://hex.pm/docs/publish), the package can be installed as:
@@ -14,19 +108,9 @@ If [available in Hex](https://hex.pm/docs/publish), the package can be installed
14108
[{:rop, "~> 0.5.0"}]
15109
end
16110
17-
2. Ensure rop is started before your application:
18-
19-
def application do
20-
[applications: [:rop]]
21-
end
22-
23-
24-
### Sources for inspiration + copying
25-
- https://github.com/remiq/railway-oriented-programming-elixir
26-
- https://gist.github.com/zabirauf/17ced02bdf9829b6956e (Railway Oriented Programming macros in Elixir)
27-
28111
29112
### Some discussions about Railsway programming:
113+
30114
- http://insights.workshop14.io/2015/10/18/handling-errors-in-elixir-no-one-say-monad.html
31115
- http://blog.danielberkompas.com/2015/09/03/better-pipelines-with-monadex.html
32116
- http://onor.io/2015/08/27/railway-oriented-programming-in-elixir/
@@ -42,8 +126,7 @@ If [available in Hex](https://hex.pm/docs/publish), the package can be installed
42126
- https://github.com/rob-brown/MonadEx.git
43127
44128
### Code (Railsway Programming)
45-
46-
- https://github.com/CrowdHailer/OK/blob/master/lib/ok.ex
47129
- https://github.com/remiq/railway-oriented-programming-elixir/blob/master/lib/rop.ex
48130
- https://gist.github.com/zabirauf/17ced02bdf9829b6956e (Railway Oriented Programming macros in Elixir) -> Rop
131+
- https://github.com/CrowdHailer/OK/blob/master/lib/ok.ex
49132
- https://gist.github.com/danielberkompas/52216db76d764a68dfa3 -> pipeline.ex

0 commit comments

Comments
 (0)