hamster 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +16 -0
- data/lib/hamster/core_ext/enumerable.rb +9 -1
- data/lib/hamster/hash.rb +7 -0
- data/lib/hamster/list.rb +110 -109
- data/lib/hamster/set.rb +4 -3
- data/lib/hamster/sorter.rb +19 -0
- data/lib/hamster/tuple.rb +7 -0
- data/lib/hamster/version.rb +1 -1
- data/spec/hamster/core_ext/array_spec.rb +20 -0
- data/spec/hamster/core_ext/enumerable_spec.rb +1 -1
- data/spec/hamster/core_ext/io_spec.rb +1 -1
- data/spec/hamster/hash/all_spec.rb +1 -1
- data/spec/hamster/hash/any_spec.rb +1 -1
- data/spec/hamster/hash/construction_spec.rb +1 -1
- data/spec/hamster/hash/copying_spec.rb +1 -1
- data/spec/hamster/hash/delete_spec.rb +1 -1
- data/spec/hamster/hash/each_spec.rb +1 -1
- data/spec/hamster/hash/empty_spec.rb +1 -1
- data/spec/hamster/hash/eql_spec.rb +1 -1
- data/spec/hamster/hash/filter_spec.rb +1 -1
- data/spec/hamster/hash/find_spec.rb +58 -0
- data/spec/hamster/hash/get_spec.rb +1 -1
- data/spec/hamster/hash/has_key_spec.rb +7 -3
- data/spec/hamster/hash/inspect_spec.rb +1 -1
- data/spec/hamster/hash/map_spec.rb +1 -1
- data/spec/hamster/hash/none_spec.rb +1 -1
- data/spec/hamster/hash/put_spec.rb +1 -1
- data/spec/hamster/hash/reduce_spec.rb +1 -1
- data/spec/hamster/hash/remove_spec.rb +1 -1
- data/spec/hamster/hash/size_spec.rb +1 -1
- data/spec/hamster/hash/uniq_spec.rb +1 -1
- data/spec/hamster/list/all_spec.rb +1 -1
- data/spec/hamster/list/any_spec.rb +1 -1
- data/spec/hamster/list/append_spec.rb +1 -1
- data/spec/hamster/list/at_spec.rb +1 -1
- data/spec/hamster/list/break_spec.rb +1 -1
- data/spec/hamster/list/cadr_spec.rb +1 -1
- data/spec/hamster/list/chunk_spec.rb +1 -1
- data/spec/hamster/list/clear_spec.rb +1 -1
- data/spec/hamster/list/combinations_spec.rb +1 -1
- data/spec/hamster/list/compact_spec.rb +1 -1
- data/spec/hamster/list/cons_spec.rb +1 -1
- data/spec/hamster/list/construction_spec.rb +1 -1
- data/spec/hamster/list/copying_spec.rb +1 -1
- data/spec/hamster/list/count_spec.rb +1 -1
- data/spec/hamster/list/cycle_spec.rb +1 -1
- data/spec/hamster/list/drop_spec.rb +1 -1
- data/spec/hamster/list/drop_while_spec.rb +1 -1
- data/spec/hamster/list/each_slice_spec.rb +1 -1
- data/spec/hamster/list/each_spec.rb +1 -1
- data/spec/hamster/list/elem_index_spec.rb +58 -0
- data/spec/hamster/list/elem_indices_spec.rb +45 -0
- data/spec/hamster/list/empty_spec.rb +1 -1
- data/spec/hamster/list/eql_spec.rb +1 -1
- data/spec/hamster/list/filter_spec.rb +1 -1
- data/spec/hamster/list/find_index_spec.rb +58 -0
- data/spec/hamster/list/find_indices_spec.rb +45 -0
- data/spec/hamster/list/find_spec.rb +1 -1
- data/spec/hamster/list/flatten_spec.rb +1 -1
- data/spec/hamster/list/grep_spec.rb +1 -1
- data/spec/hamster/list/group_by_spec.rb +1 -1
- data/spec/hamster/list/head_spec.rb +1 -1
- data/spec/hamster/list/include_spec.rb +1 -1
- data/spec/hamster/list/init_spec.rb +1 -1
- data/spec/hamster/list/inits_spec.rb +1 -1
- data/spec/hamster/list/inspect_spec.rb +1 -1
- data/spec/hamster/list/intersperse_spec.rb +1 -1
- data/spec/hamster/list/join_spec.rb +1 -1
- data/spec/hamster/list/last_spec.rb +1 -1
- data/spec/hamster/list/map_spec.rb +1 -1
- data/spec/hamster/list/maximum_spec.rb +1 -1
- data/spec/hamster/list/minimum_spec.rb +1 -1
- data/spec/hamster/list/none_spec.rb +1 -1
- data/spec/hamster/list/one_spec.rb +2 -2
- data/spec/hamster/list/partition_spec.rb +1 -1
- data/spec/hamster/list/product_spec.rb +1 -1
- data/spec/hamster/list/reduce_spec.rb +1 -1
- data/spec/hamster/list/remove_spec.rb +1 -1
- data/spec/hamster/list/reverse_spec.rb +1 -1
- data/spec/hamster/list/size_spec.rb +1 -1
- data/spec/hamster/list/slice_spec.rb +1 -1
- data/spec/hamster/list/sorting_spec.rb +1 -1
- data/spec/hamster/list/span_spec.rb +1 -1
- data/spec/hamster/list/split_at_spec.rb +1 -1
- data/spec/hamster/list/sum_spec.rb +1 -1
- data/spec/hamster/list/tail_spec.rb +1 -1
- data/spec/hamster/list/tails_spec.rb +1 -1
- data/spec/hamster/list/take_spec.rb +1 -1
- data/spec/hamster/list/take_while_spec.rb +1 -1
- data/spec/hamster/list/to_a_spec.rb +1 -1
- data/spec/hamster/list/to_ary_spec.rb +1 -1
- data/spec/hamster/list/to_list_spec.rb +1 -1
- data/spec/hamster/list/union_spec.rb +1 -1
- data/spec/hamster/list/uniq_spec.rb +1 -1
- data/spec/hamster/list/zip_spec.rb +1 -1
- data/spec/hamster/set/add_spec.rb +1 -1
- data/spec/hamster/set/all_spec.rb +1 -1
- data/spec/hamster/set/any_spec.rb +1 -1
- data/spec/hamster/set/clear_spec.rb +1 -1
- data/spec/hamster/set/compact_spec.rb +1 -1
- data/spec/hamster/set/construction_spec.rb +1 -1
- data/spec/hamster/set/copying_spec.rb +1 -1
- data/spec/hamster/set/count_spec.rb +1 -1
- data/spec/hamster/set/delete_spec.rb +1 -1
- data/spec/hamster/set/each_spec.rb +1 -1
- data/spec/hamster/set/empty_spec.rb +1 -1
- data/spec/hamster/set/eql_spec.rb +1 -1
- data/spec/hamster/set/filter_spec.rb +1 -1
- data/spec/hamster/set/find_spec.rb +1 -1
- data/spec/hamster/set/grep_spec.rb +1 -1
- data/spec/hamster/set/group_by_spec.rb +1 -1
- data/spec/hamster/set/head_spec.rb +1 -1
- data/spec/hamster/set/include_spec.rb +1 -1
- data/spec/hamster/set/inspect_spec.rb +1 -1
- data/spec/hamster/set/join_spec.rb +1 -1
- data/spec/hamster/set/map_spec.rb +1 -1
- data/spec/hamster/set/maximum_spec.rb +1 -1
- data/spec/hamster/set/minimum_spec.rb +1 -1
- data/spec/hamster/set/none_spec.rb +1 -1
- data/spec/hamster/set/one_spec.rb +2 -2
- data/spec/hamster/set/partition_spec.rb +1 -1
- data/spec/hamster/set/product_spec.rb +1 -1
- data/spec/hamster/set/reduce_spec.rb +1 -1
- data/spec/hamster/set/remove_spec.rb +1 -1
- data/spec/hamster/set/size_spec.rb +1 -1
- data/spec/hamster/set/sorting_spec.rb +1 -1
- data/spec/hamster/set/sum_spec.rb +1 -1
- data/spec/hamster/set/to_a_spec.rb +1 -1
- data/spec/hamster/set/to_list_spec.rb +1 -1
- data/spec/hamster/set/to_set_spec.rb +1 -1
- data/spec/hamster/set/uniq_spec.rb +1 -1
- data/spec/hamster/stack/clear_spec.rb +1 -1
- data/spec/hamster/stack/construction_spec.rb +1 -1
- data/spec/hamster/stack/copying_spec.rb +1 -1
- data/spec/hamster/stack/empty_spec.rb +1 -1
- data/spec/hamster/stack/eql_spec.rb +1 -1
- data/spec/hamster/stack/inspect_spec.rb +1 -1
- data/spec/hamster/stack/pop_spec.rb +1 -1
- data/spec/hamster/stack/push_spec.rb +1 -1
- data/spec/hamster/stack/size_spec.rb +1 -1
- data/spec/hamster/stack/to_a_spec.rb +1 -1
- data/spec/hamster/stack/to_list_spec.rb +1 -1
- data/spec/hamster/stack/top_spec.rb +1 -1
- data/spec/hamster/trie/remove_spec.rb +1 -1
- data/spec/hamster/tuple/copying_spec.rb +1 -1
- data/spec/hamster/tuple/eql_spec.rb +61 -0
- data/spec/hamster/tuple/first_spec.rb +1 -1
- data/spec/hamster/tuple/inspect_spec.rb +1 -1
- data/spec/hamster/tuple/last_spec.rb +1 -1
- data/spec/hamster/tuple/to_a_spec.rb +1 -1
- data/spec/hamster/tuple/to_ary_spec.rb +1 -1
- data/spec/spec_helper.rb +9 -6
- metadata +20 -148
- data/lib/hamster.rbc +0 -133
- data/lib/hamster/core_ext.rbc +0 -69
- data/lib/hamster/core_ext/enumerable.rbc +0 -481
- data/lib/hamster/core_ext/io.rbc +0 -652
- data/lib/hamster/hash.rbc +0 -3537
- data/lib/hamster/list.rbc +0 -14428
- data/lib/hamster/set.rbc +0 -5541
- data/lib/hamster/stack.rbc +0 -1623
- data/lib/hamster/trie.rbc +0 -3449
- data/lib/hamster/tuple.rbc +0 -800
- data/lib/hamster/version.rbc +0 -143
- data/spec/hamster/core_ext/enumerable_spec.rbc +0 -751
- data/spec/hamster/core_ext/io_spec.rbc +0 -413
- data/spec/hamster/hash/all_spec.rbc +0 -1312
- data/spec/hamster/hash/any_spec.rbc +0 -1501
- data/spec/hamster/hash/construction_spec.rbc +0 -750
- data/spec/hamster/hash/copying_spec.rbc +0 -524
- data/spec/hamster/hash/delete_spec.rbc +0 -1146
- data/spec/hamster/hash/each_spec.rbc +0 -997
- data/spec/hamster/hash/empty_spec.rbc +0 -597
- data/spec/hamster/hash/eql_spec.rbc +0 -1704
- data/spec/hamster/hash/filter_spec.rbc +0 -1450
- data/spec/hamster/hash/get_spec.rbc +0 -793
- data/spec/hamster/hash/has_key_spec.rbc +0 -720
- data/spec/hamster/hash/inspect_spec.rbc +0 -658
- data/spec/hamster/hash/map_spec.rbc +0 -1471
- data/spec/hamster/hash/none_spec.rbc +0 -1404
- data/spec/hamster/hash/put_spec.rbc +0 -1346
- data/spec/hamster/hash/reduce_spec.rbc +0 -1255
- data/spec/hamster/hash/remove_spec.rbc +0 -1467
- data/spec/hamster/hash/size_spec.rbc +0 -598
- data/spec/hamster/hash/uniq_spec.rbc +0 -529
- data/spec/hamster/list/all_spec.rbc +0 -2158
- data/spec/hamster/list/any_spec.rbc +0 -1751
- data/spec/hamster/list/append_spec.rbc +0 -1193
- data/spec/hamster/list/break_spec.rbc +0 -1804
- data/spec/hamster/list/cadr_spec.rbc +0 -1065
- data/spec/hamster/list/chunk_spec.rbc +0 -920
- data/spec/hamster/list/clear_spec.rbc +0 -635
- data/spec/hamster/list/combinations_spec.rbc +0 -1308
- data/spec/hamster/list/compact_spec.rbc +0 -987
- data/spec/hamster/list/cons_spec.rbc +0 -853
- data/spec/hamster/list/construction_spec.rbc +0 -2532
- data/spec/hamster/list/copying_spec.rbc +0 -576
- data/spec/hamster/list/count_spec.rbc +0 -1216
- data/spec/hamster/list/cycle_spec.rbc +0 -1003
- data/spec/hamster/list/drop_spec.rbc +0 -949
- data/spec/hamster/list/drop_while_spec.rbc +0 -1289
- data/spec/hamster/list/each_slice_spec.rbc +0 -1659
- data/spec/hamster/list/each_spec.rbc +0 -1403
- data/spec/hamster/list/empty_spec.rbc +0 -947
- data/spec/hamster/list/eql_spec.rbc +0 -1669
- data/spec/hamster/list/filter_spec.rbc +0 -1379
- data/spec/hamster/list/find_spec.rbc +0 -1322
- data/spec/hamster/list/flatten_spec.rbc +0 -954
- data/spec/hamster/list/grep_spec.rbc +0 -1517
- data/spec/hamster/list/group_by_spec.rbc +0 -1536
- data/spec/hamster/list/head_spec.rbc +0 -948
- data/spec/hamster/list/include_spec.rbc +0 -1044
- data/spec/hamster/list/init_spec.rbc +0 -877
- data/spec/hamster/list/inits_spec.rbc +0 -988
- data/spec/hamster/list/inspect_spec.rbc +0 -855
- data/spec/hamster/list/intersperse_spec.rbc +0 -912
- data/spec/hamster/list/join_spec.rbc +0 -1357
- data/spec/hamster/list/last_spec.rbc +0 -856
- data/spec/hamster/list/map_spec.rbc +0 -1548
- data/spec/hamster/list/maximum_spec.rbc +0 -1498
- data/spec/hamster/list/minimum_spec.rbc +0 -1498
- data/spec/hamster/list/none_spec.rbc +0 -1647
- data/spec/hamster/list/one_spec.rbc +0 -1664
- data/spec/hamster/list/partition_spec.rbc +0 -1528
- data/spec/hamster/list/product_spec.rbc +0 -844
- data/spec/hamster/list/reduce_spec.rbc +0 -1943
- data/spec/hamster/list/remove_spec.rbc +0 -1518
- data/spec/hamster/list/reverse_spec.rbc +0 -1204
- data/spec/hamster/list/size_spec.rbc +0 -920
- data/spec/hamster/list/sorting_spec.rbc +0 -1612
- data/spec/hamster/list/span_spec.rbc +0 -1798
- data/spec/hamster/list/split_at_spec.rbc +0 -1139
- data/spec/hamster/list/sum_spec.rbc +0 -842
- data/spec/hamster/list/tail_spec.rbc +0 -994
- data/spec/hamster/list/tails_spec.rbc +0 -988
- data/spec/hamster/list/take_spec.rbc +0 -943
- data/spec/hamster/list/take_while_spec.rbc +0 -1399
- data/spec/hamster/list/to_a_spec.rbc +0 -1140
- data/spec/hamster/list/to_ary_spec.rbc +0 -1128
- data/spec/hamster/list/to_list_spec.rbc +0 -561
- data/spec/hamster/list/union_spec.rbc +0 -1147
- data/spec/hamster/list/uniq_spec.rbc +0 -1007
- data/spec/hamster/list/zip_spec.rbc +0 -1020
- data/spec/hamster/set/add_spec.rbc +0 -1051
- data/spec/hamster/set/all_spec.rbc +0 -1418
- data/spec/hamster/set/any_spec.rbc +0 -1449
- data/spec/hamster/set/compact_spec.rbc +0 -807
- data/spec/hamster/set/construction_spec.rbc +0 -426
- data/spec/hamster/set/copying_spec.rbc +0 -474
- data/spec/hamster/set/count_spec.rbc +0 -953
- data/spec/hamster/set/delete_spec.rbc +0 -958
- data/spec/hamster/set/each_spec.rbc +0 -909
- data/spec/hamster/set/empty_spec.rbc +0 -656
- data/spec/hamster/set/eql_spec.rbc +0 -1311
- data/spec/hamster/set/filter_spec.rbc +0 -1289
- data/spec/hamster/set/find_spec.rbc +0 -1017
- data/spec/hamster/set/grep_spec.rbc +0 -1181
- data/spec/hamster/set/group_by_spec.rbc +0 -1276
- data/spec/hamster/set/head_spec.rbc +0 -659
- data/spec/hamster/set/include_spec.rbc +0 -668
- data/spec/hamster/set/inspect_spec.rbc +0 -593
- data/spec/hamster/set/join_spec.rbc +0 -1075
- data/spec/hamster/set/map_spec.rbc +0 -1302
- data/spec/hamster/set/maximum_spec.rbc +0 -1334
- data/spec/hamster/set/minimum_spec.rbc +0 -1334
- data/spec/hamster/set/none_spec.rbc +0 -1348
- data/spec/hamster/set/one_spec.rbc +0 -1708
- data/spec/hamster/set/partition_spec.rbc +0 -1348
- data/spec/hamster/set/product_spec.rbc +0 -581
- data/spec/hamster/set/reduce_spec.rbc +0 -1198
- data/spec/hamster/set/remove_spec.rbc +0 -1295
- data/spec/hamster/set/size_spec.rbc +0 -533
- data/spec/hamster/set/sorting_spec.rbc +0 -1228
- data/spec/hamster/set/sum_spec.rbc +0 -579
- data/spec/hamster/set/to_a_spec.rbc +0 -774
- data/spec/hamster/set/to_list_spec.rbc +0 -862
- data/spec/hamster/set/to_set_spec.rbc +0 -561
- data/spec/hamster/set/uniq_spec.rbc +0 -479
- data/spec/hamster/stack/clear_spec.rbc +0 -635
- data/spec/hamster/stack/construction_spec.rbc +0 -872
- data/spec/hamster/stack/copying_spec.rbc +0 -561
- data/spec/hamster/stack/empty_spec.rbc +0 -577
- data/spec/hamster/stack/eql_spec.rbc +0 -1283
- data/spec/hamster/stack/inspect_spec.rbc +0 -592
- data/spec/hamster/stack/pop_spec.rbc +0 -1098
- data/spec/hamster/stack/push_spec.rbc +0 -853
- data/spec/hamster/stack/size_spec.rbc +0 -657
- data/spec/hamster/stack/to_a_spec.rbc +0 -801
- data/spec/hamster/stack/to_list_spec.rbc +0 -635
- data/spec/hamster/stack/top_spec.rbc +0 -683
- data/spec/hamster/trie/remove_spec.rbc +0 -40
- data/spec/hamster/tuple/copying_spec.rbc +0 -511
- data/spec/hamster/tuple/first_spec.rbc +0 -423
- data/spec/hamster/tuple/inspect_spec.rbc +0 -423
- data/spec/hamster/tuple/last_spec.rbc +0 -423
- data/spec/hamster/tuple/to_a_spec.rbc +0 -803
- data/spec/hamster/tuple/to_ary_spec.rbc +0 -908
- data/spec/spec_helper.rbc +0 -184
- data/tasks/spec.rbc +0 -202
data/History.rdoc
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
=== 0.2.6 / 2010-01-29
|
2
|
+
|
3
|
+
* Implement Array#to_list.
|
4
|
+
|
5
|
+
* Simplify (and improve performance of) conversions from arrays to lists.
|
6
|
+
|
7
|
+
* Speed up Set and List sorting.
|
8
|
+
|
9
|
+
* Implement Hash#find (aliased as #detect).
|
10
|
+
|
11
|
+
* Implement List#elem_indices, List#find_indices (aliased as #indices as appropriate).
|
12
|
+
|
13
|
+
* Implement List#index as per MRI and alias as #elem_index, and #find_index as appropriate.
|
14
|
+
|
15
|
+
* Remove some redundant construction of streams.
|
16
|
+
|
1
17
|
=== 0.2.5 / 2010-01-19
|
2
18
|
|
3
19
|
* Implement Set#clear.
|
@@ -7,7 +7,9 @@ module Hamster
|
|
7
7
|
module Enumerable
|
8
8
|
|
9
9
|
def to_list
|
10
|
-
|
10
|
+
list = EmptyList
|
11
|
+
reverse_each { |item| list = list.cons(item) }
|
12
|
+
list
|
11
13
|
end
|
12
14
|
|
13
15
|
end
|
@@ -21,3 +23,9 @@ module Enumerable
|
|
21
23
|
include Hamster::CoreExt::Enumerable
|
22
24
|
|
23
25
|
end
|
26
|
+
|
27
|
+
class Array
|
28
|
+
|
29
|
+
include Hamster::CoreExt::Enumerable
|
30
|
+
|
31
|
+
end
|
data/lib/hamster/hash.rb
CHANGED
@@ -113,6 +113,13 @@ module Hamster
|
|
113
113
|
true
|
114
114
|
end
|
115
115
|
|
116
|
+
def find
|
117
|
+
return nil unless block_given?
|
118
|
+
each { |key, value| return Tuple.new(key, value) if yield(key, value) }
|
119
|
+
nil
|
120
|
+
end
|
121
|
+
def_delegator :self, :find, :detect
|
122
|
+
|
116
123
|
def eql?(other)
|
117
124
|
other.is_a?(self.class) && @trie.eql?(other.instance_eval{@trie})
|
118
125
|
end
|
data/lib/hamster/list.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'forwardable'
|
2
2
|
require 'thread'
|
3
3
|
|
4
|
+
require 'hamster/core_ext/enumerable'
|
4
5
|
require 'hamster/tuple'
|
6
|
+
require 'hamster/sorter'
|
5
7
|
require 'hamster/hash'
|
6
8
|
require 'hamster/set'
|
7
9
|
|
@@ -12,7 +14,7 @@ module Hamster
|
|
12
14
|
extend Forwardable
|
13
15
|
|
14
16
|
def list(*items)
|
15
|
-
items.
|
17
|
+
items.to_list
|
16
18
|
end
|
17
19
|
|
18
20
|
def stream(&block)
|
@@ -78,11 +80,8 @@ module Hamster
|
|
78
80
|
def map(&block)
|
79
81
|
return self unless block_given?
|
80
82
|
Stream.new do
|
81
|
-
if empty?
|
82
|
-
|
83
|
-
else
|
84
|
-
Sequence.new(yield(head), tail.map(&block))
|
85
|
-
end
|
83
|
+
next self if empty?
|
84
|
+
Sequence.new(yield(head), tail.map(&block))
|
86
85
|
end
|
87
86
|
end
|
88
87
|
def_delegator :self, :map, :collect
|
@@ -104,13 +103,9 @@ module Hamster
|
|
104
103
|
def filter(&block)
|
105
104
|
return self unless block_given?
|
106
105
|
Stream.new do
|
107
|
-
if empty?
|
108
|
-
|
109
|
-
|
110
|
-
Sequence.new(head, tail.filter(&block))
|
111
|
-
else
|
112
|
-
tail.filter(&block)
|
113
|
-
end
|
106
|
+
next self if empty?
|
107
|
+
next Sequence.new(head, tail.filter(&block)) if yield(head)
|
108
|
+
tail.filter(&block)
|
114
109
|
end
|
115
110
|
end
|
116
111
|
def_delegator :self, :filter, :select
|
@@ -126,50 +121,47 @@ module Hamster
|
|
126
121
|
def take_while(&block)
|
127
122
|
return self unless block_given?
|
128
123
|
Stream.new do
|
129
|
-
if empty?
|
130
|
-
|
131
|
-
|
132
|
-
Sequence.new(head, tail.take_while(&block))
|
133
|
-
else
|
134
|
-
EmptyList
|
135
|
-
end
|
124
|
+
next self if empty?
|
125
|
+
next Sequence.new(head, tail.take_while(&block)) if yield(head)
|
126
|
+
EmptyList
|
136
127
|
end
|
137
128
|
end
|
138
129
|
|
139
130
|
def drop_while(&block)
|
131
|
+
# return self unless block_given?
|
132
|
+
# return self if empty?
|
133
|
+
# return self unless yield(head)
|
134
|
+
# tail.drop_while(&block)
|
140
135
|
return self unless block_given?
|
141
136
|
Stream.new do
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
tail.drop_while(&block)
|
146
|
-
else
|
147
|
-
self
|
137
|
+
list = self
|
138
|
+
while !list.empty? && yield(list.head)
|
139
|
+
list = list.tail
|
148
140
|
end
|
141
|
+
list
|
149
142
|
end
|
150
143
|
end
|
151
144
|
|
152
145
|
def take(number)
|
153
146
|
Stream.new do
|
154
|
-
if empty?
|
155
|
-
|
156
|
-
|
157
|
-
Sequence.new(head, tail.take(number - 1))
|
158
|
-
else
|
159
|
-
EmptyList
|
160
|
-
end
|
147
|
+
next self if empty?
|
148
|
+
next Sequence.new(head, tail.take(number - 1)) if number > 0
|
149
|
+
EmptyList
|
161
150
|
end
|
162
151
|
end
|
163
152
|
|
164
153
|
def drop(number)
|
154
|
+
# return self unless block_given?
|
155
|
+
# return self if empty?
|
156
|
+
# return self unless number > 0
|
157
|
+
# tail.drop(number - 1)
|
165
158
|
Stream.new do
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
else
|
171
|
-
self
|
159
|
+
list = self
|
160
|
+
while !list.empty? && number > 0
|
161
|
+
number -= 1
|
162
|
+
list = list.tail
|
172
163
|
end
|
164
|
+
list
|
173
165
|
end
|
174
166
|
end
|
175
167
|
|
@@ -240,11 +232,8 @@ module Hamster
|
|
240
232
|
|
241
233
|
def append(other)
|
242
234
|
Stream.new do
|
243
|
-
if empty?
|
244
|
-
|
245
|
-
else
|
246
|
-
Sequence.new(head, tail.append(other))
|
247
|
-
end
|
235
|
+
next other if empty?
|
236
|
+
Sequence.new(head, tail.append(other))
|
248
237
|
end
|
249
238
|
end
|
250
239
|
def_delegator :self, :append, :concat
|
@@ -252,9 +241,7 @@ module Hamster
|
|
252
241
|
def_delegator :self, :append, :+
|
253
242
|
|
254
243
|
def reverse
|
255
|
-
Stream.new
|
256
|
-
reduce(EmptyList) { |list, item| list.cons(item) }
|
257
|
-
end
|
244
|
+
Stream.new { reduce(EmptyList) { |list, item| list.cons(item) } }
|
258
245
|
end
|
259
246
|
|
260
247
|
def minimum(&block)
|
@@ -275,21 +262,15 @@ module Hamster
|
|
275
262
|
|
276
263
|
def zip(other)
|
277
264
|
Stream.new do
|
278
|
-
if empty? && other.empty?
|
279
|
-
|
280
|
-
else
|
281
|
-
Sequence.new(Sequence.new(head, Sequence.new(other.head)), tail.zip(other.tail))
|
282
|
-
end
|
265
|
+
next self if empty? && other.empty?
|
266
|
+
Sequence.new(Sequence.new(head, Sequence.new(other.head)), tail.zip(other.tail))
|
283
267
|
end
|
284
268
|
end
|
285
269
|
|
286
270
|
def cycle
|
287
271
|
Stream.new do
|
288
|
-
if empty?
|
289
|
-
|
290
|
-
else
|
291
|
-
Sequence.new(head, tail.append(self.cycle))
|
292
|
-
end
|
272
|
+
next self if empty?
|
273
|
+
Sequence.new(head, tail.append(self.cycle))
|
293
274
|
end
|
294
275
|
end
|
295
276
|
|
@@ -303,7 +284,7 @@ module Hamster
|
|
303
284
|
end
|
304
285
|
|
305
286
|
def break(&block)
|
306
|
-
return
|
287
|
+
return span unless block_given?
|
307
288
|
span { |item| !yield(item) }
|
308
289
|
end
|
309
290
|
|
@@ -316,16 +297,12 @@ module Hamster
|
|
316
297
|
end
|
317
298
|
|
318
299
|
def sort(&block)
|
319
|
-
Stream.new
|
320
|
-
Hamster.list(*to_a.sort(&block))
|
321
|
-
end
|
300
|
+
Stream.new { Sorter.new(self).sort(&block).to_list }
|
322
301
|
end
|
323
302
|
|
324
303
|
def sort_by(&block)
|
325
304
|
return sort unless block_given?
|
326
|
-
Stream.new
|
327
|
-
Hamster.list(*to_a.sort_by(&block))
|
328
|
-
end
|
305
|
+
Stream.new { Sorter.new(self).sort_by(&block).to_list }
|
329
306
|
end
|
330
307
|
|
331
308
|
def join(sep = "")
|
@@ -336,23 +313,16 @@ module Hamster
|
|
336
313
|
|
337
314
|
def intersperse(sep)
|
338
315
|
Stream.new do
|
339
|
-
if tail.empty?
|
340
|
-
|
341
|
-
else
|
342
|
-
Sequence.new(head, Sequence.new(sep, tail.intersperse(sep)))
|
343
|
-
end
|
316
|
+
next self if tail.empty?
|
317
|
+
Sequence.new(head, Sequence.new(sep, tail.intersperse(sep)))
|
344
318
|
end
|
345
319
|
end
|
346
320
|
|
347
321
|
def uniq(items = Set.new)
|
348
322
|
Stream.new do
|
349
|
-
if empty?
|
350
|
-
|
351
|
-
|
352
|
-
tail.uniq(items)
|
353
|
-
else
|
354
|
-
Sequence.new(head, tail.uniq(items.add(head)))
|
355
|
-
end
|
323
|
+
next self if empty?
|
324
|
+
next tail.uniq(items) if items.include?(head)
|
325
|
+
Sequence.new(head, tail.uniq(items.add(head)))
|
356
326
|
end
|
357
327
|
end
|
358
328
|
def_delegator :self, :uniq, :nub
|
@@ -386,32 +356,23 @@ module Hamster
|
|
386
356
|
|
387
357
|
def tails
|
388
358
|
Stream.new do
|
389
|
-
if empty?
|
390
|
-
|
391
|
-
else
|
392
|
-
Sequence.new(self, tail.tails)
|
393
|
-
end
|
359
|
+
next Sequence.new(self) if empty?
|
360
|
+
Sequence.new(self, tail.tails)
|
394
361
|
end
|
395
362
|
end
|
396
363
|
|
397
364
|
def inits
|
398
365
|
Stream.new do
|
399
|
-
if empty?
|
400
|
-
|
401
|
-
else
|
402
|
-
Sequence.new(EmptyList, tail.inits.map { |list| list.cons(head) })
|
403
|
-
end
|
366
|
+
next Sequence.new(self) if empty?
|
367
|
+
Sequence.new(EmptyList, tail.inits.map { |list| list.cons(head) })
|
404
368
|
end
|
405
369
|
end
|
406
370
|
|
407
371
|
def combinations(number)
|
408
372
|
return Sequence.new(EmptyList) if number == 0
|
409
373
|
Stream.new do
|
410
|
-
if empty?
|
411
|
-
|
412
|
-
else
|
413
|
-
tail.combinations(number - 1).map { |list| list.cons(head) }.append(tail.combinations(number))
|
414
|
-
end
|
374
|
+
next self if empty?
|
375
|
+
tail.combinations(number - 1).map { |list| list.cons(head) }.append(tail.combinations(number))
|
415
376
|
end
|
416
377
|
end
|
417
378
|
def_delegator :self, :combinations, :combination
|
@@ -422,12 +383,9 @@ module Hamster
|
|
422
383
|
|
423
384
|
def chunk(number)
|
424
385
|
Stream.new do
|
425
|
-
if empty?
|
426
|
-
|
427
|
-
|
428
|
-
first, remainder = split_at(number)
|
429
|
-
Sequence.new(first, remainder.chunk(number))
|
430
|
-
end
|
386
|
+
next self if empty?
|
387
|
+
first, remainder = split_at(number)
|
388
|
+
Sequence.new(first, remainder.chunk(number))
|
431
389
|
end
|
432
390
|
end
|
433
391
|
|
@@ -438,13 +396,9 @@ module Hamster
|
|
438
396
|
|
439
397
|
def flatten
|
440
398
|
Stream.new do
|
441
|
-
if empty?
|
442
|
-
|
443
|
-
|
444
|
-
head.append(tail.flatten)
|
445
|
-
else
|
446
|
-
Sequence.new(head, tail.flatten)
|
447
|
-
end
|
399
|
+
next self if empty?
|
400
|
+
next head.append(tail.flatten) if head.is_a?(List)
|
401
|
+
Sequence.new(head, tail.flatten)
|
448
402
|
end
|
449
403
|
end
|
450
404
|
|
@@ -466,6 +420,50 @@ module Hamster
|
|
466
420
|
end
|
467
421
|
def_delegator :self, :slice, :[]
|
468
422
|
|
423
|
+
def find_index
|
424
|
+
# def find_index(i = 0, &block)
|
425
|
+
# return nil if empty?
|
426
|
+
# return nil unless block_given?
|
427
|
+
# return i if yield(head)
|
428
|
+
# tail.find_index(i + 1, &block)
|
429
|
+
return nil unless block_given?
|
430
|
+
i = 0
|
431
|
+
list = self
|
432
|
+
loop do
|
433
|
+
return nil if list.empty?
|
434
|
+
return i if yield(list.head)
|
435
|
+
i += 1
|
436
|
+
list = list.tail
|
437
|
+
end
|
438
|
+
end
|
439
|
+
|
440
|
+
def elem_index(object)
|
441
|
+
find_index { |item| item == object }
|
442
|
+
end
|
443
|
+
|
444
|
+
def index(object = Undefined, &block)
|
445
|
+
return elem_index(object) unless object.equal?(Undefined)
|
446
|
+
find_index(&block)
|
447
|
+
end
|
448
|
+
|
449
|
+
def find_indices(i = 0, &block)
|
450
|
+
return EmptyList unless block_given?
|
451
|
+
Stream.new do
|
452
|
+
next EmptyList if empty?
|
453
|
+
next Sequence.new(i, tail.find_indices(i + 1, &block)) if yield(head)
|
454
|
+
tail.find_indices(i + 1, &block)
|
455
|
+
end
|
456
|
+
end
|
457
|
+
|
458
|
+
def elem_indices(object)
|
459
|
+
find_indices { |item| item == object }
|
460
|
+
end
|
461
|
+
|
462
|
+
def indices(object = Undefined, &block)
|
463
|
+
return elem_indices(object) unless object.equal?(Undefined)
|
464
|
+
find_indices(&block)
|
465
|
+
end
|
466
|
+
|
469
467
|
def eql?(other)
|
470
468
|
# return true if other.equal?(self)
|
471
469
|
# return false unless other.is_a?(List)
|
@@ -511,11 +509,8 @@ module Hamster
|
|
511
509
|
private
|
512
510
|
|
513
511
|
def method_missing(name, *args, &block)
|
514
|
-
if CADR === name.to_s
|
515
|
-
|
516
|
-
else
|
517
|
-
super
|
518
|
-
end
|
512
|
+
return accessor($1) if CADR === name.to_s
|
513
|
+
super
|
519
514
|
end
|
520
515
|
|
521
516
|
# Perform compositions of <tt>car</tt> and <tt>cdr</tt> operations. Their names consist of a 'c', followed by at
|
@@ -552,6 +547,8 @@ module Hamster
|
|
552
547
|
|
553
548
|
class Stream
|
554
549
|
|
550
|
+
extend Forwardable
|
551
|
+
|
555
552
|
include List
|
556
553
|
|
557
554
|
def initialize(&block)
|
@@ -559,6 +556,10 @@ module Hamster
|
|
559
556
|
@lock = Mutex.new
|
560
557
|
end
|
561
558
|
|
559
|
+
def_delegator :target, :head
|
560
|
+
def_delegator :target, :tail
|
561
|
+
def_delegator :target, :empty?
|
562
|
+
|
562
563
|
def head
|
563
564
|
target.head
|
564
565
|
end
|
data/lib/hamster/set.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'forwardable'
|
2
2
|
|
3
3
|
require 'hamster/tuple'
|
4
|
+
require 'hamster/sorter'
|
4
5
|
require 'hamster/trie'
|
5
6
|
require 'hamster/list'
|
6
7
|
|
@@ -48,7 +49,6 @@ module Hamster
|
|
48
49
|
else
|
49
50
|
self.class.new(trie)
|
50
51
|
end
|
51
|
-
|
52
52
|
end
|
53
53
|
|
54
54
|
def each
|
@@ -152,11 +152,12 @@ module Hamster
|
|
152
152
|
end
|
153
153
|
|
154
154
|
def sort(&block)
|
155
|
-
|
155
|
+
Stream.new { Sorter.new(self).sort(&block).to_list }
|
156
156
|
end
|
157
157
|
|
158
158
|
def sort_by(&block)
|
159
|
-
|
159
|
+
return sort unless block_given?
|
160
|
+
Stream.new { Sorter.new(self).sort_by(&block).to_list }
|
160
161
|
end
|
161
162
|
|
162
163
|
def join(sep = nil)
|