Module: System::Collections::FoldableStreamLambdas
- Included in:
- F
- Defined in:
- lib/raskell/f.rb
Instance Method Summary collapse
- #bucket_by ⇒ Object
- #bucket_by_and_summarize ⇒ Object
- #foldleft ⇒ Object
- #foldr ⇒ Object
- #partition_by ⇒ Object
Instance Method Details
#bucket_by ⇒ Object
319 320 321 322 323 324 325 326 327 328 |
# File 'lib/raskell/f.rb', line 319 def bucket_by @@bucket_by||= ->(fn, xs) { foldl.(->(acc, el) { key = fn.(el) acc[key] ||= [] acc[key] << el acc }, {}).(to_stream.(xs)) } end |
#bucket_by_and_summarize ⇒ Object
330 331 332 333 334 |
# File 'lib/raskell/f.rb', line 330 def bucket_by_and_summarize @@bucket_by_and_summarize||= ->(group_fn, summary_fn) { map.(summary_fn) * bucket_by.(group_fn) } end |
#foldleft ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/raskell/f.rb', line 272 def foldleft @@foldleft||= ->(f,u) { ->(stream) { next_item = stream.next_item result = u while next_item != [:done] if next_item.first == :skip elsif next_item.first == :yield result = f.(result, next_item[1]) else raise "#{next_item} is a malformed stream response" end next_item = next_item.last.next_item end result } * to_stream } end |
#foldr ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/raskell/f.rb', line 294 def foldr @@foldr||= ->(f,u) { ->(stream) { next_item = stream.next_item if next_item == [:done] u elsif next_item.first == :skip foldr.(f, u, next_item.last) elsif next_item.first == :yield f.(next_item[1], foldr.(f, u, next_item.last)) else raise "#{next_item} is improperly formed for a Stream" end } * to_stream } end |
#partition_by ⇒ Object
312 313 314 315 316 317 |
# File 'lib/raskell/f.rb', line 312 def partition_by @@partition_by||= ->(fn) { folded_filter = ->(f) { foldl.(->(acc, el) { f.(el) ? acc << el : acc }, []) } (folded_filter.(fn) + folded_filter.(->(x) { !fn.(x) })) * to_stream } end |