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.
- checksums.yaml +7 -0
- data/.gitignore +24 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +70 -0
- data/Rakefile +7 -0
- data/docs/iolite.md +278 -0
- data/example/array.rb +17 -0
- data/example/fizzbuzz.rb +15 -0
- data/example/hash.rb +14 -0
- data/example/minimal_implement.rb +42 -0
- data/example/simple.rb +46 -0
- data/example/to_lazy.rb +37 -0
- data/iolite.gemspec +24 -0
- data/lib/iolite.rb +14 -0
- data/lib/iolite/adaptor.rb +2 -0
- data/lib/iolite/adaptor/all.rb +22 -0
- data/lib/iolite/adaptor/apply.rb +10 -0
- data/lib/iolite/adaptor/bind.rb +9 -0
- data/lib/iolite/adaptor/callable.rb +7 -0
- data/lib/iolite/adaptor/define_send_original_methods.rb +12 -0
- data/lib/iolite/adaptor/method_missing.rb +10 -0
- data/lib/iolite/adaptor/operators.rb +26 -0
- data/lib/iolite/adaptor/send.rb +11 -0
- data/lib/iolite/adaptor/to_lazy.rb +11 -0
- data/lib/iolite/adaptor/to_proc.rb +10 -0
- data/lib/iolite/adaptored/array.rb +12 -0
- data/lib/iolite/adaptored/hash.rb +14 -0
- data/lib/iolite/adaptored/iolite_lazy_with_hash.rb +7 -0
- data/lib/iolite/adaptored/object_with_to_lazy.rb +5 -0
- data/lib/iolite/adaptored/proc.rb +5 -0
- data/lib/iolite/adaptored/string.rb +21 -0
- data/lib/iolite/functinal.rb +4 -0
- data/lib/iolite/functinal/bind.rb +12 -0
- data/lib/iolite/functinal/define_iolite_functinal_send_method.rb +9 -0
- data/lib/iolite/functinal/invoke.rb +11 -0
- data/lib/iolite/functinal/send.rb +13 -0
- data/lib/iolite/lazy.rb +40 -0
- data/lib/iolite/placeholders.rb +30 -0
- data/lib/iolite/refinements.rb +6 -0
- data/lib/iolite/refinements/array.rb +16 -0
- data/lib/iolite/refinements/hash.rb +18 -0
- data/lib/iolite/refinements/object_with_to_lazy.rb +9 -0
- data/lib/iolite/refinements/proc.rb +9 -0
- data/lib/iolite/refinements/string.rb +26 -0
- data/lib/iolite/statement.rb +3 -0
- data/lib/iolite/statement/if.rb +48 -0
- data/lib/iolite/statement/if_else.rb +11 -0
- data/lib/iolite/version.rb +3 -0
- data/spec/iolite_adaptored_spec.rb +101 -0
- data/spec/iolite_functinal_spec.rb +87 -0
- data/spec/iolite_lazy_spec.rb +92 -0
- data/spec/iolite_spec.rb +212 -0
- data/spec/spec_helper.rb +2 -0
- metadata +146 -0
data/example/fizzbuzz.rb
ADDED
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
|
+
|
data/example/to_lazy.rb
ADDED
@@ -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,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,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,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,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,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,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
|