iolite 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.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +24 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +70 -0
  8. data/Rakefile +7 -0
  9. data/docs/iolite.md +278 -0
  10. data/example/array.rb +17 -0
  11. data/example/fizzbuzz.rb +15 -0
  12. data/example/hash.rb +14 -0
  13. data/example/minimal_implement.rb +42 -0
  14. data/example/simple.rb +46 -0
  15. data/example/to_lazy.rb +37 -0
  16. data/iolite.gemspec +24 -0
  17. data/lib/iolite.rb +14 -0
  18. data/lib/iolite/adaptor.rb +2 -0
  19. data/lib/iolite/adaptor/all.rb +22 -0
  20. data/lib/iolite/adaptor/apply.rb +10 -0
  21. data/lib/iolite/adaptor/bind.rb +9 -0
  22. data/lib/iolite/adaptor/callable.rb +7 -0
  23. data/lib/iolite/adaptor/define_send_original_methods.rb +12 -0
  24. data/lib/iolite/adaptor/method_missing.rb +10 -0
  25. data/lib/iolite/adaptor/operators.rb +26 -0
  26. data/lib/iolite/adaptor/send.rb +11 -0
  27. data/lib/iolite/adaptor/to_lazy.rb +11 -0
  28. data/lib/iolite/adaptor/to_proc.rb +10 -0
  29. data/lib/iolite/adaptored/array.rb +12 -0
  30. data/lib/iolite/adaptored/hash.rb +14 -0
  31. data/lib/iolite/adaptored/iolite_lazy_with_hash.rb +7 -0
  32. data/lib/iolite/adaptored/object_with_to_lazy.rb +5 -0
  33. data/lib/iolite/adaptored/proc.rb +5 -0
  34. data/lib/iolite/adaptored/string.rb +21 -0
  35. data/lib/iolite/functinal.rb +4 -0
  36. data/lib/iolite/functinal/bind.rb +12 -0
  37. data/lib/iolite/functinal/define_iolite_functinal_send_method.rb +9 -0
  38. data/lib/iolite/functinal/invoke.rb +11 -0
  39. data/lib/iolite/functinal/send.rb +13 -0
  40. data/lib/iolite/lazy.rb +40 -0
  41. data/lib/iolite/placeholders.rb +30 -0
  42. data/lib/iolite/refinements.rb +6 -0
  43. data/lib/iolite/refinements/array.rb +16 -0
  44. data/lib/iolite/refinements/hash.rb +18 -0
  45. data/lib/iolite/refinements/object_with_to_lazy.rb +9 -0
  46. data/lib/iolite/refinements/proc.rb +9 -0
  47. data/lib/iolite/refinements/string.rb +26 -0
  48. data/lib/iolite/statement.rb +3 -0
  49. data/lib/iolite/statement/if.rb +48 -0
  50. data/lib/iolite/statement/if_else.rb +11 -0
  51. data/lib/iolite/version.rb +3 -0
  52. data/spec/iolite_adaptored_spec.rb +101 -0
  53. data/spec/iolite_functinal_spec.rb +87 -0
  54. data/spec/iolite_lazy_spec.rb +92 -0
  55. data/spec/iolite_spec.rb +212 -0
  56. data/spec/spec_helper.rb +2 -0
  57. metadata +146 -0
@@ -0,0 +1,92 @@
1
+ load 'spec_helper.rb'
2
+ require "iolite/lazy"
3
+
4
+
5
+ describe "Iolite lazy" do
6
+ describe "Iolite::lazy" do
7
+ include Iolite
8
+ arg1 = Iolite.lazy { |*args| args[0] }
9
+ arg2 = Iolite.lazy { |*args| args[1] }
10
+
11
+ describe "#class" do
12
+ it "#class by Obejct#class" do
13
+ expect(lazy { |a, b| a + b }.class).to eq(Iolite::Lazy)
14
+ end
15
+ end
16
+
17
+ describe "#call" do
18
+ it "call" do
19
+ expect(lazy { |a, b| a + b }.call(1, 2)).to eq(3)
20
+ end
21
+ end
22
+
23
+ describe "#bind" do
24
+ it "bind argument" do
25
+ expect(lazy { |a, b| a + b }.bind(1, 2).call()).to eq(3)
26
+ end
27
+ it "bind placeholders" do
28
+ expect(lazy { |a, b| a + b }.bind(arg2, 2).call(2, 1)).to eq(3)
29
+ end
30
+ it "bind by placeholders" do
31
+ expect((arg1 - arg2).bind(arg2, 2).call(1, 1)).to eq(-1)
32
+ end
33
+ end
34
+
35
+ describe "#send" do
36
+ it "send method" do
37
+ expect(arg1.send(:length).call("homu")).to eq(4)
38
+ end
39
+ it "send Object method" do
40
+ expect(arg1.send(:class).call("homu")).to eq(String)
41
+ end
42
+ end
43
+
44
+ describe "#method_missing" do
45
+ it "send method" do
46
+ expect(arg1.length.call("homu")).to eq(4)
47
+ end
48
+ it "send Object method" do
49
+ expect(arg1.class).to eq(arg1.__send__(:class))
50
+ end
51
+ end
52
+
53
+ describe "#apply" do
54
+ it "apply lazy" do
55
+ expect(arg1.apply(1, 2).call(arg1 + arg2)).to eq(3)
56
+ end
57
+ end
58
+
59
+ describe "#+" do
60
+ it "call lazy" do
61
+ expect((lazy { |a, b| a + b } + 3).call(1, 2)).to eq(6)
62
+ end
63
+ end
64
+
65
+ describe "#==" do
66
+ it "call" do
67
+ expect((arg1 == 3).call(3)).to eq(true)
68
+ end
69
+ end
70
+
71
+ describe "#===" do
72
+ it "call" do
73
+ expect((arg1 === arg2).call(/^m/, "mami")).to eq(true)
74
+ end
75
+ end
76
+
77
+ describe "#to_s" do
78
+ it "Symbol#to_s" do
79
+ expect(arg1.to_s.call(:mami)).to eq("mami")
80
+ end
81
+ it "Fixnum#to_s" do
82
+ expect(arg1.to_s.call(42)).to eq("42")
83
+ end
84
+ end
85
+
86
+ describe "#class" do
87
+ it "Symbol#class" do
88
+ expect(arg1.class.call(:mami)).to eq(Symbol)
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,212 @@
1
+ load 'spec_helper.rb'
2
+
3
+ describe Iolite do
4
+ it 'has a version number' do
5
+ expect(Iolite::VERSION).not_to be nil
6
+ end
7
+
8
+ describe "Iolite" do
9
+ it "::lazy" do
10
+ expect(Iolite.lazy { |a, b| a + b }.class).to eq(Iolite::Lazy)
11
+ end
12
+ # it "::wrap" do
13
+ # expect((Iolite.wrap lambda { |a, b| a + b }).call(1, 2).class).to eq(Proc)
14
+ # end
15
+ end
16
+
17
+ describe "Iolite::Lazy" do
18
+ first = Iolite::lazy { |x| x }
19
+ plus = Iolite::lazy { |a, b| a + b }
20
+ describe "#call" do
21
+ it "call" do
22
+ expect(first.call(10)).to eq(10)
23
+ end
24
+
25
+ it "()" do
26
+ expect(first.(10)).to eq(10)
27
+ end
28
+
29
+ it "call multi arguments" do
30
+ expect(first.call(10, 2)).to eq(10)
31
+ expect(plus.call(10, 2)).to eq(12)
32
+ end
33
+ end
34
+
35
+ describe "#bind" do
36
+ include Iolite::Placeholders
37
+ f = Iolite::lazy { |a, b| a + b }
38
+ it "argument" do
39
+ expect(f.bind(-3, 1).call()).to eq(-2)
40
+ end
41
+ it "argument" do
42
+ expect(f.bind(arg2, arg1).call(-1, 3)).to eq(2)
43
+ end
44
+ it "operator" do
45
+ expect(Iolite::lazy(&:+).bind(arg2, arg1).call(-1, 3)).to eq(2)
46
+ end
47
+ end
48
+
49
+ describe "#send" do
50
+ it "call method" do
51
+ expect(first.send(:to_s).call(10).class).to eq(String)
52
+ end
53
+ it "call method with args" do
54
+ expect(first.send(:+, 1).call(2)).to eq(3)
55
+ end
56
+ include Iolite::Placeholders
57
+ it "call method with placeholders" do
58
+ expect(first.send(:+, arg2).call(2, -2)).to eq(0)
59
+ end
60
+ end
61
+
62
+ describe "#apply" do
63
+ include Iolite::Placeholders
64
+ it "call lambda" do
65
+ expect(arg1.apply(1, 2).call(lambda { |a ,b| a + b })).to eq(3)
66
+ end
67
+ it "call Iolite::Lazy" do
68
+ expect(arg1.apply(1, 2).call(Iolite::lazy { |a ,b| a + b })).to eq(3)
69
+ end
70
+ end
71
+
72
+ describe "operators" do
73
+ it "a + b" do
74
+ expect((first + first).call(1)).to eq(2)
75
+ end
76
+ it "a - b" do
77
+ expect((plus - first).call(2, 3)).to eq(3)
78
+ end
79
+ it "a * b" do
80
+ expect((first * first).call(2)).to eq(4)
81
+ end
82
+ it "a[b]" do
83
+ expect((first[:name]).call({name: :homu})).to eq(:homu)
84
+ end
85
+ it "a + {value}" do
86
+ expect((first + 3).call(4)).to eq(7)
87
+ end
88
+ it "==" do
89
+ expect([1, 2, 3].any? &first == 3).to eq(true)
90
+ end
91
+ it "=~" do
92
+ expect(["homu", "123", "mami"].any? &first =~ /\d+/).to eq(true)
93
+ end
94
+ it "!" do
95
+ expect([1, nil, 6].find_index &!first).to eq(1)
96
+ end
97
+ end
98
+
99
+ describe "#method_missing" do
100
+ it "call #length method" do
101
+ expect(first.length.call("homu")).to eq(4)
102
+ end
103
+ it "call #to_s method" do
104
+ expect(first.to_i.call("1").class).to eq(Fixnum)
105
+ end
106
+ it "call Object#*" do
107
+ # not call method_missing
108
+ expect(first.class).to eq(Iolite::Lazy)
109
+ end
110
+ it "call Object#* from #send" do
111
+ expect(first.send(:class).call("homu")).to eq(String)
112
+ end
113
+ end
114
+
115
+ describe "Array block" do
116
+ it "#map by" do
117
+ expect((1..3).map &(first + 3)).to eq([4, 5, 6])
118
+ end
119
+ it "#inject by" do
120
+ expect((2..4).inject &(plus)).to eq(9)
121
+ end
122
+ end
123
+ end
124
+
125
+ describe "Iolite::Placeholders" do
126
+ include Iolite::Placeholders
127
+ it "arg1" do
128
+ expect(arg1.call(1, 2)).to eq(1)
129
+ end
130
+ it "arg1 + arg2" do
131
+ expect((arg1 + arg2).call(1, 2)).to eq(3)
132
+ end
133
+ it "arg1[arg2]" do
134
+ expect((arg1[arg2]).call([1, 2, 3], 2)).to eq(3)
135
+ end
136
+ it "args" do
137
+ expect((args[arg2]).call(1, 3, 2, 4)).to eq(4)
138
+ end
139
+ it "bind" do
140
+ # expect(arg1.bind(10).call(Iolite.lambda { |x| x + x})).to eq(20)
141
+ end
142
+ it "alias _1, arg1" do
143
+ expect((_1 + _2).call(1, 2)).to eq(3)
144
+ end
145
+ end
146
+
147
+ describe "Iolite::adaptor" do
148
+ describe "Adapt callable" do
149
+ include Iolite::Placeholders
150
+ class UnCallableFromIolite
151
+ end
152
+ it "not callable" do
153
+ # UnCallableFromIolite.new.class
154
+ expect(arg1.send(:class).bind(UnCallableFromIolite.new).call(10)).to eq(UnCallableFromIolite)
155
+ end
156
+ class CallableFromIolite
157
+ include Iolite::Adaptor::Callable
158
+ def call n
159
+ n
160
+ end
161
+ end
162
+ it "callable" do
163
+ # CallableFromIolite.new.call("homu")
164
+ expect(arg1.send(:class).bind(CallableFromIolite.new).call("homu")).to eq(String)
165
+ end
166
+ it "wrap" do
167
+ # expect(arg1.send(:class).bind(Iolite.wrap CallableFromIolite.new).call("homu")).to eq(CallableFromIolite)
168
+ end
169
+ end
170
+ end
171
+
172
+ describe "Iolite::Statement" do
173
+ describe "if_else" do
174
+ include Iolite::Statement
175
+ include Iolite::Placeholders
176
+ it "call then" do
177
+ f = if_else(arg1 > 0, arg2, nil)
178
+ expect(f.(10, "yes")).to eq("yes")
179
+ end
180
+ it "call else" do
181
+ f = if_else(arg1 > 0, arg2, nil)
182
+ expect(f.(-10, "yes")).to eq(nil)
183
+ end
184
+ end
185
+ describe "if_" do
186
+ describe "if then" do
187
+ include Iolite::Statement
188
+ include Iolite::Placeholders
189
+ it "call then" do
190
+ f = if_(arg1 > 0)[ arg1, arg1 + arg1 ]
191
+ expect(f.(10)).to eq(20)
192
+ end
193
+ it "call else" do
194
+ f = if_(arg1 > 0)[ arg1, arg1 + arg1 ]
195
+ expect(f.(0)).to eq(nil)
196
+ end
197
+ end
198
+ describe "if then else" do
199
+ include Iolite::Statement
200
+ include Iolite::Placeholders
201
+ it "call then" do
202
+ f = if_(arg1 > 0)[ arg1, arg1 + arg1 ].else_[ arg1, arg1 - arg1 ]
203
+ expect(f.(10)).to eq(20)
204
+ end
205
+ it "call else" do
206
+ f = if_(arg1 > 0)[ arg1, arg1 + arg1 ].else_[ arg1, arg1 - arg1 ]
207
+ expect(f.(-10)).to eq(0)
208
+ end
209
+ end
210
+ end
211
+ end
212
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'iolite'
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iolite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - manga_osyo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-03 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: unblockable library
56
+ email:
57
+ - manga.osyo@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - docs/iolite.md
70
+ - example/array.rb
71
+ - example/fizzbuzz.rb
72
+ - example/hash.rb
73
+ - example/minimal_implement.rb
74
+ - example/simple.rb
75
+ - example/to_lazy.rb
76
+ - iolite.gemspec
77
+ - lib/iolite.rb
78
+ - lib/iolite/adaptor.rb
79
+ - lib/iolite/adaptor/all.rb
80
+ - lib/iolite/adaptor/apply.rb
81
+ - lib/iolite/adaptor/bind.rb
82
+ - lib/iolite/adaptor/callable.rb
83
+ - lib/iolite/adaptor/define_send_original_methods.rb
84
+ - lib/iolite/adaptor/method_missing.rb
85
+ - lib/iolite/adaptor/operators.rb
86
+ - lib/iolite/adaptor/send.rb
87
+ - lib/iolite/adaptor/to_lazy.rb
88
+ - lib/iolite/adaptor/to_proc.rb
89
+ - lib/iolite/adaptored/array.rb
90
+ - lib/iolite/adaptored/hash.rb
91
+ - lib/iolite/adaptored/iolite_lazy_with_hash.rb
92
+ - lib/iolite/adaptored/object_with_to_lazy.rb
93
+ - lib/iolite/adaptored/proc.rb
94
+ - lib/iolite/adaptored/string.rb
95
+ - lib/iolite/functinal.rb
96
+ - lib/iolite/functinal/bind.rb
97
+ - lib/iolite/functinal/define_iolite_functinal_send_method.rb
98
+ - lib/iolite/functinal/invoke.rb
99
+ - lib/iolite/functinal/send.rb
100
+ - lib/iolite/lazy.rb
101
+ - lib/iolite/placeholders.rb
102
+ - lib/iolite/refinements.rb
103
+ - lib/iolite/refinements/array.rb
104
+ - lib/iolite/refinements/hash.rb
105
+ - lib/iolite/refinements/object_with_to_lazy.rb
106
+ - lib/iolite/refinements/proc.rb
107
+ - lib/iolite/refinements/string.rb
108
+ - lib/iolite/statement.rb
109
+ - lib/iolite/statement/if.rb
110
+ - lib/iolite/statement/if_else.rb
111
+ - lib/iolite/version.rb
112
+ - spec/iolite_adaptored_spec.rb
113
+ - spec/iolite_functinal_spec.rb
114
+ - spec/iolite_lazy_spec.rb
115
+ - spec/iolite_spec.rb
116
+ - spec/spec_helper.rb
117
+ homepage: ''
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.4.5
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: unblockable library
141
+ test_files:
142
+ - spec/iolite_adaptored_spec.rb
143
+ - spec/iolite_functinal_spec.rb
144
+ - spec/iolite_lazy_spec.rb
145
+ - spec/iolite_spec.rb
146
+ - spec/spec_helper.rb