User Tools

Site Tools


This is an old revision of the document!

CSci 326 Lab #2 - Enumerations

Purpose: to gain experience with the functions that operate on enumerable collections.

  1. Work through the Enum exercise at ElixirSchool. There is a typo in the first “experiment”, so try this:
    Enum.__info__(:functions) |> Enum.each(fn({function, arity}) -> IO.puts "#{function}/#{arity}" end)
  2. All the examples use lists, but the Enum module works for other enumerables, like map. Try this one out:
    Enum.each(%{1 => "one", 2 => "two", 3 =>"three"}, fn({key, value}) -> IO.puts(key) end)
  3. Now try this one:
    Enum.each(%{1 => "one", 2 => "two", 3 =>"three"}, fn({key, value}) -> IO.puts(value) end)
  4. So to enumerate over maps, we need an anonymous function that takes a {key, value} tuple. Come up with an example of your own for both and Enum.reduce that work on map collections. Show your results to the instructor.
  5. Try this and figure out the value bound to cost:
    cost = "$34.56" |> String.split("$") |> tl |> List.first |> String.to_float
  6. Now work through the Comprehensions exercise at ElixirSchool.

Have a look at the docs for the String, List, and Enum modules while trying the homework problems.

cs326/lab2.1631548426.txt.gz · Last modified: 2021/09/13 10:53 by scarl