Skip to content

Commit

Permalink
Add read from a streams koans
Browse files Browse the repository at this point in the history
  • Loading branch information
fabientownsend committed Mar 30, 2017
1 parent c9db06a commit bbf3d20
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/koans/19_streams.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Streams do
use Koans

@intro "Streams"

koan "Streams are lazier than Enum, they only return a struct" do
assert Stream.map([1, 2, 3], &(&1 + 1)).__struct__ == ___
end

koan "You have to ask it explicitly to do the operation" do
updated_list =
[1, 2, 3]
|> Stream.map(&(&1 + 1))
|> Enum.to_list

assert updated_list == ___
end

koan "Which will let you decide which element you want to compute in the stream" do
computed_element =
1..42
|> Stream.map(&("I computed: #{&1}"))
|> Enum.take(1)

assert computed_element == ___
end
end
1 change: 1 addition & 0 deletions lib/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule Runner do
Tasks,
Agents,
Protocols,
Streams,
]

def koan?(koan), do: Enum.member?(@modules, koan)
Expand Down
14 changes: 14 additions & 0 deletions test/koans/streams_koans_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule StreamsTests do
use ExUnit.Case
import TestHarness

test "Streams" do
answers = [
Stream,
[2, 3, 4],
["I computed: 1"],
]

test_all(Streams, answers)
end
end

0 comments on commit bbf3d20

Please sign in to comment.