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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d09744d8d852c0e3029f00ac2401eec3612d573
4
- data.tar.gz: f032929f64dd3e3b500825c0122b3b34a72aa22b
3
+ metadata.gz: 8c098310eab0212453f44ea9ab52df6df00e8f6a
4
+ data.tar.gz: 1c696103481384d4eb9bb046f565d374410e2751
5
5
  SHA512:
6
- metadata.gz: 7f14f3c4b30e1506bc57c38d300ca3470a88ce8203a709ff58e93bebad41f101c9e7ea63606e3fed4d03fefbd1ad8c138c95b22badb88a2974b9d3b006f31948
7
- data.tar.gz: 5349290f24fcc4eb40b283170dac139ebedbf36d1254a2a41ad5fbd12fe866b61a6cf7332976906f8baa5113bb7c1bd1effb01c6a81b3269a43d23b03de76249
6
+ metadata.gz: 8acc6147e3463add46f5f30864f61e42b8207baf50df5e16f806f184df85facfbb11eaac86bbb013dc505b4314176a18f0b9cc89ae52bba211eeeb0537cbc94b
7
+ data.tar.gz: ce8f85be4605cd03dffdcca3836d08d14d7e3b2d21fc2a35919be57a6e711a5fe7e0780c2e6a4651bce6237eca9f117d0e63dfa75078a42ff7d5df5630bec48b
data/README.md CHANGED
@@ -92,6 +92,12 @@ y = None()
92
92
  Maybe(user) >> F . Maybe . address >> F . Maybe . street
93
93
  ```
94
94
 
95
+ As always, using point-free style is much cleaner:
96
+
97
+ ```ruby
98
+ Maybe(user) >> F . Maybe . address >> F . Maybe . street
99
+ ```
100
+
95
101
  ### `fmap`
96
102
 
97
103
  ```ruby
@@ -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
@@ -1,4 +1,4 @@
1
- require 'blankslate'
1
+ require_relative 'blankslate'
2
2
 
3
3
  class Proc
4
4
  def self.comp(f, g)
@@ -1,3 +1,3 @@
1
1
  module Kleisli
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -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.3
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-10 00:00:00.000000000 Z
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