arrows 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94df8a36337317e66fc6faaef9997ae62828e4d3
4
- data.tar.gz: 5876f8e38bbb58e3bc778b0c4733f3f189776535
3
+ metadata.gz: 87e62ace5a7ed0350a0f2933213138fbc4adf91d
4
+ data.tar.gz: b3d590924508a3a8ee58b1b83c4f45d9df39244c
5
5
  SHA512:
6
- metadata.gz: 16dbc25bd13d6a367b0ed47c700999819b2dec2c820356b1153cad35473203bedc082eb3dfb341f54e13daa83e193a9cf2e43e896b7e0d410f87211ab4531e23
7
- data.tar.gz: f8efbc83a05a6acefadcafe5f7e6cdf41088b5d9a07688b53246962360606aa92e5d193fb205294dc253dcf26037e86d9a9a283487267f602783f56bb2f055c6
6
+ metadata.gz: e8a05896a7bc8593eaa83585fb71ab2957daf737597873639ffbe8653f2a4596dc9bf2220ec587200bc6066a52d342f52c13fae33b33a2397e4fef0d4735681b
7
+ data.tar.gz: cbc2435c3618794b04fad75e7ebe38f2a1a5d2fbcdd4323768774e5c3cf359a3190a899cbf1eb34fe92999e01208f67108441227d1c8be0d52d014d38ef4722c
data/README.md CHANGED
@@ -18,6 +18,12 @@ x -> H -> z
18
18
 
19
19
  As in we pipe what F poops out into the mouth of G a la Human Centipede
20
20
 
21
+ ```ruby
22
+ f = Arrows.lift -> (apple) { apple.to_orange }
23
+ g = Arrows.lift -> (orange) { orange.to_kiwi }
24
+ apple_to_kiwi = f >> g
25
+ ```
26
+ ![Regular >> composition](https://raw.githubusercontent.com/foxnewsnetwork/arrows/master/pics/compose.mermaid.png)
21
27
 
22
28
  ### Applicative composition
23
29
  Calls map (in Haskell, they generalize it to fmap) on the data passed in
@@ -28,15 +34,64 @@ x -> F -> y and [x]
28
34
  Returns
29
35
  [x] -> F -> [z]
30
36
 
37
+ ```ruby
38
+ apples = Arrows.lift [apple, apple, apple]
39
+ apple_to_kiwi = Arrows.lift -> (apple) { apple.to_kiwi }
40
+ kiwis = apples >= apple_to_kiwi
41
+ ```
42
+ ![functor >= composition](https://raw.githubusercontent.com/foxnewsnetwork/arrows/master/pics/fmap.mermaid.png)
43
+
31
44
  ### Arrow fan out
32
45
  x -> y
33
46
  x -> z
34
47
  becomes
35
48
  x -> [y,z]
36
49
 
50
+ ```ruby
51
+ apple_to_orange = Arrows.lift -> (apple) { apple.to_orange }
52
+ apple_to_kiwi = Arrows.lift -> (apple) { apple.to_kiwi }
53
+ orange_and_kiwi = Arrows.lift(apple) >> (apple_to_orange / apple_to_kiwi)
54
+ ```
55
+ ![fanout / composition](https://raw.githubusercontent.com/foxnewsnetwork/arrows/master/pics/fanout.mermaid.png)
56
+
37
57
  ### Arrow Fork
38
58
  f ^ g produces a proc that takes in a Either, and if either is good, f is evaluated, if either is evil, g is evaluated
39
59
 
60
+ ```ruby
61
+ apple_to_orange = Arrows.lift -> (apple) { apple.to_orange }
62
+ apple_to_kiwi = Arrows.lift -> (apple) { apple.to_kiwi }
63
+ orange = Arrows.lift(apple) >> Arrows::Good >> (apple_to_orange ^ apple_to_kiwi)
64
+ kiwi = Arrows.lift(apple) >> Arrows::Evil >> (apple_to_orange ^ apple_to_kiwi)
65
+ ```
66
+ ![fork / composition](https://raw.githubusercontent.com/foxnewsnetwork/arrows/master/pics/fork.mermaid.png)
67
+
68
+ ### ArrowLoop
69
+ step <=> chose produces a proc that cycles between step and chose until chose returns a good either. Then returns whatever was in the good either
70
+
71
+ ```ruby
72
+ context '<=> feedback' do
73
+ let(:step) { Arrows.lift -> (arg_acc) { [arg_acc.first - 1, arg_acc.reduce(&:*)] } }
74
+ let(:chose) { Arrows.lift -> (arg_acc) { arg_acc.first < 1 ? Arrows.good(arg_acc.last) : Arrows.evil(arg_acc) } }
75
+ let(:factorial) { step <=> chose }
76
+ context '120' do
77
+ let(:one_twenty) { Arrows.lift(5) >> factorial }
78
+ subject { one_twenty.call }
79
+ specify { should eq 120 }
80
+ end
81
+ context '1' do
82
+ let(:one) { Arrows.lift(1) >> factorial }
83
+ subject { one.call }
84
+ specify { should eq 1 }
85
+ end
86
+ context '0' do
87
+ let(:zero) { Arrows.lift(0) >> factorial }
88
+ subject { zero.call }
89
+ specify { should eq 0 }
90
+ end
91
+ end
92
+ ```
93
+ ![feedback / composition](https://raw.githubusercontent.com/foxnewsnetwork/arrows/master/pics/feedback.mermaid.png)
94
+
40
95
  ## Memoization
41
96
  ```ruby
42
97
  @some_process = Arrows.lift -> (a) { a }
@@ -4,6 +4,17 @@ module Arrows
4
4
  require 'arrows/either'
5
5
  require 'arrows/proc'
6
6
  class << self
7
+ def feedback(merge, split)
8
+ Arrows::Proc.new do |args|
9
+ single = merge.call [args]
10
+ either = split.call single
11
+ while not either.good?
12
+ single = merge.call either.payload
13
+ either = split.call single
14
+ end
15
+ either.payload
16
+ end
17
+ end
7
18
  def fork(f,g)
8
19
  Arrows::Proc.new do |either|
9
20
  either.good? ? f[either.payload] : g[either.payload]
@@ -23,11 +34,11 @@ module Arrows
23
34
  def fmap(xs, f)
24
35
  Arrows::Proc.new { |args| xs[args].map { |x| f[x] } }
25
36
  end
26
- def good(x)
37
+ def good(x=nil)
27
38
  return x if x.respond_to?(:good?) && x.respond_to?(:payload)
28
39
  Arrows::Either.new true, x
29
40
  end
30
- def evil(x)
41
+ def evil(x=nil)
31
42
  return x if x.respond_to?(:good?) && x.respond_to?(:payload)
32
43
  Arrows::Either.new false, x
33
44
  end
@@ -7,4 +7,7 @@ class Arrows::Either
7
7
  def good?
8
8
  @good
9
9
  end
10
+ def evil?
11
+ not good?
12
+ end
10
13
  end
@@ -24,6 +24,11 @@ class Arrows::Proc < Proc
24
24
  Arrows.fork self, f
25
25
  end
26
26
 
27
+ # feedback (aka ArrowLoop) composition
28
+ def <=>(g)
29
+ Arrows.feedback self, g
30
+ end
31
+
27
32
  # Returns a memoizing version of this proc
28
33
  def memoize
29
34
  cache = {}
@@ -1,3 +1,3 @@
1
1
  module Arrows
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
@@ -0,0 +1,3 @@
1
+ graph LR;
2
+ A[apple "->" orange] --> B((compose))
3
+ B --> C[orange "->" kiwi];
Binary file
@@ -0,0 +1,5 @@
1
+ graph LR;
2
+ A[apple, orange] --> X((compose));
3
+ X --> B{concurrent};
4
+ B --> C[apple "->" pear];
5
+ B --> D[orange "->" kiwi];
@@ -0,0 +1,7 @@
1
+ graph LR;
2
+ A[apple] --> B((compose))
3
+ B --> C[apple "->" pear];
4
+ B --> D[apple "->" kiwi];
5
+ C --> E{fork};
6
+ D --> E;
7
+ E --> F[pear, kiwi];
Binary file
@@ -0,0 +1,7 @@
1
+ graph LR;
2
+ A[apple] --> B((compose));
3
+ B --> C[apple, mango "->" cherry];
4
+ C --> X((feedback));
5
+ X --> D[cherry "->" Either pear, mango];
6
+ D --> C;
7
+ D --> F[pear];
Binary file
@@ -0,0 +1,4 @@
1
+ graph LR;
2
+ A[apples] --> B((fmap));
3
+ B --> C[apple "->" pear];
4
+ C --> D[pears];
Binary file
@@ -0,0 +1,4 @@
1
+ graph LR;
2
+ C[apple "->" Either pear, kiwi] --> D{ fork };
3
+ D -->|good| E[pear];
4
+ D -->|evil| F(kiwi);
Binary file
@@ -29,6 +29,27 @@ RSpec.describe Arrows::Proc do
29
29
  end
30
30
  end
31
31
 
32
+ context '<=> feedback' do
33
+ let(:step) { Arrows.lift -> (arg_acc) { [arg_acc.first - 1, arg_acc.reduce(&:*)] } }
34
+ let(:chose) { Arrows.lift -> (arg_acc) { arg_acc.first < 1 ? Arrows.good(arg_acc.last) : Arrows.evil(arg_acc) } }
35
+ let(:factorial) { step <=> chose }
36
+ context '120' do
37
+ let(:one_twenty) { Arrows.lift(5) >> factorial }
38
+ subject { one_twenty.call }
39
+ specify { should eq 120 }
40
+ end
41
+ context '1' do
42
+ let(:one) { Arrows.lift(1) >> factorial }
43
+ subject { one.call }
44
+ specify { should eq 1 }
45
+ end
46
+ context '0' do
47
+ let(:zero) { Arrows.lift(0) >> factorial }
48
+ subject { zero.call }
49
+ specify { should eq 0 }
50
+ end
51
+ end
52
+
32
53
  context '>> composition' do
33
54
  let(:times2) { -> (x) { x * 2 } }
34
55
  let(:plus3) { -> (x) { x + 3 } }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arrows
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Chen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-23 00:00:00.000000000 Z
11
+ date: 2014-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,6 +72,18 @@ files:
72
72
  - lib/arrows/either.rb
73
73
  - lib/arrows/proc.rb
74
74
  - lib/arrows/version.rb
75
+ - pics/compose.mermaid
76
+ - pics/compose.mermaid.png
77
+ - pics/concurrent.mermaid
78
+ - pics/concurrent.mermaid.png
79
+ - pics/fanout.mermaid
80
+ - pics/fanout.mermaid.png
81
+ - pics/feedback.mermaid
82
+ - pics/feedback.mermaid.png
83
+ - pics/fmap.mermaid
84
+ - pics/fmap.mermaid.png
85
+ - pics/fork.mermaid
86
+ - pics/fork.mermaid.png
75
87
  - spec/arrows/proc_spec.rb
76
88
  - spec/spec_helper.rb
77
89
  homepage: http://github.com/foxnewsnetwork/arrows