iolite 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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,15 @@
1
+ require "iolite"
2
+
3
+ include Iolite::Placeholders
4
+ include Iolite::Statement
5
+
6
+ fizzbuzz = if_else(
7
+ arg1 % 15 == 0, "FizzBuzz", if_else(
8
+ arg1 % 5 == 0, "Buzz", if_else(
9
+ arg1 % 3 == 0, "Fizz",
10
+ arg1
11
+ )))
12
+
13
+ p (1..20).map &fizzbuzz
14
+
15
+
data/example/hash.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "iolite"
2
+
3
+ # Use require
4
+ # define Hash#to_proc
5
+ require "iolite/adaptored/hash"
6
+
7
+ include Iolite::Placeholders
8
+
9
+ p ({arg1 => arg2}).to_proc.call(:name, "homu")
10
+ # => {:name=>"homu"}
11
+
12
+ p ["homu", "mami", "mado"]. map &({ name: arg1 })
13
+ # => [{:name=>"homu"}, {:name=>"mami"}, {:name=>"mado"}]
14
+
@@ -0,0 +1,42 @@
1
+
2
+ # invoke call function
3
+ def invoke func, *args
4
+ func.respond_to?(:call) ? func.call(*args) : func
5
+ end
6
+
7
+ class Proc
8
+ # send method
9
+ def send symbol, *args_
10
+ Proc.new { |*args|
11
+ self.call(*args).send(symbol, *args_.map{ |it| invoke(it, *args) })
12
+ }
13
+ end
14
+
15
+ # call any method
16
+ def method_missing symbol, *args
17
+ send(symbol, *args)
18
+ end
19
+ end
20
+
21
+
22
+ # return args[N]
23
+ arg1 = Proc.new { |*args| args[0] }
24
+ arg2 = Proc.new { |*args| args[1] }
25
+
26
+
27
+ # assemble expr
28
+ f = arg1 + arg2
29
+
30
+ # apply expr
31
+ result = f.call(1, 2)
32
+ # => 3
33
+
34
+ # call
35
+ # 1. (arg1 + arg2).call(1, 2)
36
+ # 2. arg1.send(:+, arg2).call(1, 2)
37
+ # 3. arg1.call(1, 2).send(invoke(:+, 1, 2), invoke(arg2, 1, 2))
38
+ # 4. arg1.call(1, 2).send(:+, arg2.call(1, 2))
39
+ # 5. [1, 2][0].send(:+, [1, 2][1])
40
+ # 6. 1.send(:+, 2)
41
+ # 7. 3
42
+
data/example/simple.rb ADDED
@@ -0,0 +1,46 @@
1
+ require "iolite"
2
+
3
+
4
+ #######################################
5
+ # Using block
6
+ #######################################
7
+
8
+ p (1..5).map { |it| it + 3 }
9
+ # => [4, 5, 6, 7, 8]
10
+
11
+ p (1..5).inject { |memo, item| memo + item }
12
+ # => 15
13
+
14
+ p ["homu", "mami", "an"].inject(0) { |memo, item| memo + item.length }
15
+ # => 10
16
+
17
+ p [{name: :homu}, {name: :mami}].map { |it| it[:name] }
18
+ # => [:homu, :mami]
19
+
20
+ p [:homu, :mami, :mado].select { |it| it =~ /^m/ }
21
+ # => [:mami, :mado]
22
+
23
+
24
+ #######################################
25
+ # Using iolite
26
+ #######################################
27
+
28
+ # using arg1, arg2...
29
+ include Iolite::Placeholders
30
+
31
+ p (1..5).map &arg1 + 3
32
+ # => [4, 5, 6, 7, 8]
33
+
34
+ p (1..5).inject &arg1 + arg2
35
+ # => 15
36
+
37
+ p ["homu", "mami", "an"].inject 0, &arg1 + arg2.length
38
+ # => 10
39
+
40
+ p [{name: :homu}, {name: :mami}].map &arg1[:name]
41
+ # => [:homu, :mami]
42
+
43
+ p [:homu, :mami, :mado].select &arg1 =~ /^m/
44
+ # => [:mami, :mado]
45
+
46
+
@@ -0,0 +1,37 @@
1
+ require "iolite"
2
+
3
+ require "iolite/refinements/object_with_to_lazy"
4
+ using Iolite::Refinements::ObjectWithToLazy
5
+
6
+ # Using 1.9.x
7
+ # require "iolite/adaptored/object_with_to_lazy"
8
+
9
+
10
+ include Iolite::Placeholders
11
+
12
+
13
+ class X
14
+ def plus a, b
15
+ a + b
16
+ end
17
+ end
18
+
19
+ x = X.new
20
+
21
+ # To to_lazy object
22
+ # to_lazy return "Iolite.lazy { |*args| self }"
23
+ to_lazy_x = x.to_lazy
24
+
25
+ # To to_lazy function
26
+ to_lazy_f = to_lazy_x.plus(arg1, arg2)
27
+
28
+ puts to_lazy_f.call(1, 2)
29
+ # => 3
30
+
31
+
32
+ str = "saya."
33
+ ["homu", "mami", "mado"].each &str.to_lazy.concat(arg1 + ".")
34
+ puts str
35
+ # => saya.homu.mami.mado.
36
+
37
+
data/iolite.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'iolite/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "iolite"
8
+ spec.version = Iolite::VERSION
9
+ spec.authors = ["manga_osyo"]
10
+ spec.email = ["manga.osyo@gmail.com"]
11
+ spec.summary = %q{unblockable library}
12
+ spec.description = %q{unblockable library}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
data/lib/iolite.rb ADDED
@@ -0,0 +1,14 @@
1
+ require "iolite/version"
2
+ require "iolite/adaptor"
3
+ require "iolite/functinal"
4
+ require "iolite/lazy"
5
+ require "iolite/placeholders"
6
+ require "iolite/statement"
7
+
8
+ # Not support 1.9.x
9
+ require "iolite/refinements" if RUBY_VERSION.to_f > 2.0
10
+
11
+ module Iolite
12
+ include Functinal
13
+ include Statement
14
+ end
@@ -0,0 +1,2 @@
1
+ require "iolite/adaptor/all"
2
+
@@ -0,0 +1,22 @@
1
+ require "iolite/adaptor/define_send_original_methods"
2
+ require "iolite/adaptor/callable"
3
+ require "iolite/adaptor/bind"
4
+ require "iolite/adaptor/send"
5
+ require "iolite/adaptor/to_proc"
6
+ require "iolite/adaptor/apply"
7
+ require "iolite/adaptor/operators"
8
+ require "iolite/adaptor/method_missing"
9
+ require "iolite/adaptor/to_lazy"
10
+
11
+ module Iolite module Adaptor
12
+ module All
13
+ include Callable
14
+ include Bind
15
+ include Send
16
+ include MethodMissing
17
+ include ToProc
18
+ include Apply
19
+ include Operators
20
+ include ToLazy
21
+ end
22
+ end end
@@ -0,0 +1,10 @@
1
+ require "iolite/functinal/bind"
2
+
3
+ module Iolite module Adaptor
4
+ module Apply
5
+ def apply *args, &block
6
+ Functinal.bind(self, *args, &block)
7
+ end
8
+ alias_method :iolite_apply, :apply
9
+ end
10
+ end end
@@ -0,0 +1,9 @@
1
+ require "iolite/functinal/bind"
2
+
3
+ module Iolite module Adaptor
4
+ module Bind
5
+ def bind *args
6
+ Functinal.bind(Iolite.lazy { |*args| self }, *args)
7
+ end
8
+ end
9
+ end end
@@ -0,0 +1,7 @@
1
+ module Iolite module Adaptor
2
+ module Callable
3
+ def iolite_functinal_invoke_call(*args)
4
+ call(*args)
5
+ end
6
+ end
7
+ end end
@@ -0,0 +1,12 @@
1
+ require "iolite/functinal/send"
2
+
3
+ class Module
4
+ def iolite_define_send_original_methods prefix = "_"
5
+ instance_methods.each{ |method|
6
+ next if method !~ /\w/
7
+ define_method("#{prefix + method.to_s}"){ |*args|
8
+ Iolite::Functinal.send(self, method, *args)
9
+ }
10
+ }
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ require "iolite/functinal/send"
2
+
3
+ module Iolite module Adaptor
4
+ module MethodMissing
5
+ def method_missing name, *args, &block
6
+ Functinal.send(self, name, *args, &block)
7
+ end
8
+ module_function :method_missing
9
+ end
10
+ end end
@@ -0,0 +1,26 @@
1
+ require "iolite/functinal/define_iolite_functinal_send_method"
2
+
3
+ module Iolite module Adaptor
4
+ module Operators
5
+ define_iolite_functinal_send_method :==
6
+ define_iolite_functinal_send_method :=~
7
+ define_iolite_functinal_send_method :!
8
+ define_iolite_functinal_send_method :!=
9
+ define_iolite_functinal_send_method :!~
10
+ define_iolite_functinal_send_method :===
11
+
12
+ # &&
13
+ def product rhs
14
+ Lazy.new { |*args|
15
+ Functinal.invoke(self, *args) && Functinal.invoke(rhs, *args)
16
+ }
17
+ end
18
+
19
+ # ||
20
+ def disjunction rhs
21
+ Lazy.new { |*args|
22
+ Functinal.invoke(self, *args) || Functinal.invoke(rhs, *args)
23
+ }
24
+ end
25
+ end
26
+ end end
@@ -0,0 +1,11 @@
1
+ require "iolite/functinal/send"
2
+ require "iolite/functinal/invoke"
3
+
4
+ module Iolite module Adaptor
5
+ module Send
6
+ def send *args, &block
7
+ Functinal.send(self, *args, &block)
8
+ end
9
+ alias_method :iolite_send, :send
10
+ end
11
+ end end
@@ -0,0 +1,11 @@
1
+ # require "iolite/lazy"
2
+
3
+ module Iolite module Adaptor
4
+ module ToLazy
5
+ def to_lazy
6
+ Iolite.lazy { |*args| self }
7
+ end
8
+ alias_method :iolite_to_lazy, :to_lazy
9
+ alias_method :to_l, :to_lazy
10
+ end
11
+ end end
@@ -0,0 +1,10 @@
1
+ module Iolite module Adaptor
2
+ module ToProc
3
+ def to_proc
4
+ Proc.new { |*args|
5
+ self.call(*args)
6
+ }
7
+ end
8
+ alias_method :iolite_to_proc, :to_proc
9
+ end
10
+ end end
@@ -0,0 +1,12 @@
1
+ require "iolite/adaptor/all"
2
+ require "iolite/functinal/invoke"
3
+
4
+ class Array
5
+ include Iolite::Adaptor::ToProc
6
+ include Iolite::Adaptor::Bind
7
+ include Iolite::Adaptor::Apply
8
+ include Iolite::Adaptor::Callable
9
+ def call *args
10
+ Iolite::Functinal.invoke_a(self, *args)
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ require "iolite/adaptor/all"
2
+ require "iolite/adaptored/iolite_lazy_with_hash"
3
+
4
+ class Hash
5
+ include Iolite::Adaptor::ToProc
6
+ include Iolite::Adaptor::Bind
7
+ include Iolite::Adaptor::Apply
8
+ include Iolite::Adaptor::Callable
9
+ def call *args
10
+ Hash[ self.map { |key, value|
11
+ Iolite::Functinal.invoke_a([key, value], *args)
12
+ } ]
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module Iolite
2
+ class Lazy
3
+ def hash
4
+ __id__
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ require "iolite/adaptor/to_lazy"
2
+
3
+ class Object
4
+ include Iolite::Adaptor::ToLazy
5
+ end
@@ -0,0 +1,5 @@
1
+ require "iolite/adaptor/all"
2
+
3
+ class Proc
4
+ include Iolite::Adaptor::All
5
+ end
@@ -0,0 +1,21 @@
1
+ require "iolite/placeholders"
2
+
3
+ class String
4
+ include Iolite::Adaptor::ToProc
5
+ include Iolite::Adaptor::Callable
6
+ def call *args
7
+ result = self.clone
8
+ args.each_with_index { |it, i|
9
+ result.gsub! "#{Iolite::Placeholders.const_get("ARG#{i+1}")}", it.to_s
10
+ }
11
+ result
12
+ end
13
+
14
+ def to_call_by_eval binding = nil
15
+ Iolite.lazy { |*args|
16
+ gsub(/#{'#{(.*?)}'}/) {
17
+ eval($1, binding).call(*args)
18
+ }
19
+ }
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ require "iolite/functinal/invoke"
2
+ require "iolite/functinal/send"
3
+ require "iolite/functinal/bind"
4
+ require "iolite/functinal/define_iolite_functinal_send_method"
@@ -0,0 +1,12 @@
1
+ require "iolite/functinal/invoke"
2
+
3
+ module Iolite module Functinal
4
+ def bind func, *args_
5
+ Iolite.lazy { |*args|
6
+ # func.call(*invoke_a(args_, *args))
7
+ invoke(func, *args).call(*invoke_a(args_, *args))
8
+ # func.call(*args).call(*invoke_a(args_, *args))
9
+ }
10
+ end
11
+ module_function :bind
12
+ end end