hamster 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (299) hide show
  1. data/History.rdoc +16 -0
  2. data/lib/hamster/core_ext/enumerable.rb +9 -1
  3. data/lib/hamster/hash.rb +7 -0
  4. data/lib/hamster/list.rb +110 -109
  5. data/lib/hamster/set.rb +4 -3
  6. data/lib/hamster/sorter.rb +19 -0
  7. data/lib/hamster/tuple.rb +7 -0
  8. data/lib/hamster/version.rb +1 -1
  9. data/spec/hamster/core_ext/array_spec.rb +20 -0
  10. data/spec/hamster/core_ext/enumerable_spec.rb +1 -1
  11. data/spec/hamster/core_ext/io_spec.rb +1 -1
  12. data/spec/hamster/hash/all_spec.rb +1 -1
  13. data/spec/hamster/hash/any_spec.rb +1 -1
  14. data/spec/hamster/hash/construction_spec.rb +1 -1
  15. data/spec/hamster/hash/copying_spec.rb +1 -1
  16. data/spec/hamster/hash/delete_spec.rb +1 -1
  17. data/spec/hamster/hash/each_spec.rb +1 -1
  18. data/spec/hamster/hash/empty_spec.rb +1 -1
  19. data/spec/hamster/hash/eql_spec.rb +1 -1
  20. data/spec/hamster/hash/filter_spec.rb +1 -1
  21. data/spec/hamster/hash/find_spec.rb +58 -0
  22. data/spec/hamster/hash/get_spec.rb +1 -1
  23. data/spec/hamster/hash/has_key_spec.rb +7 -3
  24. data/spec/hamster/hash/inspect_spec.rb +1 -1
  25. data/spec/hamster/hash/map_spec.rb +1 -1
  26. data/spec/hamster/hash/none_spec.rb +1 -1
  27. data/spec/hamster/hash/put_spec.rb +1 -1
  28. data/spec/hamster/hash/reduce_spec.rb +1 -1
  29. data/spec/hamster/hash/remove_spec.rb +1 -1
  30. data/spec/hamster/hash/size_spec.rb +1 -1
  31. data/spec/hamster/hash/uniq_spec.rb +1 -1
  32. data/spec/hamster/list/all_spec.rb +1 -1
  33. data/spec/hamster/list/any_spec.rb +1 -1
  34. data/spec/hamster/list/append_spec.rb +1 -1
  35. data/spec/hamster/list/at_spec.rb +1 -1
  36. data/spec/hamster/list/break_spec.rb +1 -1
  37. data/spec/hamster/list/cadr_spec.rb +1 -1
  38. data/spec/hamster/list/chunk_spec.rb +1 -1
  39. data/spec/hamster/list/clear_spec.rb +1 -1
  40. data/spec/hamster/list/combinations_spec.rb +1 -1
  41. data/spec/hamster/list/compact_spec.rb +1 -1
  42. data/spec/hamster/list/cons_spec.rb +1 -1
  43. data/spec/hamster/list/construction_spec.rb +1 -1
  44. data/spec/hamster/list/copying_spec.rb +1 -1
  45. data/spec/hamster/list/count_spec.rb +1 -1
  46. data/spec/hamster/list/cycle_spec.rb +1 -1
  47. data/spec/hamster/list/drop_spec.rb +1 -1
  48. data/spec/hamster/list/drop_while_spec.rb +1 -1
  49. data/spec/hamster/list/each_slice_spec.rb +1 -1
  50. data/spec/hamster/list/each_spec.rb +1 -1
  51. data/spec/hamster/list/elem_index_spec.rb +58 -0
  52. data/spec/hamster/list/elem_indices_spec.rb +45 -0
  53. data/spec/hamster/list/empty_spec.rb +1 -1
  54. data/spec/hamster/list/eql_spec.rb +1 -1
  55. data/spec/hamster/list/filter_spec.rb +1 -1
  56. data/spec/hamster/list/find_index_spec.rb +58 -0
  57. data/spec/hamster/list/find_indices_spec.rb +45 -0
  58. data/spec/hamster/list/find_spec.rb +1 -1
  59. data/spec/hamster/list/flatten_spec.rb +1 -1
  60. data/spec/hamster/list/grep_spec.rb +1 -1
  61. data/spec/hamster/list/group_by_spec.rb +1 -1
  62. data/spec/hamster/list/head_spec.rb +1 -1
  63. data/spec/hamster/list/include_spec.rb +1 -1
  64. data/spec/hamster/list/init_spec.rb +1 -1
  65. data/spec/hamster/list/inits_spec.rb +1 -1
  66. data/spec/hamster/list/inspect_spec.rb +1 -1
  67. data/spec/hamster/list/intersperse_spec.rb +1 -1
  68. data/spec/hamster/list/join_spec.rb +1 -1
  69. data/spec/hamster/list/last_spec.rb +1 -1
  70. data/spec/hamster/list/map_spec.rb +1 -1
  71. data/spec/hamster/list/maximum_spec.rb +1 -1
  72. data/spec/hamster/list/minimum_spec.rb +1 -1
  73. data/spec/hamster/list/none_spec.rb +1 -1
  74. data/spec/hamster/list/one_spec.rb +2 -2
  75. data/spec/hamster/list/partition_spec.rb +1 -1
  76. data/spec/hamster/list/product_spec.rb +1 -1
  77. data/spec/hamster/list/reduce_spec.rb +1 -1
  78. data/spec/hamster/list/remove_spec.rb +1 -1
  79. data/spec/hamster/list/reverse_spec.rb +1 -1
  80. data/spec/hamster/list/size_spec.rb +1 -1
  81. data/spec/hamster/list/slice_spec.rb +1 -1
  82. data/spec/hamster/list/sorting_spec.rb +1 -1
  83. data/spec/hamster/list/span_spec.rb +1 -1
  84. data/spec/hamster/list/split_at_spec.rb +1 -1
  85. data/spec/hamster/list/sum_spec.rb +1 -1
  86. data/spec/hamster/list/tail_spec.rb +1 -1
  87. data/spec/hamster/list/tails_spec.rb +1 -1
  88. data/spec/hamster/list/take_spec.rb +1 -1
  89. data/spec/hamster/list/take_while_spec.rb +1 -1
  90. data/spec/hamster/list/to_a_spec.rb +1 -1
  91. data/spec/hamster/list/to_ary_spec.rb +1 -1
  92. data/spec/hamster/list/to_list_spec.rb +1 -1
  93. data/spec/hamster/list/union_spec.rb +1 -1
  94. data/spec/hamster/list/uniq_spec.rb +1 -1
  95. data/spec/hamster/list/zip_spec.rb +1 -1
  96. data/spec/hamster/set/add_spec.rb +1 -1
  97. data/spec/hamster/set/all_spec.rb +1 -1
  98. data/spec/hamster/set/any_spec.rb +1 -1
  99. data/spec/hamster/set/clear_spec.rb +1 -1
  100. data/spec/hamster/set/compact_spec.rb +1 -1
  101. data/spec/hamster/set/construction_spec.rb +1 -1
  102. data/spec/hamster/set/copying_spec.rb +1 -1
  103. data/spec/hamster/set/count_spec.rb +1 -1
  104. data/spec/hamster/set/delete_spec.rb +1 -1
  105. data/spec/hamster/set/each_spec.rb +1 -1
  106. data/spec/hamster/set/empty_spec.rb +1 -1
  107. data/spec/hamster/set/eql_spec.rb +1 -1
  108. data/spec/hamster/set/filter_spec.rb +1 -1
  109. data/spec/hamster/set/find_spec.rb +1 -1
  110. data/spec/hamster/set/grep_spec.rb +1 -1
  111. data/spec/hamster/set/group_by_spec.rb +1 -1
  112. data/spec/hamster/set/head_spec.rb +1 -1
  113. data/spec/hamster/set/include_spec.rb +1 -1
  114. data/spec/hamster/set/inspect_spec.rb +1 -1
  115. data/spec/hamster/set/join_spec.rb +1 -1
  116. data/spec/hamster/set/map_spec.rb +1 -1
  117. data/spec/hamster/set/maximum_spec.rb +1 -1
  118. data/spec/hamster/set/minimum_spec.rb +1 -1
  119. data/spec/hamster/set/none_spec.rb +1 -1
  120. data/spec/hamster/set/one_spec.rb +2 -2
  121. data/spec/hamster/set/partition_spec.rb +1 -1
  122. data/spec/hamster/set/product_spec.rb +1 -1
  123. data/spec/hamster/set/reduce_spec.rb +1 -1
  124. data/spec/hamster/set/remove_spec.rb +1 -1
  125. data/spec/hamster/set/size_spec.rb +1 -1
  126. data/spec/hamster/set/sorting_spec.rb +1 -1
  127. data/spec/hamster/set/sum_spec.rb +1 -1
  128. data/spec/hamster/set/to_a_spec.rb +1 -1
  129. data/spec/hamster/set/to_list_spec.rb +1 -1
  130. data/spec/hamster/set/to_set_spec.rb +1 -1
  131. data/spec/hamster/set/uniq_spec.rb +1 -1
  132. data/spec/hamster/stack/clear_spec.rb +1 -1
  133. data/spec/hamster/stack/construction_spec.rb +1 -1
  134. data/spec/hamster/stack/copying_spec.rb +1 -1
  135. data/spec/hamster/stack/empty_spec.rb +1 -1
  136. data/spec/hamster/stack/eql_spec.rb +1 -1
  137. data/spec/hamster/stack/inspect_spec.rb +1 -1
  138. data/spec/hamster/stack/pop_spec.rb +1 -1
  139. data/spec/hamster/stack/push_spec.rb +1 -1
  140. data/spec/hamster/stack/size_spec.rb +1 -1
  141. data/spec/hamster/stack/to_a_spec.rb +1 -1
  142. data/spec/hamster/stack/to_list_spec.rb +1 -1
  143. data/spec/hamster/stack/top_spec.rb +1 -1
  144. data/spec/hamster/trie/remove_spec.rb +1 -1
  145. data/spec/hamster/tuple/copying_spec.rb +1 -1
  146. data/spec/hamster/tuple/eql_spec.rb +61 -0
  147. data/spec/hamster/tuple/first_spec.rb +1 -1
  148. data/spec/hamster/tuple/inspect_spec.rb +1 -1
  149. data/spec/hamster/tuple/last_spec.rb +1 -1
  150. data/spec/hamster/tuple/to_a_spec.rb +1 -1
  151. data/spec/hamster/tuple/to_ary_spec.rb +1 -1
  152. data/spec/spec_helper.rb +9 -6
  153. metadata +20 -148
  154. data/lib/hamster.rbc +0 -133
  155. data/lib/hamster/core_ext.rbc +0 -69
  156. data/lib/hamster/core_ext/enumerable.rbc +0 -481
  157. data/lib/hamster/core_ext/io.rbc +0 -652
  158. data/lib/hamster/hash.rbc +0 -3537
  159. data/lib/hamster/list.rbc +0 -14428
  160. data/lib/hamster/set.rbc +0 -5541
  161. data/lib/hamster/stack.rbc +0 -1623
  162. data/lib/hamster/trie.rbc +0 -3449
  163. data/lib/hamster/tuple.rbc +0 -800
  164. data/lib/hamster/version.rbc +0 -143
  165. data/spec/hamster/core_ext/enumerable_spec.rbc +0 -751
  166. data/spec/hamster/core_ext/io_spec.rbc +0 -413
  167. data/spec/hamster/hash/all_spec.rbc +0 -1312
  168. data/spec/hamster/hash/any_spec.rbc +0 -1501
  169. data/spec/hamster/hash/construction_spec.rbc +0 -750
  170. data/spec/hamster/hash/copying_spec.rbc +0 -524
  171. data/spec/hamster/hash/delete_spec.rbc +0 -1146
  172. data/spec/hamster/hash/each_spec.rbc +0 -997
  173. data/spec/hamster/hash/empty_spec.rbc +0 -597
  174. data/spec/hamster/hash/eql_spec.rbc +0 -1704
  175. data/spec/hamster/hash/filter_spec.rbc +0 -1450
  176. data/spec/hamster/hash/get_spec.rbc +0 -793
  177. data/spec/hamster/hash/has_key_spec.rbc +0 -720
  178. data/spec/hamster/hash/inspect_spec.rbc +0 -658
  179. data/spec/hamster/hash/map_spec.rbc +0 -1471
  180. data/spec/hamster/hash/none_spec.rbc +0 -1404
  181. data/spec/hamster/hash/put_spec.rbc +0 -1346
  182. data/spec/hamster/hash/reduce_spec.rbc +0 -1255
  183. data/spec/hamster/hash/remove_spec.rbc +0 -1467
  184. data/spec/hamster/hash/size_spec.rbc +0 -598
  185. data/spec/hamster/hash/uniq_spec.rbc +0 -529
  186. data/spec/hamster/list/all_spec.rbc +0 -2158
  187. data/spec/hamster/list/any_spec.rbc +0 -1751
  188. data/spec/hamster/list/append_spec.rbc +0 -1193
  189. data/spec/hamster/list/break_spec.rbc +0 -1804
  190. data/spec/hamster/list/cadr_spec.rbc +0 -1065
  191. data/spec/hamster/list/chunk_spec.rbc +0 -920
  192. data/spec/hamster/list/clear_spec.rbc +0 -635
  193. data/spec/hamster/list/combinations_spec.rbc +0 -1308
  194. data/spec/hamster/list/compact_spec.rbc +0 -987
  195. data/spec/hamster/list/cons_spec.rbc +0 -853
  196. data/spec/hamster/list/construction_spec.rbc +0 -2532
  197. data/spec/hamster/list/copying_spec.rbc +0 -576
  198. data/spec/hamster/list/count_spec.rbc +0 -1216
  199. data/spec/hamster/list/cycle_spec.rbc +0 -1003
  200. data/spec/hamster/list/drop_spec.rbc +0 -949
  201. data/spec/hamster/list/drop_while_spec.rbc +0 -1289
  202. data/spec/hamster/list/each_slice_spec.rbc +0 -1659
  203. data/spec/hamster/list/each_spec.rbc +0 -1403
  204. data/spec/hamster/list/empty_spec.rbc +0 -947
  205. data/spec/hamster/list/eql_spec.rbc +0 -1669
  206. data/spec/hamster/list/filter_spec.rbc +0 -1379
  207. data/spec/hamster/list/find_spec.rbc +0 -1322
  208. data/spec/hamster/list/flatten_spec.rbc +0 -954
  209. data/spec/hamster/list/grep_spec.rbc +0 -1517
  210. data/spec/hamster/list/group_by_spec.rbc +0 -1536
  211. data/spec/hamster/list/head_spec.rbc +0 -948
  212. data/spec/hamster/list/include_spec.rbc +0 -1044
  213. data/spec/hamster/list/init_spec.rbc +0 -877
  214. data/spec/hamster/list/inits_spec.rbc +0 -988
  215. data/spec/hamster/list/inspect_spec.rbc +0 -855
  216. data/spec/hamster/list/intersperse_spec.rbc +0 -912
  217. data/spec/hamster/list/join_spec.rbc +0 -1357
  218. data/spec/hamster/list/last_spec.rbc +0 -856
  219. data/spec/hamster/list/map_spec.rbc +0 -1548
  220. data/spec/hamster/list/maximum_spec.rbc +0 -1498
  221. data/spec/hamster/list/minimum_spec.rbc +0 -1498
  222. data/spec/hamster/list/none_spec.rbc +0 -1647
  223. data/spec/hamster/list/one_spec.rbc +0 -1664
  224. data/spec/hamster/list/partition_spec.rbc +0 -1528
  225. data/spec/hamster/list/product_spec.rbc +0 -844
  226. data/spec/hamster/list/reduce_spec.rbc +0 -1943
  227. data/spec/hamster/list/remove_spec.rbc +0 -1518
  228. data/spec/hamster/list/reverse_spec.rbc +0 -1204
  229. data/spec/hamster/list/size_spec.rbc +0 -920
  230. data/spec/hamster/list/sorting_spec.rbc +0 -1612
  231. data/spec/hamster/list/span_spec.rbc +0 -1798
  232. data/spec/hamster/list/split_at_spec.rbc +0 -1139
  233. data/spec/hamster/list/sum_spec.rbc +0 -842
  234. data/spec/hamster/list/tail_spec.rbc +0 -994
  235. data/spec/hamster/list/tails_spec.rbc +0 -988
  236. data/spec/hamster/list/take_spec.rbc +0 -943
  237. data/spec/hamster/list/take_while_spec.rbc +0 -1399
  238. data/spec/hamster/list/to_a_spec.rbc +0 -1140
  239. data/spec/hamster/list/to_ary_spec.rbc +0 -1128
  240. data/spec/hamster/list/to_list_spec.rbc +0 -561
  241. data/spec/hamster/list/union_spec.rbc +0 -1147
  242. data/spec/hamster/list/uniq_spec.rbc +0 -1007
  243. data/spec/hamster/list/zip_spec.rbc +0 -1020
  244. data/spec/hamster/set/add_spec.rbc +0 -1051
  245. data/spec/hamster/set/all_spec.rbc +0 -1418
  246. data/spec/hamster/set/any_spec.rbc +0 -1449
  247. data/spec/hamster/set/compact_spec.rbc +0 -807
  248. data/spec/hamster/set/construction_spec.rbc +0 -426
  249. data/spec/hamster/set/copying_spec.rbc +0 -474
  250. data/spec/hamster/set/count_spec.rbc +0 -953
  251. data/spec/hamster/set/delete_spec.rbc +0 -958
  252. data/spec/hamster/set/each_spec.rbc +0 -909
  253. data/spec/hamster/set/empty_spec.rbc +0 -656
  254. data/spec/hamster/set/eql_spec.rbc +0 -1311
  255. data/spec/hamster/set/filter_spec.rbc +0 -1289
  256. data/spec/hamster/set/find_spec.rbc +0 -1017
  257. data/spec/hamster/set/grep_spec.rbc +0 -1181
  258. data/spec/hamster/set/group_by_spec.rbc +0 -1276
  259. data/spec/hamster/set/head_spec.rbc +0 -659
  260. data/spec/hamster/set/include_spec.rbc +0 -668
  261. data/spec/hamster/set/inspect_spec.rbc +0 -593
  262. data/spec/hamster/set/join_spec.rbc +0 -1075
  263. data/spec/hamster/set/map_spec.rbc +0 -1302
  264. data/spec/hamster/set/maximum_spec.rbc +0 -1334
  265. data/spec/hamster/set/minimum_spec.rbc +0 -1334
  266. data/spec/hamster/set/none_spec.rbc +0 -1348
  267. data/spec/hamster/set/one_spec.rbc +0 -1708
  268. data/spec/hamster/set/partition_spec.rbc +0 -1348
  269. data/spec/hamster/set/product_spec.rbc +0 -581
  270. data/spec/hamster/set/reduce_spec.rbc +0 -1198
  271. data/spec/hamster/set/remove_spec.rbc +0 -1295
  272. data/spec/hamster/set/size_spec.rbc +0 -533
  273. data/spec/hamster/set/sorting_spec.rbc +0 -1228
  274. data/spec/hamster/set/sum_spec.rbc +0 -579
  275. data/spec/hamster/set/to_a_spec.rbc +0 -774
  276. data/spec/hamster/set/to_list_spec.rbc +0 -862
  277. data/spec/hamster/set/to_set_spec.rbc +0 -561
  278. data/spec/hamster/set/uniq_spec.rbc +0 -479
  279. data/spec/hamster/stack/clear_spec.rbc +0 -635
  280. data/spec/hamster/stack/construction_spec.rbc +0 -872
  281. data/spec/hamster/stack/copying_spec.rbc +0 -561
  282. data/spec/hamster/stack/empty_spec.rbc +0 -577
  283. data/spec/hamster/stack/eql_spec.rbc +0 -1283
  284. data/spec/hamster/stack/inspect_spec.rbc +0 -592
  285. data/spec/hamster/stack/pop_spec.rbc +0 -1098
  286. data/spec/hamster/stack/push_spec.rbc +0 -853
  287. data/spec/hamster/stack/size_spec.rbc +0 -657
  288. data/spec/hamster/stack/to_a_spec.rbc +0 -801
  289. data/spec/hamster/stack/to_list_spec.rbc +0 -635
  290. data/spec/hamster/stack/top_spec.rbc +0 -683
  291. data/spec/hamster/trie/remove_spec.rbc +0 -40
  292. data/spec/hamster/tuple/copying_spec.rbc +0 -511
  293. data/spec/hamster/tuple/first_spec.rbc +0 -423
  294. data/spec/hamster/tuple/inspect_spec.rbc +0 -423
  295. data/spec/hamster/tuple/last_spec.rbc +0 -423
  296. data/spec/hamster/tuple/to_a_spec.rbc +0 -803
  297. data/spec/hamster/tuple/to_ary_spec.rbc +0 -908
  298. data/spec/spec_helper.rbc +0 -184
  299. data/tasks/spec.rbc +0 -202
@@ -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
- Hamster.list(*self)
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
@@ -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
@@ -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.reverse.reduce(EmptyList) { |list, item| list.cons(item) }
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
- self
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
- self
109
- elsif yield(head)
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
- self
131
- elsif yield(head)
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
- if empty?
143
- self
144
- elsif yield(head)
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
- self
156
- elsif number > 0
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
- if empty?
167
- self
168
- elsif number > 0
169
- tail.drop(number - 1)
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
- other
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 do
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
- self
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
- self
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 Tuple.new(self, EmptyList) unless block_given?
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 do
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 do
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
- self
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
- self
351
- elsif items.include?(head)
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
- Sequence.new(self)
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
- Sequence.new(self)
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
- self
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
- self
427
- else
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
- self
443
- elsif head.is_a?(List)
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
- accessor($1)
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
@@ -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
- to_list.sort(&block)
155
+ Stream.new { Sorter.new(self).sort(&block).to_list }
156
156
  end
157
157
 
158
158
  def sort_by(&block)
159
- to_list.sort_by(&block)
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)