GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing elixir.hexdocs.pm — Stream — Elixir v1.20.5 Functions for creating and composing streams. Streams are composable, lazy enumerables (for an introduction on
enumerables, see the In the example above, as we mapped over the range, the elements being
enumerated were created one by one, during enumeration. The Note that we started with a range and then we created a stream that is
meant to multiply each element in the range by 2. At this point, no
computation was done. Only when Due to their laziness, streams are useful when working with large
(or even infinite) collections. When chaining many operations with Let's see another example: Note that we first printed each element in the list, then multiplied each element by 2 and finally printed each new value. In this example, the list was enumerated three times. Let's see an example with streams: Although the end result is the same, the order in which the elements were printed changed! With streams, we print the first element and then print its double. In this example, the list was enumerated just once! That's what we meant when we said earlier that streams are composable,
lazy enumerables. Note that we could call Like with Creating StreamsThere are many functions in Elixir's standard library that return streams, some examples are:
This module also provides many convenience functions for creating streams,
like
SummaryFunctionsChunks the Shortcut to Streams the enumerable in chunks, containing Chunks the Creates a stream that enumerates each enumerable in an enumerable. Creates a stream that enumerates the first argument, followed by the second. Creates a stream that cycles through the given enumerable, infinitely. Creates a stream that only emits elements if they are different from the last emitted element. Creates a stream that only emits elements if the result of calling Lazily drops the next Creates a stream that drops every Lazily drops elements of the enumerable while the given function returns a truthy value. Duplicates the given element Executes the given function for each element. Creates a stream that filters elements according to the given function on enumeration. Maps the given Builds a stream from an index, either starting from offset, or given by function. Lazily intersperses Creates a stream that emits a value after the given period Injects the stream values into the given collectable as a side-effect. Emits a sequence of values, starting with Creates a stream that will apply the given function on enumeration. Creates a stream that will apply the given function on
every Creates a stream that will reject elements according to the given function on enumeration. Returns a stream generated by calling Emits a sequence of values for the given resource. Runs the given stream. Creates a stream that applies the given function to each element, emits the result and uses the same result as the accumulator for the next computation. Uses the first element in the enumerable as the starting value. Creates a stream that applies the given function to each
element, emits the result and uses the same result as the accumulator
for the next computation. Uses the given Lazily takes the next Creates a stream that takes every Lazily takes elements of the enumerable while the given function returns a truthy value. Creates a stream that emits a single value after Transforms an existing stream. Similar to Transforms an existing stream with function-based start, last, and after callbacks. Emits a sequence of values for the given accumulator. Creates a stream that only emits elements if they are unique. Creates a stream that only emits elements if they are unique, by removing the
elements for which function Creates a stream where each element in the enumerable will be wrapped in a tuple alongside its index or according to a given function. Zips corresponding elements from a finite collection of enumerables into one stream of tuples. Zips two enumerables together, lazily. Lazily zips corresponding elements from a finite collection of enumerables into a new
enumerable, transforming them with the Lazily zips corresponding elements from two enumerables into a new one, transforming them with
the Types@type acc() :: any() @type default() :: any() @type element() :: any() @type index() :: non_neg_integer() Zero-based index. @type timer() :: non_neg_integer() | :infinity Functions@spec chunk_by(Enumerable.t(), (element() -> any())) :: Enumerable.t() Chunks the Elements are only emitted when Examples
@spec chunk_every(Enumerable.t(), pos_integer()) :: Enumerable.t() Shortcut to @spec chunk_every( Enumerable.t(), pos_integer(), pos_integer(), Enumerable.t() | :discard ) :: Enumerable.t() Streams the enumerable in chunks, containing
If the last chunk does not have If Examples
@spec chunk_while( Enumerable.t(), acc(), (element(), acc() -> {:cont, chunk, acc()} | {:cont, acc()} | {:halt, acc()}), (acc() -> {:cont, chunk, acc()} | {:cont, acc()}) ) :: Enumerable.t() when chunk: any() Chunks the
Examples
@spec concat(Enumerable.t()) :: Enumerable.t() Creates a stream that enumerates each enumerable in an enumerable. Examples
@spec concat(Enumerable.t(), Enumerable.t()) :: Enumerable.t() Creates a stream that enumerates the first argument, followed by the second. Examples
@spec cycle(Enumerable.t()) :: Enumerable.t() Creates a stream that cycles through the given enumerable, infinitely. Examples
@spec dedup(Enumerable.t()) :: Enumerable.t() Creates a stream that only emits elements if they are different from the last emitted element. This function only ever needs to store the last emitted element. Elements are compared using Examples
@spec dedup_by(Enumerable.t(), (element() -> term())) :: Enumerable.t() Creates a stream that only emits elements if the result of calling Examples
@spec drop(Enumerable.t(), integer()) :: Enumerable.t() Lazily drops the next If a negative Examples
@spec drop_every(Enumerable.t(), non_neg_integer()) :: Enumerable.t() Creates a stream that drops every The first element is always dropped, unless
Examples
@spec drop_while(Enumerable.t(), (element() -> as_boolean(term()))) :: Enumerable.t() Lazily drops elements of the enumerable while the given function returns a truthy value. Examples
@spec duplicate(any(), non_neg_integer()) :: Enumerable.t() Duplicates the given element
If Examples
@spec each(Enumerable.t(), (element() -> term())) :: Enumerable.t() Executes the given function for each element. The values in the stream do not change, therefore this
function is useful for adding side effects (like printing)
to a stream. See Examples
@spec filter(Enumerable.t(), (element() -> as_boolean(term()))) :: Enumerable.t() Creates a stream that filters elements according to the given function on enumeration. Examples
@spec flat_map(Enumerable.t(), (element() -> Enumerable.t())) :: Enumerable.t() Maps the given This function returns a new stream built by appending the result of invoking Examples
@spec from_index(integer()) :: Enumerable.t(integer()) @spec from_index((integer() -> return_value)) :: Enumerable.t(return_value) when return_value: term() Builds a stream from an index, either starting from offset, or given by function. May receive a function or an integer offset. If an If a Examples
@spec intersperse(Enumerable.t(), any()) :: Enumerable.t() Lazily intersperses Examples
@spec interval(timer()) :: Enumerable.t() Creates a stream that emits a value after the given period The values emitted are an increasing counter starting at Do not use this function to generate a sequence of numbers.
If blocking the caller process is not necessary, use
Examples
@spec into(Enumerable.t(), Collectable.t(), (term() -> term())) :: Enumerable.t() Injects the stream values into the given collectable as a side-effect. This function is often used with @spec iterate(element(), (element() -> element())) :: Enumerable.t() Emits a sequence of values, starting with Successive values are generated by calling Examples
@spec map(Enumerable.t(), (element() -> any())) :: Enumerable.t() Creates a stream that will apply the given function on enumeration. Examples
@spec map_every(Enumerable.t(), non_neg_integer(), (element() -> any())) :: Enumerable.t() Creates a stream that will apply the given function on
every The first element is always passed to the given function.
Examples
@spec reject(Enumerable.t(), (element() -> as_boolean(term()))) :: Enumerable.t() Creates a stream that will reject elements according to the given function on enumeration. Examples
@spec repeatedly((-> element())) :: Enumerable.t() Returns a stream generated by calling Examples
@spec resource((-> acc()), (acc() -> {[element()], acc()} | {:halt, acc()}), (acc() -> term())) :: Enumerable.t() Emits a sequence of values for the given resource. Similar to Successive values are generated by calling As the function name suggests, this function is useful to stream values from resources. Examples
@spec run(Enumerable.t()) :: :ok Runs the given stream. This is useful when a stream needs to be run, for side effects, and there is no interest in its return result. ExamplesOpen up a file, replace all No computation will be done until we call one of the @spec scan(Enumerable.t(), (element(), acc() -> any())) :: Enumerable.t() Creates a stream that applies the given function to each element, emits the result and uses the same result as the accumulator for the next computation. Uses the first element in the enumerable as the starting value. Examples
@spec scan(Enumerable.t(), acc(), (element(), acc() -> any())) :: Enumerable.t() Creates a stream that applies the given function to each
element, emits the result and uses the same result as the accumulator
for the next computation. Uses the given Examples
@spec take(Enumerable.t(), integer()) :: Enumerable.t() Lazily takes the next If a negative Examples
@spec take_every(Enumerable.t(), non_neg_integer()) :: Enumerable.t() Creates a stream that takes every The first element is always included, unless
Examples
@spec take_while(Enumerable.t(), (element() -> as_boolean(term()))) :: Enumerable.t() Lazily takes elements of the enumerable while the given function returns a truthy value. Examples
@spec timer(timer()) :: Enumerable.t() Creates a stream that emits a single value after The value emitted is Examples
@spec transform(Enumerable.t(), acc, fun) :: Enumerable.t() when fun: (element(), acc -> {Enumerable.t(), acc} | {:halt, acc}), acc: term() Transforms an existing stream. It expects an accumulator and a function that receives two arguments,
the stream element and the updated accumulator. It must return a tuple,
where the first element is a new stream (often a list) or the atom Note: this function is equivalent to Examples
@spec transform(Enumerable.t(), start_fun, reducer, after_fun) :: Enumerable.t() when start_fun: (-> acc), reducer: (element(), acc -> {Enumerable.t(), acc} | {:halt, acc}), after_fun: (acc -> term()), acc: term() Similar to This function can be seen as a combination of @spec transform(Enumerable.t(), start_fun, reducer, last_fun, after_fun) :: Enumerable.t() when start_fun: (-> acc), reducer: (element(), acc -> {Enumerable.t(), acc} | {:halt, acc}), last_fun: (acc -> {Enumerable.t(), acc} | {:halt, acc}), after_fun: (acc -> term()), acc: term() Transforms an existing stream with function-based start, last, and after callbacks. Once transformation starts, Once the collection is done, @spec unfold(acc(), (acc() -> {element(), acc()} | nil)) :: Enumerable.t() Emits a sequence of values for the given accumulator. Successive values are generated by calling ExamplesTo create a stream that counts down and stops before zero: If
@spec uniq(Enumerable.t()) :: Enumerable.t() Creates a stream that only emits elements if they are unique. Keep in mind that, in order to know if an element is unique or not, this function needs to store all unique values emitted by the stream. Therefore, if the stream is infinite, the number of elements stored will grow infinitely, never being garbage-collected. Examples
@spec uniq_by(Enumerable.t(), (element() -> term())) :: Enumerable.t() Creates a stream that only emits elements if they are unique, by removing the
elements for which function The function Keep in mind that, in order to know if an element is unique or not, this function needs to store all unique values emitted by the stream. Therefore, if the stream is infinite, the number of elements stored will grow infinitely, never being garbage-collected. Example
@spec with_index(Enumerable.t(), integer()) :: Enumerable.t({element(), integer()}) @spec with_index(Enumerable.t(), (element(), index() -> return_value)) :: Enumerable.t(return_value) when return_value: term() Creates a stream where each element in the enumerable will be wrapped in a tuple alongside its index or according to a given function. May receive a function or an integer offset. If an If a Examples
@spec zip(enumerables) :: Enumerable.t() when enumerables: [Enumerable.t()] | Enumerable.t() Zips corresponding elements from a finite collection of enumerables into one stream of tuples. The zipping finishes as soon as any enumerable in the given collection completes. Examples
@spec zip(Enumerable.t(), Enumerable.t()) :: Enumerable.t() Zips two enumerables together, lazily. Because a list of two-element tuples with atoms as the first
tuple element is a keyword list ( The zipping finishes as soon as either enumerable completes. Examples
@spec zip_with(enumerables, (Enumerable.t() -> term())) :: Enumerable.t() when enumerables: [Enumerable.t()] | Enumerable.t() Lazily zips corresponding elements from a finite collection of enumerables into a new
enumerable, transforming them with the The first element from each of the enums in Returns a new enumerable with the results of calling Examples
@spec zip_with(Enumerable.t(), Enumerable.t(), (term(), term() -> term())) :: Enumerable.t() Lazily zips corresponding elements from two enumerables into a new one, transforming them with
the The Examples
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |