hamsterdam 1.0.9 → 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,6 +16,10 @@ module Hamsterdam
16
16
  ::Hamster.list(*values)
17
17
  end
18
18
 
19
+ def self.queue(*values)
20
+ ::Hamster.queue(*values)
21
+ end
22
+
19
23
  def self.internal_hash_class
20
24
  ::Hamster::Hash
21
25
  end
@@ -55,6 +59,10 @@ module Hamsterdam
55
59
  internals.list(*values)
56
60
  end
57
61
 
62
+ def self.queue(*values)
63
+ internals.queue(*values)
64
+ end
65
+
58
66
  def self.internal_hash_class
59
67
  internals.internal_hash_class
60
68
  end
@@ -1,65 +1,34 @@
1
1
 
2
- class Java::ClojureLang::PersistentHashMap
3
- alias_method :put, :assoc
4
- alias_method :delete, :without
5
- alias_method :==, :equals
6
- end
7
-
8
- # java_import 'clojure.lang.PersistentList$EmptyList'
9
- # class EmptyList
10
- # def inspect
11
- # to_a.inspect
12
- # end
13
- # end
14
- #
15
- # class Java::ClojureLang::PersistentList
16
- # def inspect
17
- # to_a.inspect
18
- # end
19
- # end
20
- #
21
- # class Java::ClojureLang::PersistentHashSet
22
- # def inspect
23
- # to_set.inspect
24
- # end
25
- #
26
- # def -(other)
27
- # reject { |e| other.include?(e) }
28
- # end
29
- #
30
- # alias_method :add, :cons
31
- # end
32
-
33
2
  module Hamsterdam
34
3
  module Clojure
4
+ java_import 'clojure.lang.PersistentList$EmptyList'
5
+ List = Java::ClojureLang::PersistentList
6
+ Hash = Java::ClojureLang::PersistentHashMap
7
+ Set = Java::ClojureLang::PersistentHashSet
8
+ Queue = Java::ClojureLang::PersistentQueue
9
+
35
10
  def self.hash(h = nil)
36
11
  if h.nil?
37
- Java::ClojureLang::PersistentHashMap::EMPTY
12
+ Hash::EMPTY
38
13
  else
39
- Java::ClojureLang::PersistentHashMap.create(h)
14
+ Hash.create(h)
40
15
  end
41
16
  end
42
17
 
43
18
  def self.internal_hash_class
44
- Java::ClojureLang::PersistentHashMap
19
+ Hash
45
20
  end
46
21
 
47
- # def self.set(*values)
48
- # Java::ClojureLang::PersistentHashSet.create(values)
49
- # end
50
-
51
- # def self.list(*values)
52
- # values.reverse.inject(Java::ClojureLang::PersistentList::EMPTY) do |list, value|
53
- # list.cons(value)
54
- # end
55
- # end
56
-
57
22
  def self.set(*values)
58
- ::Hamster.set(*values)
23
+ Set.create(values)
59
24
  end
60
25
 
61
26
  def self.list(*values)
62
- ::Hamster.list(*values)
27
+ List.create(values).pop
28
+ end
29
+
30
+ def self.queue(*values)
31
+ Queue.create(values)
63
32
  end
64
33
 
65
34
  def self.symbolize_keys(hash)
@@ -74,4 +43,170 @@ module Hamsterdam
74
43
  end
75
44
  end
76
45
 
46
+ class Hamsterdam::Clojure::Hash
47
+ alias_method :put, :assoc
48
+ alias_method :delete, :without
49
+ alias_method :==, :equals
50
+ end
51
+
52
+ class Hamsterdam::Clojure::Queue
53
+ alias_method :dequeue, :pop
54
+ alias_method :enqueue, :cons
55
+
56
+ def inspect
57
+ to_a.inspect
58
+ end
59
+
60
+ def self.create(values)
61
+ values.inject(Hamsterdam::Clojure::Queue::EMPTY) do |queue, val|
62
+ queue.cons(val)
63
+ end
64
+ end
65
+ end
66
+
67
+ class Hamsterdam::Clojure::EmptyList
68
+ def inspect
69
+ "[]"
70
+ end
71
+
72
+ alias_method :to_ary, :to_a
73
+
74
+ def reverse
75
+ self
76
+ end
77
+
78
+ def reject(&block)
79
+ self
80
+ end
81
+
82
+ def reduce(initial)
83
+ initial
84
+ end
85
+
86
+ def map
87
+ self
88
+ end
89
+
90
+ def compact
91
+ self
92
+ end
93
+
94
+ def to_set
95
+ Java::ClojureLang::PersistentHashSet::EMPTY
96
+ end
97
+
98
+ def flatten
99
+ self
100
+ end
101
+
102
+ def uniq
103
+ self
104
+ end
105
+
106
+ def last
107
+ nil
108
+ end
109
+
110
+ def delete(entry)
111
+ self
112
+ end
113
+ end
114
+
115
+ class Hamsterdam::Clojure::List
116
+
117
+ def inspect
118
+ to_a.inspect
119
+ end
120
+
121
+ alias_method :to_ary, :to_a
122
+
123
+ def reverse
124
+ make_list to_a.reverse
125
+ end
126
+
127
+ def reject(&block)
128
+ make_list to_a.reject(&block)
129
+ end
130
+
131
+ def flatten
132
+ make_list to_a.flatten
133
+ end
134
+
135
+ def uniq
136
+ make_list to_a.uniq
137
+ end
138
+
139
+ def last
140
+ to_a.reverse.first
141
+ end
142
+
143
+ def compact
144
+ reject { |e| e.nil? }
145
+ end
146
+
147
+ def reduce(initial, &block)
148
+ to_a.inject(initial, &block)
149
+ end
150
+ alias_method :inject, :reduce
151
+
152
+ def map(&block)
153
+ make_list to_a.map(&block)
154
+ end
155
+
156
+ def delete(entry)
157
+ reject { |i| i == entry }
158
+ end
159
+
160
+ def to_set
161
+ Hamsterdam::Clojure::Set.create(to_a)
162
+ end
163
+
164
+ private
165
+ def make_list(array)
166
+ Hamsterdam::Clojure::List.create(array).pop
167
+ end
168
+ end
169
+
170
+ class Hamsterdam::Clojure::Set
171
+
172
+ def inspect
173
+ to_a.inspect.sub(/^\[/, "{").sub(/\]$/, "}")
174
+ end
175
+
176
+ def reject(&block)
177
+ make_set to_a.reject(&block)
178
+ end
179
+
180
+ def subtract(enumerable)
181
+ reject { |e| enumerable.include?(e) }
182
+ end
183
+
184
+ def reduce(initial, &block)
185
+ to_a.inject(initial, &block)
186
+ end
187
+
188
+ def map(&block)
189
+ make_set to_a.map(&block)
190
+ end
191
+
192
+ def flatten
193
+ make_set to_a.flatten
194
+ end
195
+
196
+ def compact
197
+ reject { |e| e.nil? }
198
+ end
199
+
200
+ alias_method :delete, :disjoin
201
+ alias_method :to_ary, :to_a
202
+ alias_method :add, :cons
203
+ alias_method :-, :subtract
204
+ alias_method :inject, :reduce
205
+
206
+ private
207
+ def make_set(array)
208
+ Hamsterdam::Clojure::Set.create(array)
209
+ end
210
+ end
211
+
77
212
  Hamsterdam.internals = Hamsterdam::Clojure
@@ -1,3 +1,3 @@
1
1
  module Hamsterdam
2
- VERSION = "1.0.9"
2
+ VERSION = "1.0.10"
3
3
  end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Hamsterdam's immutable persistent data structures" do
4
+
5
+ describe "list" do
6
+ describe "an empty list" do
7
+ subject { Hamsterdam.list }
8
+
9
+ it "is a conforming empty list" do
10
+ subject.should == subject
11
+ subject.inspect.should == "[]"
12
+ subject.reverse.should == subject
13
+ subject.cons(1).should == Hamsterdam.list(1)
14
+ subject.reject { |i| i % 2 == 0 }.should == subject
15
+ subject.reduce(0) { |total, i| total + i }.should == 0
16
+ subject.map { |i| i+1 }.should == subject
17
+ subject.compact.should == subject
18
+ subject.flatten.should == subject
19
+ subject.uniq.should == subject
20
+ subject.last.should == nil
21
+ subject.to_a.should == []
22
+ subject.to_ary.should == []
23
+ subject.to_set.should == Hamsterdam.set
24
+ end
25
+ end
26
+
27
+ describe "a populated list" do
28
+ subject { Hamsterdam.list("a", "b", "c") }
29
+
30
+ it "is a conforming list" do
31
+ subject.should == subject
32
+
33
+ subject.to_ary.should == ["a", "b", "c"]
34
+ subject.to_a.should == ["a", "b", "c"]
35
+ subject.cons("d").should == Hamsterdam.list("d", "a", "b", "c")
36
+ subject.inspect.should == ["a", "b", "c"].inspect
37
+ subject.reverse.should == Hamsterdam.list("c", "b", "a")
38
+ subject.reject { |char| char == "b" }.should == Hamsterdam.list("a", "c")
39
+ subject.cons(Hamsterdam.list("d")).flatten.should == Hamsterdam.list("d", "a", "b", "c")
40
+ subject.cons("a").uniq.should == subject
41
+ subject.last.should == "c"
42
+ subject.cons(nil).compact.should == subject
43
+ subject.reduce("Word: ") { |str, char| "#{str}#{char}"}.should == "Word: abc"
44
+ subject.inject("Word: ") { |str, char| "#{str}#{char}"}.should == "Word: abc"
45
+ subject.map { |char| "#{char}!" }.should == Hamsterdam.list("a!", "b!", "c!")
46
+ subject.to_set.should == Hamsterdam.set("a", "b", "c")
47
+ end
48
+ end
49
+ end
50
+
51
+ describe "set" do
52
+ describe "an empty set" do
53
+ subject { Hamsterdam.set }
54
+
55
+ it "is a conforming empty set" do
56
+ subject.should == subject
57
+ subject.inspect.should == "{}"
58
+ subject.reject { |i| i % 2 == 0 }.should == subject
59
+ (subject - [1,2,3]).should == subject
60
+ subject.add("a").should == Hamsterdam.set("a")
61
+ subject.reduce(0) { |total, i| total + i }.should == 0
62
+ subject.map { |i| i+1 }.should == subject
63
+ subject.compact.should == subject
64
+ subject.flatten.should == subject
65
+ subject.delete("a").should == subject
66
+ subject.to_a.should == []
67
+ subject.to_ary.should == []
68
+ end
69
+ end
70
+
71
+ describe "a populated set" do
72
+ subject { Hamsterdam.set("a", "b", "c", "a") }
73
+
74
+ it "is a conforming list" do
75
+ subject.should == subject
76
+
77
+ subject.add("d").should == Hamsterdam.set("d", "a", "b", "c")
78
+ subject.inspect.should =~ /^\{"(a|b|c)", "(a|b|c)", "(a|b|c)"\}$/
79
+ subject.reject { |char| char == "b" }.should == Hamsterdam.set("a", "c")
80
+ (subject - ["c", "a"]).should == Hamsterdam.set("b")
81
+ subject.add(Hamsterdam.set("d")).flatten.should == Hamsterdam.set("d", "a", "b", "c")
82
+ subject.add(nil).compact.should == subject
83
+ subject.reduce("Word: ") { |str, char| "#{str}#{char}"}.should =~ /^Word: (a|b|c)(a|b|c)(a|b|c)$/
84
+ subject.inject("Word: ") { |str, char| "#{str}#{char}"}.should =~ /^Word: (a|b|c)(a|b|c)(a|b|c)$/
85
+ subject.map { |char| "#{char}!" }.should == Hamsterdam.set("a!", "b!", "c!")
86
+ subject.to_ary.sort.should == ["a", "b", "c"]
87
+ subject.to_a.sort.should == ["a", "b", "c"]
88
+ end
89
+ end
90
+ end
91
+
92
+ describe "hash" do
93
+ describe "an empty hash" do
94
+ subject { Hamsterdam.hash}
95
+
96
+ it "is a conforming empty hash" do
97
+ subject.should == subject
98
+ subject.put("hey", "now").should == Hamsterdam.hash("hey" => "now")
99
+ subject.delete("hey").should == subject
100
+ end
101
+ end
102
+
103
+ describe "a populated hash" do
104
+ subject { Hamsterdam.hash(foo: "a", bar: "b", qux: "c") }
105
+
106
+ it "is a confirming populated hash" do
107
+ subject.should == subject
108
+ subject.put(:foot, "bart").should == Hamsterdam.hash(foo: "a", bar: "b", qux: "c", foot: "bart")
109
+ subject.delete(:bar).should == Hamsterdam.hash(foo: "a", qux: "c")
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "queue" do
115
+ describe "an empty queue" do
116
+ subject { Hamsterdam.queue }
117
+
118
+ it "is a conforming empty queue" do
119
+ subject.should == subject
120
+ subject.inspect.should == [].inspect
121
+ subject.dequeue.should == subject
122
+ subject.enqueue("a").should == Hamsterdam.queue("a")
123
+ end
124
+ end
125
+
126
+ describe "a populated queue" do
127
+ subject { Hamsterdam.queue("a", "b", "c") }
128
+
129
+ it "is a conforming populated queue" do
130
+ subject.should == subject
131
+ subject.inspect.should == ["a", "b", "c"].inspect
132
+ subject.dequeue.should == Hamsterdam.queue("b", "c")
133
+ subject.enqueue("d").should == Hamsterdam.queue("a", "b", "c", "d")
134
+ end
135
+ end
136
+ end
137
+ end
@@ -52,6 +52,21 @@ describe "Hamsterdam structures" do
52
52
  l.to_a.should == ["a", "b", "c", "a"]
53
53
  end
54
54
  end
55
+
56
+ describe "#queue" do
57
+ it "provides an empty queue" do
58
+ q = Hamsterdam.queue
59
+ q.should be_empty
60
+ end
61
+
62
+ it "provides a queue populated with passed values" do
63
+ q = Hamsterdam.queue("a", "b", "c", "d")
64
+ q.should_not be_empty
65
+ q.should have(4).items
66
+ q.peek.should == "a"
67
+ q.dequeue.to_a.should == ["b", "c", "d"]
68
+ end
69
+ end
55
70
  end
56
71
 
57
72
  describe "Struct.define" do
metadata CHANGED
@@ -1,163 +1,146 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hamsterdam
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 1.0.9
4
+ version: 1.0.10
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - David Crosby
9
- autorequire:
9
+ - Patrick Bacon
10
+ autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2013-07-31 00:00:00.000000000 Z
13
+ date: 2013-08-21 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: hamster
16
- version_requirements: !ruby/object:Gem::Requirement
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
17
19
  requirements:
18
20
  - - ! '>='
19
21
  - !ruby/object:Gem::Version
20
- version: !binary |-
21
- MA==
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
22
26
  none: false
23
- requirement: !ruby/object:Gem::Requirement
24
27
  requirements:
25
28
  - - ! '>='
26
29
  - !ruby/object:Gem::Version
27
- version: !binary |-
28
- MA==
29
- none: false
30
- prerelease: false
31
- type: :runtime
30
+ version: '0'
32
31
  - !ruby/object:Gem::Dependency
33
32
  name: rake
34
- version_requirements: !ruby/object:Gem::Requirement
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
35
  requirements:
36
36
  - - ! '>='
37
37
  - !ruby/object:Gem::Version
38
- version: !binary |-
39
- MA==
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
40
42
  none: false
41
- requirement: !ruby/object:Gem::Requirement
42
43
  requirements:
43
44
  - - ! '>='
44
45
  - !ruby/object:Gem::Version
45
- version: !binary |-
46
- MA==
47
- none: false
48
- prerelease: false
49
- type: :development
46
+ version: '0'
50
47
  - !ruby/object:Gem::Dependency
51
48
  name: rspec
52
- version_requirements: !ruby/object:Gem::Requirement
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
53
51
  requirements:
54
52
  - - ! '>='
55
53
  - !ruby/object:Gem::Version
56
- version: !binary |-
57
- MA==
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
58
  none: false
59
- requirement: !ruby/object:Gem::Requirement
60
59
  requirements:
61
60
  - - ! '>='
62
61
  - !ruby/object:Gem::Version
63
- version: !binary |-
64
- MA==
65
- none: false
66
- prerelease: false
67
- type: :development
62
+ version: '0'
68
63
  - !ruby/object:Gem::Dependency
69
64
  name: simplecov
70
- version_requirements: !ruby/object:Gem::Requirement
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
71
67
  requirements:
72
68
  - - ! '>='
73
69
  - !ruby/object:Gem::Version
74
- version: !binary |-
75
- MA==
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
76
74
  none: false
77
- requirement: !ruby/object:Gem::Requirement
78
75
  requirements:
79
76
  - - ! '>='
80
77
  - !ruby/object:Gem::Version
81
- version: !binary |-
82
- MA==
83
- none: false
84
- prerelease: false
85
- type: :development
78
+ version: '0'
86
79
  - !ruby/object:Gem::Dependency
87
80
  name: pry
88
- version_requirements: !ruby/object:Gem::Requirement
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
89
83
  requirements:
90
84
  - - ! '>='
91
85
  - !ruby/object:Gem::Version
92
- version: !binary |-
93
- MA==
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
94
90
  none: false
95
- requirement: !ruby/object:Gem::Requirement
96
91
  requirements:
97
92
  - - ! '>='
98
93
  - !ruby/object:Gem::Version
99
- version: !binary |-
100
- MA==
101
- none: false
102
- prerelease: false
103
- type: :development
94
+ version: '0'
104
95
  description: Immutable Struct-like record structures based on Hamster.
105
96
  email:
106
97
  - david.crosby@atomicobject.com
98
+ - bacon@atomicobject.com
107
99
  executables: []
108
100
  extensions: []
109
101
  extra_rdoc_files: []
110
102
  files:
111
103
  - README.md
112
104
  - CHANGELOG
113
- - !binary |-
114
- bGliL2hhbXN0ZXJkYW0ucmI=
115
- - !binary |-
116
- bGliL2hhbXN0ZXJkYW0vY2xqLnJi
117
- - !binary |-
118
- bGliL2hhbXN0ZXJkYW0vdmVyc2lvbi5yYg==
119
- - !binary |-
120
- c3BlYy9oYW1zdGVyZGFtX3NwZWMucmI=
121
- - !binary |-
122
- c3BlYy9zcGVjX2hlbHBlci5yYg==
123
- - !binary |-
124
- c3BlYy9zdXBwb3J0L2hhbXN0ZXJkYW1faGVscGVycy5yYg==
105
+ - lib/hamsterdam/clj.rb
106
+ - lib/hamsterdam/version.rb
107
+ - lib/hamsterdam.rb
108
+ - spec/collections_spec.rb
109
+ - spec/hamsterdam_spec.rb
110
+ - spec/spec_helper.rb
111
+ - spec/support/hamsterdam_helpers.rb
125
112
  homepage: https://github.com/atomicobject/hamsterdam
126
113
  licenses: []
127
- post_install_message:
114
+ post_install_message:
128
115
  rdoc_options: []
129
116
  require_paths:
130
117
  - lib
131
118
  required_ruby_version: !ruby/object:Gem::Requirement
119
+ none: false
132
120
  requirements:
133
121
  - - ! '>='
134
122
  - !ruby/object:Gem::Version
123
+ version: '0'
135
124
  segments:
136
125
  - 0
137
- hash: 2
138
- version: !binary |-
139
- MA==
140
- none: false
126
+ hash: -1856113987643234250
141
127
  required_rubygems_version: !ruby/object:Gem::Requirement
128
+ none: false
142
129
  requirements:
143
130
  - - ! '>='
144
131
  - !ruby/object:Gem::Version
132
+ version: '0'
145
133
  segments:
146
134
  - 0
147
- hash: 2
148
- version: !binary |-
149
- MA==
150
- none: false
135
+ hash: -1856113987643234250
151
136
  requirements: []
152
- rubyforge_project:
153
- rubygems_version: 1.8.24
154
- signing_key:
137
+ rubyforge_project:
138
+ rubygems_version: 1.8.25
139
+ signing_key:
155
140
  specification_version: 3
156
141
  summary: Immutable Struct-like record structures based on Hamster.
157
142
  test_files:
158
- - !binary |-
159
- c3BlYy9oYW1zdGVyZGFtX3NwZWMucmI=
160
- - !binary |-
161
- c3BlYy9zcGVjX2hlbHBlci5yYg==
162
- - !binary |-
163
- c3BlYy9zdXBwb3J0L2hhbXN0ZXJkYW1faGVscGVycy5yYg==
143
+ - spec/collections_spec.rb
144
+ - spec/hamsterdam_spec.rb
145
+ - spec/spec_helper.rb
146
+ - spec/support/hamsterdam_helpers.rb