tiny_frp 0.0.1
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 +7 -0
- data/.gitignore +16 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +32 -0
- data/Rakefile +8 -0
- data/lib/tiny_frp/version.rb +3 -0
- data/lib/tiny_frp.rb +138 -0
- data/tiny_frp.gemspec +24 -0
- data/unit_test/test_tiny_frp.rb +83 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7b7543c7cb87158728c311de095ecabe8efe772f
|
4
|
+
data.tar.gz: 6adb9c7b7e19c7ad4c5c42dd834b291055f10f27
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5512cda0b886039366ba73b45d78fe08f61cadfb297365a3e1eb34bce747ee80ab97749c99b1b75f4d19d13544bd29e12ad2d5eb5f46fbc2009daaf98de3f3e8
|
7
|
+
data.tar.gz: 9865e8ae70843349b993b15e353f3a644b46bb675f9c707437f89338ee2cf4ea797321ee21113beea564d9cd7d9209908ab1f7b16a8c37fca2444d22dbc27783
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 sawaken
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# TinyFrp
|
2
|
+
|
3
|
+
Very small and simple library to do (functional)-reactive-programming in Ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'frp'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install frp
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it ( https://github.com/sawaken/tiny-frp/fork )
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/tiny_frp.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
require "tiny_frp/version"
|
2
|
+
|
3
|
+
module TinyFRP
|
4
|
+
class Runtime
|
5
|
+
def initialize(signal)
|
6
|
+
@parents = Runtime.make_map(signal)
|
7
|
+
@children = Runtime.traverse(signal)
|
8
|
+
@inputs = @parents.keys.select{|node| @parents[node] == []}
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.make_map(signal)
|
12
|
+
signal.assigned_signals.inject({}){|h, s|
|
13
|
+
h.merge(make_map(s))
|
14
|
+
}.merge(signal.node => signal.assigned_signals.map{|s| s.node})
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.traverse(signal)
|
18
|
+
signal.assigned_signals.each_with_index.inject({signal.node =>[]}) do |h, a|
|
19
|
+
s, i = *a
|
20
|
+
edges = traverse(s).merge(s.node => [[i, signal.node]]){|k, old, new| (old + new).uniq}
|
21
|
+
h.merge(edges){|k, old, new| (old + new).uniq}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def conduct
|
26
|
+
received = Hash.new([])
|
27
|
+
queue = Array.new(@inputs)
|
28
|
+
until queue.empty?
|
29
|
+
node = queue.shift
|
30
|
+
val = node.call(*received[node].sort{|a, b| a[0] <=> b[0]}.map{|idx, val| val})
|
31
|
+
@children[node].each do |idx, ch_node|
|
32
|
+
received[ch_node] = received[ch_node] + [[idx, val]]
|
33
|
+
if received[ch_node].size == @parents[ch_node].size
|
34
|
+
queue << ch_node
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
return val
|
39
|
+
end
|
40
|
+
|
41
|
+
def run(mode=:loop, &proc)
|
42
|
+
case mode
|
43
|
+
when :loop
|
44
|
+
loop{
|
45
|
+
v = conduct
|
46
|
+
proc.call(v) if proc
|
47
|
+
}
|
48
|
+
when 0
|
49
|
+
[]
|
50
|
+
when 1
|
51
|
+
[conduct]
|
52
|
+
when Integer
|
53
|
+
[conduct] + run(mode - 1)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class Signal
|
59
|
+
attr_reader :node, :assigned_signals
|
60
|
+
def initialize(node, assigned_signals)
|
61
|
+
@node, @assigned_signals = node, assigned_signals
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class Node
|
66
|
+
attr_reader :fixed_argc
|
67
|
+
|
68
|
+
def >>(node)
|
69
|
+
node.apply([self])
|
70
|
+
end
|
71
|
+
|
72
|
+
def apply(args)
|
73
|
+
signals = args.map{|s| s.is_a?(Node) && s.fixed_argc == 0 ? Signal.new(s, []) : s}
|
74
|
+
if signals.any?{|s| !s.is_a?(Signal)}
|
75
|
+
raise "Error: cannot apply SignalFunction to Non-Signal Object."
|
76
|
+
end
|
77
|
+
if signals.size < @fixed_argc || (!@rest_arg && signals.size != @fixed_argc)
|
78
|
+
raise "Error: cannot apply SignalFunction to Signals."
|
79
|
+
end
|
80
|
+
Signal.new(self, signals)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class Foldp < Node
|
85
|
+
def initialize(initial_state, proc, fixed_argc, rest_arg)
|
86
|
+
@state, @proc = initial_state, proc
|
87
|
+
@fixed_argc, @rest_arg = fixed_argc, rest_arg
|
88
|
+
end
|
89
|
+
|
90
|
+
def update(diff)
|
91
|
+
@state.keys.map{|k| [k, diff[k] || @state[k]]}.to_h
|
92
|
+
end
|
93
|
+
|
94
|
+
def call(*raw_args)
|
95
|
+
@state = update(@proc.call(@state, *raw_args))
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class Lift < Node
|
100
|
+
def initialize(proc, fixed_argc, rest_arg=false)
|
101
|
+
@proc, @fixed_argc, @rest_arg = proc, fixed_argc, rest_arg
|
102
|
+
end
|
103
|
+
|
104
|
+
def call(*raw_args)
|
105
|
+
@proc.call(*raw_args)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
class Bundler
|
110
|
+
def initialize(signals)
|
111
|
+
@signals = signals
|
112
|
+
end
|
113
|
+
|
114
|
+
def >>(node)
|
115
|
+
node.apply(@signals)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.foldp(initial_state, &proc)
|
120
|
+
fixed_argc = proc.parameters.select{|t, n| t == :opt}.count - 1
|
121
|
+
rest_arg = proc.parameters.any?{|t, n| t == :rest}
|
122
|
+
Foldp.new(initial_state, proc, fixed_argc, rest_arg)
|
123
|
+
end
|
124
|
+
|
125
|
+
def self.bundle(*signals)
|
126
|
+
Bundler.new(signals)
|
127
|
+
end
|
128
|
+
|
129
|
+
def self.lift(&proc)
|
130
|
+
fixed_argc = proc.parameters.select{|t, n| t == :opt}.count
|
131
|
+
rest_arg = proc.parameters.any?{|t, n| t == :rest}
|
132
|
+
Lift.new(proc, fixed_argc, rest_arg)
|
133
|
+
end
|
134
|
+
|
135
|
+
def self.bottom
|
136
|
+
Lift.new(proc{|*_| nil}, 0, true)
|
137
|
+
end
|
138
|
+
end
|
data/tiny_frp.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- mode: ruby -*-
|
2
|
+
# coding: utf-8
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'tiny_frp/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "tiny_frp"
|
9
|
+
spec.version = TinyFRP::VERSION
|
10
|
+
spec.authors = ["sawaken"]
|
11
|
+
spec.email = ["sasasawada@gmail.com"]
|
12
|
+
spec.summary = %q{Very small and simple library to do (functional)-reactive-programming in Ruby.}
|
13
|
+
spec.description = %q{}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
$LIBRARY_ROOT_PATH = File.dirname(File.expand_path(File.dirname(__FILE__)))
|
2
|
+
|
3
|
+
module TinyFRP
|
4
|
+
module UnitTest
|
5
|
+
require 'test/unit'
|
6
|
+
require $LIBRARY_ROOT_PATH + '/lib/tiny_frp.rb'
|
7
|
+
|
8
|
+
class RuntimeTest < Test::Unit::TestCase
|
9
|
+
def test_make_map
|
10
|
+
s1 = Signal.new(:node_a, [])
|
11
|
+
s2 = Signal.new(:node_b, [])
|
12
|
+
s3 = Signal.new(:node_c, [s1, s2])
|
13
|
+
s4 = Signal.new(:node_d, [s3])
|
14
|
+
s5 = Signal.new(:node_e, [s3])
|
15
|
+
s6 = Signal.new(:node_f, [s4, s5])
|
16
|
+
ps = Runtime.make_map(s6)
|
17
|
+
|
18
|
+
assert_equal([], ps[:node_a])
|
19
|
+
assert_equal([], ps[:node_b])
|
20
|
+
assert_equal([:node_a, :node_b], ps[:node_c])
|
21
|
+
assert_equal([:node_c], ps[:node_d])
|
22
|
+
assert_equal([:node_c], ps[:node_e])
|
23
|
+
assert_equal([:node_d, :node_e], ps[:node_f])
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_traverse
|
27
|
+
s1 = Signal.new(:node_a, [])
|
28
|
+
s2 = Signal.new(:node_b, [])
|
29
|
+
s3 = Signal.new(:node_c, [s1, s2])
|
30
|
+
s4 = Signal.new(:node_d, [s3])
|
31
|
+
s5 = Signal.new(:node_e, [s3])
|
32
|
+
s6 = Signal.new(:node_f, [s4, s5])
|
33
|
+
cs = Runtime.traverse(s6)
|
34
|
+
|
35
|
+
assert_equal(1, cs[:node_a].size)
|
36
|
+
assert(cs[:node_a].include? [0, :node_c])
|
37
|
+
|
38
|
+
assert_equal(1, cs[:node_b].size)
|
39
|
+
assert(cs[:node_b].include? [1, :node_c])
|
40
|
+
|
41
|
+
assert_equal(2, cs[:node_c].size)
|
42
|
+
assert(cs[:node_c].include? [0, :node_d])
|
43
|
+
assert(cs[:node_c].include? [0, :node_e])
|
44
|
+
|
45
|
+
assert_equal(1, cs[:node_d].size)
|
46
|
+
assert(cs[:node_d].include? [0, :node_f])
|
47
|
+
|
48
|
+
assert_equal(1, cs[:node_e].size)
|
49
|
+
assert(cs[:node_e].include? [1, :node_f])
|
50
|
+
|
51
|
+
assert_equal(0, cs[:node_f].size)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_conduct
|
55
|
+
v1, v2 = 0, 1
|
56
|
+
s1 = Signal.new(proc{ v1 = v1 + 1}, [])
|
57
|
+
s2 = Signal.new(proc{ v2 = v2 * 2}, [])
|
58
|
+
s3 = Signal.new(proc{|a, b| a + b}, [s1, s2])
|
59
|
+
|
60
|
+
assert_equal([1+2, 2+4, 3+8, 4+16, 5+32], Runtime.new(s3).run(5))
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_lift
|
64
|
+
v1, v2 = 0, 1
|
65
|
+
l1 = TinyFRP.lift{v1 = v1 + 1}
|
66
|
+
l2 = TinyFRP.lift{v2 = v2 * 2}
|
67
|
+
signal = TinyFRP.bundle(l1, l2) >> TinyFRP.lift{|a, b| a + b}
|
68
|
+
|
69
|
+
assert_equal([1+2, 2+4, 3+8, 4+16, 5+32], Runtime.new(signal).run(5))
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_foldp
|
73
|
+
f1 = TinyFRP.foldp(val: 0){|acc| {val: acc[:val] + 1}}
|
74
|
+
f2 = TinyFRP.foldp(val: 1){|acc| {val: acc[:val] * 2}}
|
75
|
+
signal = TinyFRP.bundle(f1, f2) >> TinyFRP.lift{|a, b| a[:val] + b[:val]}
|
76
|
+
|
77
|
+
assert_equal([1+2, 2+4, 3+8, 4+16, 5+32], Runtime.new(signal).run(5))
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tiny_frp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- sawaken
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: ''
|
42
|
+
email:
|
43
|
+
- sasasawada@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/tiny_frp.rb
|
54
|
+
- lib/tiny_frp/version.rb
|
55
|
+
- tiny_frp.gemspec
|
56
|
+
- unit_test/test_tiny_frp.rb
|
57
|
+
homepage: ''
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.2.2
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Very small and simple library to do (functional)-reactive-programming in
|
81
|
+
Ruby.
|
82
|
+
test_files: []
|