From bbf3d201c79645760609068a2a3b0da4301227f0 Mon Sep 17 00:00:00 2001 From: fabien townsend Date: Thu, 30 Mar 2017 20:27:47 +0100 Subject: [PATCH] Add read from a streams koans --- lib/koans/19_streams.ex | 27 +++++++++++++++++++++++++++ lib/runner.ex | 1 + test/koans/streams_koans_test.exs | 14 ++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 lib/koans/19_streams.ex create mode 100644 test/koans/streams_koans_test.exs diff --git a/lib/koans/19_streams.ex b/lib/koans/19_streams.ex new file mode 100644 index 00000000..e6c02a31 --- /dev/null +++ b/lib/koans/19_streams.ex @@ -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 diff --git a/lib/runner.ex b/lib/runner.ex index ac87ae5d..f96e46a7 100644 --- a/lib/runner.ex +++ b/lib/runner.ex @@ -18,6 +18,7 @@ defmodule Runner do Tasks, Agents, Protocols, + Streams, ] def koan?(koan), do: Enum.member?(@modules, koan) diff --git a/test/koans/streams_koans_test.exs b/test/koans/streams_koans_test.exs new file mode 100644 index 00000000..713e11f7 --- /dev/null +++ b/test/koans/streams_koans_test.exs @@ -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