Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us
Notesclub

Concurrent Word Count

exercises/concurrent_word_count.livemd

Concurrent Word Count

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

Navigation

Home Report An Issue Task DrillsHTML & CSS

Concurrent Word Count

Given a list of documents, use the Task module to concurrently find the total word count of every document combined.

For our purposes, a word will be any sequence of characters separated by a space. You do not have to handle invalid words, punctuation, or other special cases.

document = """
This is my document document
"""

String.split(document, " ", trim: true)

Example Solution

defmodule WordCount do
  def individual_count(documents) do
    tasks =
      Enum.map(documents, fn document ->
        Task.async(fn ->
          document |> String.split(" ", trim: true) |> Enum.count()
        end)
      end)

    Task.await_many(tasks)
  end

  def total_count(documents) do
    documents |> individual_count() |> Enum.sum()
  end
end

Implement the WordCount module as documented below.

defmodule WordCount do
  @moduledoc """
  Documentation for `WordCount`.
  """

  @doc """
  Concurrently count the number of words in each document.

  ## Examples

      iex> WordCount.individual_count(["document one", "document two"])
      [2, 2]
  """
  def individual_count(documents) do
  end

  @doc """
  Concurrently count the number of words in each document and return
  the combined count.

  iex> WordCount.total_count(["document one", "document two"])
  4
  """
  def total_count(documents) do
  end
end

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Concurrent Word Count exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue Task DrillsHTML & CSS