yield_from 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e1e893e0ea1fa052f6fdf76b08c85403e02f260d
4
+ data.tar.gz: a16be25839897476a4b027b332942994ce039bd2
5
+ SHA512:
6
+ metadata.gz: e30379f79350448864118be13916a38787ae97b41f5c229412dc2b50dba96b182fe074e8d85d36cf869b20b7ced2d054cc1ebcaf10b3941d2c2ee76bb21f2883
7
+ data.tar.gz: fe05e132d38d7296461740f81e6bba0ee728f23809e6183fd4b2dc5be88f44495d1e6f6cfc936ff680ce3fe0828a0263eca5428a52c533ee28b6e885cfc25b04
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ - Copyright (C) @cielavenir
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this
7
+ list of conditions and the following disclaimer.
8
+ 2. Redistributions in binary form must reproduce the above copyright notice,
9
+ this list of conditions and the following disclaimer in the documentation
10
+ and/or other materials provided with the distribution.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
13
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
16
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
+
data/lib/yield_from.rb ADDED
@@ -0,0 +1,47 @@
1
+ =begin
2
+ (C) @cielavenir
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+ =end
24
+
25
+ def yield_from(*funcsyms)
26
+ funcsyms.each{|funcsym|
27
+ func = method(funcsym)
28
+ define_method(funcsym){|*args,&blk|
29
+ return to_enum(funcsym,*args) if !blk
30
+ func.(*args){|*a|a.each{|e|blk.(e)}}
31
+ }
32
+ private funcsym
33
+ }
34
+ end
35
+
36
+ module YieldFrom
37
+ VERSION = '0.0.0.1'
38
+ def yield_from(*funcsyms)
39
+ funcsyms.each{|funcsym|
40
+ func = instance_method(funcsym)
41
+ define_method(funcsym){|*args,&blk|
42
+ return to_enum(funcsym,*args) if !blk
43
+ func.bind(self).(*args){|*a|a.each{|e|blk.(e)}}
44
+ }
45
+ }
46
+ end
47
+ end
data/readme.md ADDED
@@ -0,0 +1,44 @@
1
+ ## yield_from: implementing `yield from func()` functionality by modifying `yield *func()` behavior
2
+
3
+ ### Usage
4
+
5
+ ```
6
+ require 'yield_from'
7
+ class A
8
+ extend YieldFrom
9
+ def rec(n)
10
+ return to_enum(:rec,n) if !block_given?
11
+ return if n<0
12
+ yield n
13
+ yield *rec(n-1)
14
+ end
15
+ yield_from :rec
16
+ end
17
+ p A.new.rec(5).to_a # => [5, 4, 3, 2, 1, 0]
18
+ ```
19
+
20
+ There are instance method version and local function version.
21
+
22
+ ### Motivation
23
+
24
+ In the above example, similar code was running until **Ruby 2.7** .
25
+
26
+ ```
27
+ class A
28
+ def rec(n)
29
+ return to_enum(:rec,n) if !block_given?
30
+ return if n<0
31
+ yield n
32
+ rec(n-1, &proc)
33
+ end
34
+ end
35
+ p A.new.rec(5).to_a # => [5, 4, 3, 2, 1, 0]
36
+ ```
37
+
38
+ **The bare proc got forbidden in Ruby 3.0.**
39
+
40
+ You can see the discussion at https://qiita.com/cielavenir/items/0cc9189f2c40d6047d8b .
41
+
42
+ ### Acknowledgement
43
+
44
+ Learned Ruby decorator from Nakayama R et al. Automatic Translation of Decorators from Python to Ruby, The 77th National Convention of IPSJ, 2015
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ RSpec.configure{|config|
3
+ config.color=true
4
+ }
5
+
6
+ require File.expand_path(File.dirname(__FILE__)+'/../lib/yield_from')
@@ -0,0 +1,77 @@
1
+ require File.expand_path(File.dirname(__FILE__)+'/spec_helper')
2
+
3
+ def rec(n)
4
+ return to_enum(:rec,n) if !block_given?
5
+ return if n<0
6
+ yield n
7
+ yield *rec(n-1)
8
+ end
9
+ yield_from :rec
10
+
11
+ describe 'local function' do
12
+ it 'yields recursively' do
13
+ rec(5).to_a.should eq [5,4,3,2,1,0]
14
+ end
15
+ end
16
+
17
+ class A
18
+ extend YieldFrom
19
+ def rec1(n)
20
+ return to_enum(:rec1,n) if !block_given?
21
+ return if n<=0
22
+ yield n
23
+ yield *rec2(n-1)
24
+ end
25
+ def rec2(n)
26
+ return to_enum(:rec2,n) if !block_given?
27
+ return if n<=0
28
+ yield n
29
+ yield *rec1(n/2)
30
+ end
31
+ yield_from :rec1, :rec2
32
+ end
33
+
34
+ describe 'instance method' do
35
+ it 'yields recursively' do
36
+ A.new.rec1(7).to_a.should eq [7,6,3,2,1]
37
+ end
38
+ end
39
+
40
+ H=Hash.new{|h,k|h[k]=[]}
41
+ n=10
42
+ root=1
43
+ roots=[*1..n]
44
+ 2.upto(n){|i|
45
+ H[i/2] << i
46
+ }
47
+ def preorder(node)
48
+ return to_enum(:preorder,node) if !block_given?
49
+ yield node
50
+ H[node].each{|e|yield *preorder(e) if e!=-1}
51
+ end
52
+ def inorder(node)
53
+ return to_enum(:inorder,node) if !block_given?
54
+ children=H[node]
55
+ yield *inorder(children[0]) if children[0]&&children[0]!=-1
56
+ yield node
57
+ yield *inorder(children[1]) if children[1]&&children[1]!=-1
58
+ end
59
+ def postorder(node)
60
+ return to_enum(:postorder,node) if !block_given?
61
+ H[node].each{|e|yield *postorder(e) if e!=-1}
62
+ yield node
63
+ end
64
+ yield_from :preorder, :inorder, :postorder
65
+
66
+ describe 'complex case' do
67
+ specify 'preorder' do
68
+ preorder(root).to_a.should eq [1, 2, 4, 8, 9, 5, 10, 3, 6, 7]
69
+ end
70
+ specify 'inorder' do
71
+ inorder(root).to_a.should eq [8, 4, 9, 2, 10, 5, 1, 6, 3, 7]
72
+ end
73
+ specify 'postorder' do
74
+ postorder(root).to_a.should eq [8, 9, 4, 10, 5, 2, 6, 7, 3, 1]
75
+ end
76
+ end
77
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ require './lib/yield_from'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "yield_from"
6
+ spec.version = YieldFrom::VERSION
7
+ spec.authors = ["cielavenir"]
8
+ spec.email = ["cielartisan@gmail.com"]
9
+ spec.description = "implementing yield from func() functionality by modifying yield *func() behavior"
10
+ spec.summary = "implementing yield from func() functionality by modifying yield *func() behavior"
11
+ spec.homepage = "http://github.com/cielavenir/yield_from"
12
+ spec.license = "Ruby License (2-clause BSDL or Artistic)"
13
+
14
+ spec.files = `git ls-files`.split($/) + [
15
+ "LICENSE.txt",
16
+ "readme.md",
17
+ #"CHANGELOG.md",
18
+ ]
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", ">= 1.0"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yield_from
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - cielavenir
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-15 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.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: implementing yield from func() functionality by modifying yield *func()
56
+ behavior
57
+ email:
58
+ - cielartisan@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - LICENSE.txt
64
+ - lib/yield_from.rb
65
+ - readme.md
66
+ - spec/spec_helper.rb
67
+ - spec/yield_from_spec.rb
68
+ - yield_from.gemspec
69
+ homepage: http://github.com/cielavenir/yield_from
70
+ licenses:
71
+ - Ruby License (2-clause BSDL or Artistic)
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.5.2.1
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: implementing yield from func() functionality by modifying yield *func() behavior
93
+ test_files:
94
+ - spec/spec_helper.rb
95
+ - spec/yield_from_spec.rb