callable_tree 0.1.1 → 0.1.2
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 +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +56 -1
- data/callable_tree.gemspec +3 -3
- data/examples/example5.rb +1 -0
- data/examples/example7.rb +31 -0
- data/lib/callable_tree.rb +1 -0
- data/lib/callable_tree/node/internal.rb +10 -0
- data/lib/callable_tree/node/internal/broadcast.rb +1 -5
- data/lib/callable_tree/node/internal/compose.rb +19 -0
- data/lib/callable_tree/version.rb +1 -1
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 822f8080ff2652895cd3cb7255d856eea72290564dbe7054d93c2fca01ac6149
|
4
|
+
data.tar.gz: 783496e58325e0d5be2595b3c33b0acd1095c61400b89addc0ce9d8fc8e845db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0381d33a9f1a82b524aa176574d24961e7585ebb4ea76185759be2d17d739b9c84612b0ed9f7f2323083a71515acbf6fa622b65528c5a903dd911147bebfb7c7'
|
7
|
+
data.tar.gz: 9b715d2b2fd6ca25c1bf8c446574a2097271525facc306198695d01fbadffd2e7823866e16c58c83cb8a7f08dc8047909023ecc8256d083f07854b9c714a57ac
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -470,6 +470,7 @@ Node::HooksSample.new
|
|
470
470
|
input + 1
|
471
471
|
end
|
472
472
|
.append(
|
473
|
+
# anonymous external node
|
473
474
|
lambda do |input, **options|
|
474
475
|
puts "external input: #{input}"
|
475
476
|
input * 2
|
@@ -505,7 +506,7 @@ result: 16
|
|
505
506
|
|
506
507
|
#### `CallableTree::Node::Internal#broadcast` (experimental)
|
507
508
|
|
508
|
-
If you want to call
|
509
|
+
If you want to call all child nodes of the internal node in order to output their results as array, call it. The `broadcast` strategy ignores the `terminate?` method of the nodes.
|
509
510
|
|
510
511
|
`examples/example6.rb`:
|
511
512
|
```ruby
|
@@ -538,6 +539,60 @@ Run `examples/example6.rb`:
|
|
538
539
|
---
|
539
540
|
```
|
540
541
|
|
542
|
+
#### `CallableTree::Node::Internal#compose` (experimental)
|
543
|
+
|
544
|
+
If you want to call all child nodes of the internal node in order to input the output of the previous node to the next node and output a single result , call it. The `compose` strategy ignores the `terminate?` method of the nodes.
|
545
|
+
|
546
|
+
`examples/example7.rb`:
|
547
|
+
```ruby
|
548
|
+
module Node
|
549
|
+
class LessThan
|
550
|
+
include CallableTree::Node::Internal
|
551
|
+
|
552
|
+
def initialize(num)
|
553
|
+
@num = num
|
554
|
+
end
|
555
|
+
|
556
|
+
def match?(input)
|
557
|
+
super && input < @num
|
558
|
+
end
|
559
|
+
end
|
560
|
+
end
|
561
|
+
|
562
|
+
tree = CallableTree::Node::Root.new.append(
|
563
|
+
Node::LessThan.new(5).append(
|
564
|
+
proc { |input| input * 2 }, # anonymous external node
|
565
|
+
proc { |input| input + 1 } # anonymous external node
|
566
|
+
).compose,
|
567
|
+
Node::LessThan.new(10).append(
|
568
|
+
proc { |input| input * 3 }, # anonymous external node
|
569
|
+
proc { |input| input - 1 } # anonymous external node
|
570
|
+
).compose
|
571
|
+
).compose
|
572
|
+
|
573
|
+
(0..10).each do |input|
|
574
|
+
output = tree.call(input)
|
575
|
+
puts "#{input} -> #{output}"
|
576
|
+
end
|
577
|
+
|
578
|
+
```
|
579
|
+
|
580
|
+
Run `examples/example7.rb`:
|
581
|
+
```sh
|
582
|
+
% ruby examples/example7.rb
|
583
|
+
0 -> 2
|
584
|
+
1 -> 8
|
585
|
+
2 -> 14
|
586
|
+
3 -> 20
|
587
|
+
4 -> 26
|
588
|
+
5 -> 14
|
589
|
+
6 -> 17
|
590
|
+
7 -> 20
|
591
|
+
8 -> 23
|
592
|
+
9 -> 26
|
593
|
+
10 -> 10
|
594
|
+
```
|
595
|
+
|
541
596
|
## Contributing
|
542
597
|
|
543
598
|
Bug reports and pull requests are welcome on GitHub at https://github.com/jsmmr/callable_tree.
|
data/callable_tree.gemspec
CHANGED
@@ -8,15 +8,15 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ['jsmmr']
|
9
9
|
spec.email = ['jsmmr@icloud.com']
|
10
10
|
|
11
|
-
spec.summary = 'Builds a tree by linking callable nodes. The nodes that match the calling condition are called in a chain from the root node to the leaf node. These are like nested case
|
12
|
-
spec.description = 'Builds a tree by linking callable nodes. The nodes that match the calling condition are called in a chain from the root node to the leaf node. These are like nested case
|
11
|
+
spec.summary = 'Builds a tree by linking callable nodes. The nodes that match the calling condition are called in a chain from the root node to the leaf node. These are like nested `if` or `case` expressions.'
|
12
|
+
spec.description = 'Builds a tree by linking callable nodes. The nodes that match the calling condition are called in a chain from the root node to the leaf node. These are like nested `if` or `case` expressions.'
|
13
13
|
spec.homepage = 'https://github.com/jsmmr/ruby_callable_tree'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
|
16
16
|
|
17
17
|
spec.metadata['homepage_uri'] = spec.homepage
|
18
18
|
spec.metadata['source_code_uri'] = 'https://github.com/jsmmr/ruby_callable_tree'
|
19
|
-
spec.metadata['changelog_uri'] =
|
19
|
+
spec.metadata['changelog_uri'] = "https://github.com/jsmmr/ruby_callable_tree/blob/v#{spec.version}/CHANGELOG.md"
|
20
20
|
|
21
21
|
# Specify which files should be added to the gem when it is released.
|
22
22
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
data/examples/example5.rb
CHANGED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'callable_tree'
|
2
|
+
|
3
|
+
module Node
|
4
|
+
class LessThan
|
5
|
+
include CallableTree::Node::Internal
|
6
|
+
|
7
|
+
def initialize(num)
|
8
|
+
@num = num
|
9
|
+
end
|
10
|
+
|
11
|
+
def match?(input)
|
12
|
+
super && input < @num
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
tree = CallableTree::Node::Root.new.append(
|
18
|
+
Node::LessThan.new(5).append(
|
19
|
+
proc { |input| input * 2 }, # anonymous external node
|
20
|
+
proc { |input| input + 1 } # anonymous external node
|
21
|
+
).compose,
|
22
|
+
Node::LessThan.new(10).append(
|
23
|
+
proc { |input| input * 3 }, # anonymous external node
|
24
|
+
proc { |input| input - 1 } # anonymous external node
|
25
|
+
).compose
|
26
|
+
).compose
|
27
|
+
|
28
|
+
(0..10).each do |input|
|
29
|
+
output = tree.call(input)
|
30
|
+
puts "#{input} -> #{output}"
|
31
|
+
end
|
data/lib/callable_tree.rb
CHANGED
@@ -10,6 +10,7 @@ require_relative 'callable_tree/node'
|
|
10
10
|
require_relative 'callable_tree/node/hooks/call'
|
11
11
|
require_relative 'callable_tree/node/internal/broadcast'
|
12
12
|
require_relative 'callable_tree/node/internal/seek'
|
13
|
+
require_relative 'callable_tree/node/internal/compose'
|
13
14
|
require_relative 'callable_tree/node/external/verbose'
|
14
15
|
require_relative 'callable_tree/node/external'
|
15
16
|
require_relative 'callable_tree/node/internal'
|
@@ -6,11 +6,7 @@ module CallableTree
|
|
6
6
|
class Broadcast
|
7
7
|
def call(nodes, input:, options:)
|
8
8
|
nodes.map do |node|
|
9
|
-
if node.match?(input, **options)
|
10
|
-
node.call(input, **options)
|
11
|
-
else
|
12
|
-
nil
|
13
|
-
end
|
9
|
+
node.call(input, **options) if node.match?(input, **options)
|
14
10
|
end
|
15
11
|
end
|
16
12
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CallableTree
|
4
|
+
module Node
|
5
|
+
module Internal
|
6
|
+
class Compose
|
7
|
+
def call(nodes, input:, options:)
|
8
|
+
nodes.reduce(input) do |input, node|
|
9
|
+
if node.match?(input, **options)
|
10
|
+
node.call(input, **options)
|
11
|
+
else
|
12
|
+
input
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: callable_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jsmmr
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-05-
|
11
|
+
date: 2021-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Builds a tree by linking callable nodes. The nodes that match the calling
|
14
14
|
condition are called in a chain from the root node to the leaf node. These are like
|
15
|
-
nested case
|
15
|
+
nested `if` or `case` expressions.
|
16
16
|
email:
|
17
17
|
- jsmmr@icloud.com
|
18
18
|
executables: []
|
@@ -42,6 +42,7 @@ files:
|
|
42
42
|
- examples/example4.rb
|
43
43
|
- examples/example5.rb
|
44
44
|
- examples/example6.rb
|
45
|
+
- examples/example7.rb
|
45
46
|
- lib/callable_tree.rb
|
46
47
|
- lib/callable_tree/node.rb
|
47
48
|
- lib/callable_tree/node/external.rb
|
@@ -49,6 +50,7 @@ files:
|
|
49
50
|
- lib/callable_tree/node/hooks/call.rb
|
50
51
|
- lib/callable_tree/node/internal.rb
|
51
52
|
- lib/callable_tree/node/internal/broadcast.rb
|
53
|
+
- lib/callable_tree/node/internal/compose.rb
|
52
54
|
- lib/callable_tree/node/internal/seek.rb
|
53
55
|
- lib/callable_tree/node/root.rb
|
54
56
|
- lib/callable_tree/version.rb
|
@@ -58,7 +60,7 @@ licenses:
|
|
58
60
|
metadata:
|
59
61
|
homepage_uri: https://github.com/jsmmr/ruby_callable_tree
|
60
62
|
source_code_uri: https://github.com/jsmmr/ruby_callable_tree
|
61
|
-
changelog_uri: https://github.com/jsmmr/ruby_callable_tree/blob/
|
63
|
+
changelog_uri: https://github.com/jsmmr/ruby_callable_tree/blob/v0.1.2/CHANGELOG.md
|
62
64
|
post_install_message:
|
63
65
|
rdoc_options: []
|
64
66
|
require_paths:
|
@@ -79,5 +81,5 @@ signing_key:
|
|
79
81
|
specification_version: 4
|
80
82
|
summary: Builds a tree by linking callable nodes. The nodes that match the calling
|
81
83
|
condition are called in a chain from the root node to the leaf node. These are like
|
82
|
-
nested case
|
84
|
+
nested `if` or `case` expressions.
|
83
85
|
test_files: []
|