-
-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the Wiki! Here's what we got:
Inspired by Java 8's Stream API, ruby-stream-api aims to provide a wide range of useful Streams for Ruby.
A Stream is a wrapper over a collection of elements offering a number of useful operations to modify and/or get information about the collection. The operations are chainable and can be categorized as follows:
- Source operations -- these are the operations which are generating the Stream.
- Intermediate operations (skip, filter, map etc) -- operations which are altering the Stream and still leave it open for further modifications.
- Terminal operations (count, collect etc) -- operations which are executed after all the modifications have been done and are returning a finite result.
stream = Stream::FromArray.new([1, 2, 3, 4])
# or, if you want to hide the class
stream = Stream.from_array([1, 2, 3, 4])
stream = Stream.generate(150) { &seed }
The generate
method takes a limit (max number of elements) and a &seed
Block function which returns a new element at each seed.call
. A limit is necessary as, without it, this Stream would be infinite. If no limit is specified, the default is 100
elements.
This mechanism is useful, for instance, when you have to consume an incomming stream of objects from some IO objects.
-
Intermediate and chainable:
-
filter(&condition)
- filter out elements which do not satisfy the condition -
map(&function)
- map the stream's element using the given function -
distinct
- remove all duplicates from the stream -
skip(n)
- skip the firstn
elements of the stream
-
-
Terminal:
-
collect
- collect the stream's elements into an array -
count
- return the number of elements in the stream -
all_match(&test)
- return true if all elements pass the test (true if the stream is empty) -
any_match(&test)
- returns true if any element passes the test (false if the stream is empty)
-