Skip to content
Browse files

add #concat as an alias of #flatten.

move methods.
  • Loading branch information...
1 parent 626c612 commit 7b413733ca96e9a88a0230f2e4eab2dfda6b0c93 @shugo committed Apr 29, 2012
Showing with 29 additions and 27 deletions.
  1. +29 −27 lib/immutable/stream.rb
View
56 lib/immutable/stream.rb
@@ -229,25 +229,6 @@ def +(s)
}
end
- # Concatenates a stream of streams.
- #
- # @return [Stream] the concatenated stream.
- def flatten
- Stream.lazy {
- if empty?
- self
- else
- if head.empty?
- tail.flatten
- else
- Stream.cons ->{head.head}, ->{
- Stream.cons(->{head.tail}, ->{tail}).flatten
- }
- end
- end
- }
- end
-
# Returns the stream obtained by applying the given block to each
# element in +self+.
#
@@ -308,24 +289,27 @@ def intercalate(xs)
intersperse(xs).flatten
end
- # Returns the elements in +self+ for which the given block evaluates to
- # true.
+ # Concatenates a stream of streams.
#
- # @return [Stream] the elements that satisfies the condition.
- def filter(&block)
+ # @return [Stream] the concatenated stream.
+ def flatten
Stream.lazy {
if empty?
- Stream.empty
+ self
else
- if yield(head)
- Stream.cons ->{ head }, ->{ tail.filter(&block) }
+ if head.empty?
+ tail.flatten
else
- tail.filter(&block)
+ Stream.cons ->{head.head}, ->{
+ Stream.cons(->{head.tail}, ->{tail}).flatten
+ }
end
end
}
end
+ alias concat flatten
+
# Returns the first +n+ elements of +self+, or +self+ itself if
# +n > self.length+.
#
@@ -384,6 +368,24 @@ def drop_while(&block)
}
end
+ # Returns the elements in +self+ for which the given block evaluates to
+ # true.
+ #
+ # @return [Stream] the elements that satisfies the condition.
+ def filter(&block)
+ Stream.lazy {
+ if empty?
+ Stream.empty
+ else
+ if yield(head)
+ Stream.cons ->{ head }, ->{ tail.filter(&block) }
+ else
+ tail.filter(&block)
+ end
+ end
+ }
+ end
+
# Builds a stream from the seed value +e+ and the given block. The block
# takes a seed value and returns +nil+ if the seed should
# unfold to the empty stream, or returns +[a, b]+, where

0 comments on commit 7b41373

Please sign in to comment.
Something went wrong with that request. Please try again.