kleisli 0.0.3 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -0
- data/lib/kleisli/blankslate.rb +110 -0
- data/lib/kleisli/composition.rb +1 -1
- data/lib/kleisli/version.rb +1 -1
- data/test/kleisli/try_test.rb +10 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c098310eab0212453f44ea9ab52df6df00e8f6a
|
4
|
+
data.tar.gz: 1c696103481384d4eb9bb046f565d374410e2751
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8acc6147e3463add46f5f30864f61e42b8207baf50df5e16f806f184df85facfbb11eaac86bbb013dc505b4314176a18f0b9cc89ae52bba211eeeb0537cbc94b
|
7
|
+
data.tar.gz: ce8f85be4605cd03dffdcca3836d08d14d7e3b2d21fc2a35919be57a6e711a5fe7e0780c2e6a4651bce6237eca9f117d0e63dfa75078a42ff7d5df5630bec48b
|
data/README.md
CHANGED
@@ -0,0 +1,110 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#--
|
3
|
+
# Copyright 2004, 2006 by Jim Weirich (jim@weirichhouse.org).
|
4
|
+
# All rights reserved.
|
5
|
+
|
6
|
+
# Permission is granted for use, copying, modification, distribution,
|
7
|
+
# and distribution of modified versions of this work as long as the
|
8
|
+
# above copyright notice is included.
|
9
|
+
#++
|
10
|
+
|
11
|
+
######################################################################
|
12
|
+
# BlankSlate provides an abstract base class with no predefined
|
13
|
+
# methods (except for <tt>\_\_send__</tt> and <tt>\_\_id__</tt>).
|
14
|
+
# BlankSlate is useful as a base class when writing classes that
|
15
|
+
# depend upon <tt>method_missing</tt> (e.g. dynamic proxies).
|
16
|
+
#
|
17
|
+
class BlankSlate
|
18
|
+
class << self
|
19
|
+
|
20
|
+
# Hide the method named +name+ in the BlankSlate class. Don't
|
21
|
+
# hide +instance_eval+ or any method beginning with "__".
|
22
|
+
def hide(name)
|
23
|
+
methods = instance_methods.map(&:to_sym)
|
24
|
+
if methods.include?(name.to_sym) and
|
25
|
+
name !~ /^(__|instance_eval)/
|
26
|
+
@hidden_methods ||= {}
|
27
|
+
@hidden_methods[name.to_sym] = instance_method(name)
|
28
|
+
undef_method name
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def find_hidden_method(name)
|
33
|
+
@hidden_methods ||= {}
|
34
|
+
@hidden_methods[name] || superclass.find_hidden_method(name)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Redefine a previously hidden method so that it may be called on a blank
|
38
|
+
# slate object.
|
39
|
+
def reveal(name)
|
40
|
+
hidden_method = find_hidden_method(name)
|
41
|
+
fail "Don't know how to reveal method '#{name}'" unless hidden_method
|
42
|
+
define_method(name, hidden_method)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
instance_methods.each { |m| hide(m) }
|
47
|
+
end
|
48
|
+
|
49
|
+
######################################################################
|
50
|
+
# Since Ruby is very dynamic, methods added to the ancestors of
|
51
|
+
# BlankSlate <em>after BlankSlate is defined</em> will show up in the
|
52
|
+
# list of available BlankSlate methods. We handle this by defining a
|
53
|
+
# hook in the Object and Kernel classes that will hide any method
|
54
|
+
# defined after BlankSlate has been loaded.
|
55
|
+
#
|
56
|
+
module Kernel
|
57
|
+
class << self
|
58
|
+
alias_method :blank_slate_method_added, :method_added
|
59
|
+
|
60
|
+
# Detect method additions to Kernel and remove them in the
|
61
|
+
# BlankSlate class.
|
62
|
+
def method_added(name)
|
63
|
+
result = blank_slate_method_added(name)
|
64
|
+
return result if self != Kernel
|
65
|
+
BlankSlate.hide(name)
|
66
|
+
result
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
######################################################################
|
72
|
+
# Same as above, except in Object.
|
73
|
+
#
|
74
|
+
class Object
|
75
|
+
class << self
|
76
|
+
alias_method :blank_slate_method_added, :method_added
|
77
|
+
|
78
|
+
# Detect method additions to Object and remove them in the
|
79
|
+
# BlankSlate class.
|
80
|
+
def method_added(name)
|
81
|
+
result = blank_slate_method_added(name)
|
82
|
+
return result if self != Object
|
83
|
+
BlankSlate.hide(name)
|
84
|
+
result
|
85
|
+
end
|
86
|
+
|
87
|
+
def find_hidden_method(name)
|
88
|
+
nil
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
######################################################################
|
94
|
+
# Also, modules included into Object need to be scanned and have their
|
95
|
+
# instance methods removed from blank slate. In theory, modules
|
96
|
+
# included into Kernel would have to be removed as well, but a
|
97
|
+
# "feature" of Ruby prevents late includes into modules from being
|
98
|
+
# exposed in the first place.
|
99
|
+
#
|
100
|
+
class Module
|
101
|
+
alias blankslate_original_append_features append_features
|
102
|
+
def append_features(mod)
|
103
|
+
result = blankslate_original_append_features(mod)
|
104
|
+
return result if mod != Object
|
105
|
+
instance_methods.each do |name|
|
106
|
+
BlankSlate.hide(name)
|
107
|
+
end
|
108
|
+
result
|
109
|
+
end
|
110
|
+
end
|
data/lib/kleisli/composition.rb
CHANGED
data/lib/kleisli/version.rb
CHANGED
data/test/kleisli/try_test.rb
CHANGED
@@ -24,4 +24,14 @@ class TryTest < MiniTest::Unit::TestCase
|
|
24
24
|
def test_fmap_failure
|
25
25
|
assert_kind_of ZeroDivisionError, Try { 10 / 0 }.fmap { |x| x * 2 }.exception
|
26
26
|
end
|
27
|
+
|
28
|
+
def test_bind
|
29
|
+
try = Try { 20 / 10 } >-> number { Try { 10 / number } }
|
30
|
+
assert_equal 5, try.value
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_pointfree
|
34
|
+
try = Try { 20 / 10 } >> F . Try { 10 / 2 }
|
35
|
+
assert_equal 5, try.value
|
36
|
+
end
|
27
37
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kleisli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josep M. Bach
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-11-
|
12
|
+
date: 2014-11-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -55,6 +55,7 @@ files:
|
|
55
55
|
- Rakefile
|
56
56
|
- kleisli.gemspec
|
57
57
|
- lib/kleisli.rb
|
58
|
+
- lib/kleisli/blankslate.rb
|
58
59
|
- lib/kleisli/composition.rb
|
59
60
|
- lib/kleisli/either.rb
|
60
61
|
- lib/kleisli/functor.rb
|