User Tools

Site Tools



This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
cs326:lab2 [2018/02/19 11:43]
cs326:lab2 [2021/09/23 22:13] (current)
Line 1: Line 1:
-==== CSci 326 Lab #2 ====+==== CSci 326 Lab #2 - Enumerations ​====
 +**Purpose:​** to gain experience with the functions that operate on //​enumerable//​ collections. These problems are based on the examples at [[https://​​en/​lessons/​basics/​enum/​|ElixirSchool]] and you might go through some of those on your own to get the ideas down.
-  - Work through the [[https://​​en/​lessons/​basics/​enum/​|Enum exercise]] at ElixirSchool. There is a typo in the first "​experiment",​ so try this: <​code>​Enum.__info__(:​functions) |> Enum.each(fn({functionarity}) -> IO.puts "#​{function}/#​{arity}"​ end)</​code> ​  +For this labeither write down your results and turn it inor email a transcript showing ​the results along with answers ​for the questions in #4#5, and #7.
-  - All the examples use listsbut the ''​Enum''​ module works for other enumerableslike map. Try this one out: <​code>​Enum.each(%{1 => "​one"​2 => "​two",​ 3 =>"​three"​},​ fn({key, value}) -> IO.puts(key) end)</​code>​ +
-  - Now try this one: <​code>​Enum.each(%{1 => "​one",​ 2 => "​two",​ 3 =>"​three"​},​ fn({key, value}) -> IO.puts(value) end)</​code>​ +
-  - 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. +
-  - Try this: <​code>​value = "​$34.56"​ |> String.split("​$"​) |> tl |> List.first |> String.to_float</​code>​ +
-  - Now work through the [[https://​​en/​lessons/​basics/​comprehensions/​|Comprehensions exercise]] at [[https://​​en/​|ElixirSchool]].+
-Have a look at the docs for the [[https://​​elixir/​String.html#​content|String]],​ [[https://​​elixir/​List.html#​content|List]],​ and [[https://​​elixir/​Enum.html#​content|Enum]] modules while trying the homework ​problems.+**Part 1 - Enum:** 
 +  - This expression will list every function available in the module so start **iex** and give it a try: <​code>​Enum.__info__(:​functions) |> Enum.each(fn({fname,​ arity}) -> IO.puts "#​{fname}/#​{arity}"​ end)</​code>​Let'​s break this down: 
 +    * <​html><​pre>​Enum.__info__(:​functions)</​pre></​html>​ returns a keyword list containing the names of all the functions in the module (this works for any module by the way). This list is apparently too long for **iex** to show the entirety (maybe it gets tired, poor thing). So we need to process it somehow... 
 +    * This operator ''​|>''​ is called the //pipe operator//. It is __awesome__ (Joe Armstrong said so!) simply takes the output of one command and makes it the input of the next. Transforming data through series of piped commands is a common Elixir idiom. 
 +    * ''​Enum.each''​ is similar to ''​''​ but it does not return its results as a list. The anonymous function we're passing to it takes a tuple from the keyword list and simply prints it out as a more readable string. 
 +  - ''​Enum.all?''​ returns ''​true''​ only if a predicate applied to each element of a collection returns ''​true''​ for the entire collection, otherwise it returns false. Try these:<​code>​Enum.all?​ ["​foo",​ "​bar",​ "​hello"​],​ fn(s) -> String.length(s) > 1 end</​code><​code>​Enum.all?​ ["​foo",​ "​bar",​ "​hello"​],​ fn(s) -> String.length(s) > 3 end</​code>​ 
 +  - All the examples use lists, but the ''​Enum''​ module works for other enumerables,​ like map. Try this one out: <​code>​Enum.each %{"​jan"​ => 31, "​feb"​ => 28, "​mar"​ =>"​31",​ "​apr"​ => 30}, fn({key, _value}) -> IO.puts(key) end</​code>​ 
 +  - Now try this one: <​code>​Enum.each %{"​jan"​ => 31, "​feb"​ => 28, "​mar"​ =>"​31",​ "​apr"​ => 30}, fn({_key, value}) -> IO.puts(value) end</​code>​Looking at the results of the last two commands, what can we say about how maps store (key, value) pairs in memory?  
 +  - Apparently, to iterate over maps, we need an anonymous function that takes a ''​{key,​ value}''​ tuple. Come up with an example of your own for ''​''​ or ''​Enum.reduce''​ that work on maps. Show your results to the instructor.  
 +  - First define the function ''​odd?''<​code>​odd?​ = &​(rem(&​1,​ 2) != 0)</​code>​Now use it with ''​Enum.filter'':<​code>​Enum.filter(1..10,​ odd?​)</​code>​ 
 +  - Now try this monstrosity:<​code>​1..100_000 |>​(&​1 * 3)) |> Enum.filter(odd?​) |> Enum.sum()</​code>​See if you can describe the operation in English. 
 +  - The ''​sort''​ function uses the mergesort algorithm using Elixir'​s //term order//. To see this in action, try:<​code>​Enum.sort([:​foo,​ "​bar",​ Enum, -1, 4])</​code>​ 
 +  - Another version of ''​sort''​ lets you pass a function to change the default sort mechanism. Try these two to see the difference:<​code>​Enum.sort ["​x",​ "​yz",​ "​abc",​ "​klmn",​ "​defgh"​]</​code><​code>​Enum.sort ["​x",​ "​yz",​ "​abc",​ "​klmn",​ "​defgh"​],​ fn(x, y) -> (String.length x) < (String.length y) end</​code>​ 
 +**Part 2 - Strings:​** 
 +  - Have a look at the docs for the [[https://​​elixir/​String.html#​at/​2]]. Try out the examples for the functions ''​codepoints/​1'',​ ''​ends_with/​2'',​ ''​slice/​2''​ and ''​split/​1''​ in **iex**.  
 +  - This expression uses the pipe operator and functions from the ''​String''​ and ''​List''​ modules to convert a monetary amount stored as a string into the equivalent number. Try it out: <​code>"​$34.56"​ |> String.split("​$"​) |> List.last |> String.to_float</​code>​ 
 +Have a look at the docs for the [[https://​​elixir/​String.html#​content|String]],​ [[https://​​elixir/​List.html#​content|List]],​ and [[https://​​elixir/​Enum.html#​content|Enum]] modules while trying the next two homework ​assignments.
cs326/lab2.1519062202.txt.gz · Last modified: 2018/02/19 11:43 by scarl