callable_tree 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea52457dd99aca79ec45ac97716f53824b14dde4d479486b9d9fa6571a714c76
4
- data.tar.gz: 4fa5ee37a4a061b7c92f3373131b588bd30f12a351f344646dab2ec2d1089718
3
+ metadata.gz: 822f8080ff2652895cd3cb7255d856eea72290564dbe7054d93c2fca01ac6149
4
+ data.tar.gz: 783496e58325e0d5be2595b3c33b0acd1095c61400b89addc0ce9d8fc8e845db
5
5
  SHA512:
6
- metadata.gz: 5f6e91b80e81f89d0e8f1c15ac5043de2b100245232de6b3584e900d1378952ad91442e4a55f80c59cfe8a01e5a9b34cf277b3a213290cf0b5aa8e0985edc7ca
7
- data.tar.gz: 2a89b0e60761fa826c1e083b34f24138693c9f44743c0c3c018f6dcbf330a6471481421dc9c99b4f4d5ae2f410aaeec293bed4ed46f10f28482280ea582f895a
6
+ metadata.gz: '0381d33a9f1a82b524aa176574d24961e7585ebb4ea76185759be2d17d739b9c84612b0ed9f7f2323083a71515acbf6fa622b65528c5a903dd911147bebfb7c7'
7
+ data.tar.gz: 9b715d2b2fd6ca25c1bf8c446574a2097271525facc306198695d01fbadffd2e7823866e16c58c83cb8a7f08dc8047909023ecc8256d083f07854b9c714a57ac
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.2] - 2021-05-29
4
+
5
+ - Add `CallableTree::Node::Internal#compose` (experimental)
6
+
3
7
  ## [0.1.1] - 2021-05-27
4
8
 
5
9
  - Add `CallableTree::Node::Internal#broadcast` (experimental)
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- callable_tree (0.1.1)
4
+ callable_tree (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 and output all child nodes of the internal node, call it. Broadcast strategy ignores the `terminate?` method of the child node.
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.
@@ -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 statements.'
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 statements.'
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'] = 'https://github.com/jsmmr/ruby_callable_tree/blob/v#{version}/CHANGELOG.md'
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
@@ -13,6 +13,7 @@ Node::HooksSample.new
13
13
  input + 1
14
14
  end
15
15
  .append(
16
+ # anonymous external node
16
17
  lambda do |input, **options|
17
18
  puts "external input: #{input}"
18
19
  input * 2
@@ -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'
@@ -54,6 +54,16 @@ module CallableTree
54
54
  end
55
55
  end
56
56
 
57
+ def compose
58
+ if strategy.is_a?(Compose)
59
+ self
60
+ else
61
+ clone.tap do |node|
62
+ node.send(:strategy=, Compose.new)
63
+ end
64
+ end
65
+ end
66
+
57
67
  private
58
68
 
59
69
  attr_writer :children, :strategy
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CallableTree
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  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.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-27 00:00:00.000000000 Z
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 statements.
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/v#{version}/CHANGELOG.md
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 statements.
84
+ nested `if` or `case` expressions.
83
85
  test_files: []