User Tools

Site Tools


cs326:lab2

Differences

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 [2021/09/23 20:11]
scarl
cs326:lab2 [2021/09/23 22:13] (current)
scarl
Line 2: Line 2:
  
 **Purpose:​** to gain experience with the functions that operate on //​enumerable//​ collections. These problems are based on the examples at [[https://​elixirschool.com/​en/​lessons/​basics/​enum/​|ElixirSchool]] and you might go through some of those on your own to get the ideas down. **Purpose:​** to gain experience with the functions that operate on //​enumerable//​ collections. These problems are based on the examples at [[https://​elixirschool.com/​en/​lessons/​basics/​enum/​|ElixirSchool]] and you might go through some of those on your own to get the ideas down.
 +
 +For this lab, either write down your results and turn it in, or email a transcript showing the results along with answers for the questions in #4, #5, and #7.
  
 **Part 1 - Enum:** **Part 1 - Enum:**
  
-  - This command ​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 command ​down:  +  - 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: 
-    * ''​Enum.__info__(:​functions)''​ is a function that 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...+    * <​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!)...it 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.     * This operator ''​|>''​ is called the //pipe operator//. It is __awesome__ (Joe Armstrong said so!)...it 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 ''​Enum.map''​ 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.each''​ is similar to ''​Enum.map''​ 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.
Line 12: Line 14:
   - 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>​   - 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? ​   - 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? ​
-  - So to iterate over maps, we need an anonymous function that takes a ''​{key,​ value}''​ tuple. Come up with an example of your own for ''​Enum.map''​ or ''​Enum.reduce''​ that work on maps. Show your results to the instructor. ​+  - 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 ''​Enum.map''​ 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 |> Enum.map(&​(&​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>​   - 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>​   - 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:** **Part 2 - Strings:**
-  - Try this and figure ​out the value bound to ''​cost'':​ <​code>​cost = "​$34.56"​ |> String.split("​$"​) ​|> tl |> List.first |> String.to_float</​code>​ +  - Have a look at the docs for the [[https://​hexdocs.pm/​elixir/​String.html#​at/​2]]. ​Try out the examples for the functions ​''​codepoints/​1''​, ''​ends_with/​2'',​ ''​slice/​2''​ and ''​split/​1''​ in **iex**.  
-  - Now work through the [[https://​elixirschool.com/​en/​lessons/​basics/​comprehensions/​|Comprehensions exercise]] at [[https://​elixirschool.com/​en/​|ElixirSchool]].+  - 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://​hexdocs.pm/​elixir/​String.html#​content|String]],​ [[https://​hexdocs.pm/​elixir/​List.html#​content|List]],​ and [[https://​hexdocs.pm/​elixir/​Enum.html#​content|Enum]] modules while trying the homework ​problems.+Have a look at the docs for the [[https://​hexdocs.pm/​elixir/​String.html#​content|String]],​ [[https://​hexdocs.pm/​elixir/​List.html#​content|List]],​ and [[https://​hexdocs.pm/​elixir/​Enum.html#​content|Enum]] modules while trying the next two homework ​assignments.
cs326/lab2.1632445902.txt.gz · Last modified: 2021/09/23 20:11 by scarl