1
1
# ROP - Railway Oriented Programming in Elixir
2
2
3
-
4
3
[ ![ Build Status] ( https://travis-ci.org/ruby2elixir/rop.png )] ( https://travis-ci.org/ruby2elixir/rop )
5
4
6
5
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
+
7
101
## Installation
8
102
9
103
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
14
108
[{:rop, "~> 0.5.0"}]
15
109
end
16
110
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
-
28
111
29
112
### Some discussions about Railsway programming:
113
+
30
114
- http://insights.workshop14.io/2015/10/18/handling-errors-in-elixir-no-one-say-monad.html
31
115
- http://blog.danielberkompas.com/2015/09/03/better-pipelines-with-monadex.html
32
116
- 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
42
126
- https://github.com/rob-brown/MonadEx.git
43
127
44
128
### Code (Railsway Programming)
45
-
46
- - https://github.com/CrowdHailer/OK/blob/master/lib/ok.ex
47
129
- https://github.com/remiq/railway-oriented-programming-elixir/blob/master/lib/rop.ex
48
130
- https://gist.github.com/zabirauf/17ced02bdf9829b6956e (Railway Oriented Programming macros in Elixir) -> Rop
131
+ - https://github.com/CrowdHailer/OK/blob/master/lib/ok.ex
49
132
- https://gist.github.com/danielberkompas/52216db76d764a68dfa3 -> pipeline.ex
0 commit comments