Powered by AppSignal & Oban Pro

Stream Drills

exercises/stream_drills.livemd

Stream Drills

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.8.0", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Return Home Report An Issue

Stream Drills

Drills help you develop familiarity and muscle memory with syntax through repeated exercises. Unlike usual problems, Drills are not intended to develop problem solving skills, they are purely for developing comfort and speed.

This set of drills is for Streams. Follow the instructions for each drill and complete them as quickly as you can.

Stream.map/2

Use Stream.map/2 to double elements in a range from 1..10, then use Enum.to_list/1 to convert the resulting stream into a list [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Use Stream.map/2 and Enum.to_list/1 to sum a list of tuple pairs [{1, 1}, {2, 2}, {3, 3}] into [2, 4, 6].

Stream.filter/2

Use Stream.filter/2 and Enum.to_list/1 to filter even numbers from a range 1..10 to return [2, 4, 6, 8, 10].

Use Stream.filter/2 and Enum.to_list/1 to filter numbers greater than 5 from a range 1..10 to return [6, 7, 8, 9, 10].

Stream.cycle/1

Use Stream.cycle/1 with Enum.take/2 to generate the list [1, 2, 3, 1, 2, 3, 1, 2, 3].

Use Stream.cycle/1 with Enum.take/2 to generate the list ["a", "b", "a", "b"]

Stream.iterate/2

Use Stream.iterate/2 and Enum.take/2 to generate a list of numbers from 1 to 10.

Use Stream.iterate/2 and Enum.take/2 to generate a list of negative numbers from 0 to -10.

Use Stream.iterate/2 and Enum.take/2 to generate a list of 2 to increasing powers by multiplying the accumulator by two. i.e. $2^1$, $2^2$, $2^3$, $2^4$.

Stream.unfold/2

Use Stream.unfold/2 and Enum.take/2 to generate a list of 10 cubed numbers. i.e. $1^3, 2^3, 3^3$

Use Stream.unfold/2 and Enum.take/2 to generate a list of 5 integers as strings. ["1", "2", "3", "4", "5"].

Use Stream.unfold/2 and Enum.take/2 to generate a list of integers from 1 to 10 divided by 2.

Stream.chunk_every/2

Use Stream.chunk_every/2 with Enum.to_list/1 to chunk [1, 1, 2, 2, 3, 3] into [[1, 1], [2, 2], [3, 3]]

Use Stream.chunk_every/2 to chunk [1, 1, 1, 2, 2, 2] into [[1, 1, 1], [2, 2, 2]]

Use Stream.chunk_every/2 to chunk [1, 2, 2, 1, 4, 4] into pairs, then use Stream.map/2 to sum the pairs together to make [3, 3, 8].

Mark As Completed

file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")

save_name =
  case Path.basename(__DIR__) do
    "reading" -> "stream_drills_reading"
    "exercises" -> "stream_drills_exercise"
  end

progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()

default = Map.get(existing_progress, save_name, false)

form =
  Kino.Control.form(
    [
      completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
    ],
    report_changes: true
  )

Task.async(fn ->
  for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
    File.write!(
      progress_path,
      Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)
    )
  end
end)

form

Commit Your Progress

Run the following in your command line from the curriculum folder to track and save your progress in a Git commit. Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.

$ git checkout -b stream-drills-exercise
$ git add .
$ git commit -m "finish stream drills exercise"
$ git push origin stream-drills-exercise

Create a pull request from your stream-drills-exercise branch to your solutions branch. Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.

DockYard Academy Students Only:

Notify your teacher by including @BrooklinJazz in your PR description to get feedback. You (or your teacher) may merge your PR into your solutions branch after review.

If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.

Up Next

Previous Next
Streams Lazy Product Filters