In this example, the list was enumerated just once! Printed changed! With streams, we print the first element and then print to_list ( stream ) 1 2 2 4 3 6 #=> Īlthough the end result is the same, the order in which the elements were Let's see an example with streams: stream = 1. Note that we first printed each element in the list, then multiplied eachĮlement by 2 and finally printed each new value. Intermediate lists are created, while Stream creates a recipe ofĬomputations that are executed at a later moment. We say the functions in Stream are lazy and the functions in Enumĭue to their laziness, streams are useful when working with large Only when Enum.map/2 is called we actuallyĮnumerate over each element in the range, multiplying it by 2 and adding 1. Meant to multiply each element in the range by 2. Note that we started with a range and then we created a stream that is Module allows us to map the range, without triggering its enumeration: iex> range = 1. In the example above, as we mapped over the range, the elements beingĮnumerated were created one by one, during enumeration. For example,Įlixir's Range is a stream: iex> range = 1. Any enumerable that generatesĮlements one by one during enumeration is called a stream. Streams are composable, lazy enumerables (for an introduction onĮnumerables, see the Enum module). Ctrl-Shift-] (Cmd-Alt-] on macOS): unfoldCode.Settings View Source Stream (Elixir v1.14.4)įunctions for creating and composing streams.Ctrl-Shift-[ (Cmd-Alt-[ on macOS): foldCode.Cmd Alt k, Cmd Alt j: Unfold All.Cmd Alt k, Cmd Alt -: Fold All except selected region.Cmd Alt k, Cmd Alt ]: Unfold Region Recursively. ![]()
0 Comments
Leave a Reply. |