funkr 0.0.17 → 0.0.18

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.
data/lib/funkr/types.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'funkr/types/maybe'
2
2
  require 'funkr/types/failable'
3
3
  require 'funkr/types/simple_record'
4
+ require 'funkr/types/container'
@@ -0,0 +1,32 @@
1
+ require 'funkr/categories'
2
+
3
+ module Funkr
4
+ module Types
5
+ class Container
6
+
7
+ include Funkr::Categories
8
+
9
+ def initialize(value)
10
+ @value = value
11
+ end
12
+
13
+ def unbox; @value; end
14
+
15
+ def to_s; format("{- %s -}", @value.to_s); end
16
+
17
+ include Functor
18
+
19
+ def map(&block)
20
+ self.class.new(yield(@value))
21
+ end
22
+
23
+ include Monoid
24
+ extend Monoid::ClassMethods
25
+
26
+ def mplus(c_y)
27
+ self.class.new(@value.mplus(c_y.unbox))
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -19,7 +19,7 @@ module Funkr
19
19
  def map(&block)
20
20
  case self.const
21
21
  when :ok then
22
- Failable.ok(yield(*self.data))
22
+ self.class.ok(yield(*self.data))
23
23
  else self
24
24
  end
25
25
  end
@@ -30,7 +30,7 @@ module Funkr
30
30
  self.match do |f_on|
31
31
  f_on.ok do |f|
32
32
  to.match do |t_on|
33
- t_on.ok {|t| Failable.ok(f.call(t)) }
33
+ t_on.ok {|t| self.class.ok(f.call(t)) }
34
34
  t_on.failed { to }
35
35
  end
36
36
  end
@@ -58,7 +58,7 @@ module Funkr
58
58
  x_on.ok do |x|
59
59
  m_y.match do |y_on|
60
60
  y_on.failed { self }
61
- y_on.ok {|y| Failable.ok(x.mplus(y))}
61
+ y_on.ok {|y| self.class.ok(x.mplus(y))}
62
62
  end
63
63
  end
64
64
  end
@@ -15,7 +15,7 @@ module Funkr
15
15
 
16
16
  def map(&block)
17
17
  self.match do |on|
18
- on.just {|v| Maybe.just(yield(v))}
18
+ on.just {|v| self.class.just(yield(v))}
19
19
  on.nothing { self }
20
20
  end
21
21
  end
@@ -27,7 +27,7 @@ module Funkr
27
27
  self.match do |f_on|
28
28
  f_on.just do |f|
29
29
  to.match do |t_on|
30
- t_on.just {|t| Maybe.unit(f.call(t)) }
30
+ t_on.just {|t| self.class.unit(f.call(t)) }
31
31
  t_on.nothing { to }
32
32
  end
33
33
  end
@@ -54,7 +54,7 @@ module Funkr
54
54
  x_on.just do |x|
55
55
  m_y.match do |y_on|
56
56
  y_on.nothing { self }
57
- y_on.just {|y| Maybe.just(x.mplus(y))}
57
+ y_on.just {|y| self.class.just(x.mplus(y))}
58
58
  end
59
59
  end
60
60
  end
@@ -6,8 +6,21 @@ module Funkr
6
6
  ### r = SimpleRecord.new( name: "Paul", age: 27 )
7
7
  ### r.name => "Paul" ; r.age => 27
8
8
  ### name, age = r
9
-
9
+
10
+ ### other usage :
11
+ ### class Person < SimpleRecord; fields :name, :age; end
12
+ ### Person.new( name: Paul ) => Error, missing :age
13
+
14
+ class << self; attr_accessor :fields_list; end
15
+ @fields_list = nil
16
+
17
+ def self.fields(*flds); self.fields_list = flds; end
18
+
10
19
  def initialize(key_vals)
20
+ fields = self.class.fields_list
21
+ if not fields.nil? and fields != key_vals.keys then
22
+ raise "#{self.class.to_s} wrong initialization, expected #{fields}"
23
+ end
11
24
  @key_vals = key_vals
12
25
  key_vals.each do |k,v|
13
26
  getter = k.to_sym
data/lib/funkr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Funkr
2
- VERSION = "0.0.17"
2
+ VERSION = "0.0.18"
3
3
  end
data/test/tests.rb CHANGED
@@ -62,6 +62,10 @@ puts n.unbox.inspect
62
62
  puts (Maybe.box(12)).unbox.inspect
63
63
  puts (Maybe.box(nil)).unbox.inspect
64
64
 
65
+ puts "\n> Containers"
66
+ c = Container.new("Value")
67
+ puts c.unbox
68
+ puts c.map(&:reverse)
65
69
 
66
70
  a = [1,2,3]
67
71
  b = [10,20,30]
@@ -115,4 +119,8 @@ puts format("%s is back to %s and really lives in %s", r.name, r.age, r.city)
115
119
  r.name = "Paul R"
116
120
  puts r.to_s
117
121
 
122
+ class Person < SimpleRecord; fields :name, :age, :city; end
123
+ r = Person.new(name: "Paul", age: 27, city: "Rennes")
124
+ x = Person.new(name: "Paul", age: 27, city: "Rennes", genre: "Male") rescue "Forbidden"
125
+ y = Person.new(name: "Paul", age: 27) rescue "Forbidden"
118
126
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 17
9
- version: 0.0.17
8
+ - 18
9
+ version: 0.0.18
10
10
  platform: ruby
11
11
  authors:
12
12
  - Paul Rivier
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-09-26 00:00:00 +02:00
17
+ date: 2011-09-28 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -47,6 +47,7 @@ files:
47
47
  - lib/funkr/extensions/enumerable.rb
48
48
  - lib/funkr/extensions/fixnum.rb
49
49
  - lib/funkr/types.rb
50
+ - lib/funkr/types/container.rb
50
51
  - lib/funkr/types/failable.rb
51
52
  - lib/funkr/types/maybe.rb
52
53
  - lib/funkr/types/simple_record.rb