Skip to content

Latest commit

 

History

History
209 lines (134 loc) · 5.42 KB

non_enumerables.livemd

File metadata and controls

209 lines (134 loc) · 5.42 KB

Non-Enumerables

Mix.install([
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:tested_cell, github: "brooklinjazz/tested_cell"},
  {:utils, path: "#{__DIR__}/../utils"}
])

Navigation

Return Home Report An Issue

Setup

Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.

Enumeration With Non-Enumerables

Data types that do not contain multiple elements are not enumerable. However, it's still sometimes useful to enumerate over non-enumerable data types.

Even though we can't enumerate over them directly, the trick is to convert non-enumerable data into an enumerable data type, then convert them back if necessary.

Integers

We can use the Integer.digits/1 function to convert integers to a list of digits.

Integer.digits(123)

Your Turn

In the Elixir cell below, convert the integer 4389 into a list of digits.

Undigits

We can convert a list of digits back into a single integer using Integer.undigits/2.

Integer.undigits([1, 2, 3])

Tuples

Despite being so similar to lists, tuples are not considered enumerable. That's primarily because they are intended as fixed-sized containers, and any case that requires enumeration would likely be better served with lists.

To get around this if needed, you can convert a tuple to a list with Tuple.to_list/1.

However, if you find yourself doing this often, it may be a better choice to start with a list instead of a tuple.

Tuple.to_list({1, 2, 3})

We can then convert a list back into a tuple using List.to_tuple/1

List.to_tuple([1, 2, 3])

Strings

Strings are not enumerable.

Enum.map("abc", fn char -> char end)

However, strings can be converted into lists using String.split/3.

String.split/3 splits a string based on some value.

For example, we can split a string by every comma like so:

String.split("a,b,c,d", ",")

We can split a string into a list of characters by splitting on every empty space "" This does create an empty string becaused of the start and the end of the list.

String.split("abcde", "")

You can trim whitespace using the [trim: true] option.

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

By default, String.split/1 will split the string on every space.

String.split("hello world")

Now your string is an enumerable list of characters.

Your Turn

In the Elixir cell below, convert the string "Hello, world!" into a list of single characters.

Joining Strings

Once you've applied your transformation to the list of string characters, you can rejoin them using Enum.join/2

Enum.join(["a", "b", "c"], "")

The default joiner is a string, so you can omit the second argument.

Enum.join(["a", "b", "c"])

Alternatively we can join the strings by some joining character.

Enum.join(["a", "b", "c"], "_")

We can use String.split/3 and Enum.join/2 in combination with each other to split strings, enumerate over them applying some transformation, and then rejoining them.

Here's a simple example of replacing all characters other than spaces with "X".

split_string = String.split("secret information", "")

transformed_list =
  Enum.map(split_string, fn char ->
    case char do
      " " -> " "
      _ -> "X"
    end
  end)

joined_string = Enum.join(transformed_list)

Your Turn

Start with a string such as "hello". Use enumeration to double every character in the string so the "hello" would become "hheelllloo".

Example Solution
split_string = String.split("hello", "")

doubled_string = Enum.map(split_string, fn char -> char <> char end)

Enum.join(doubled_string)

You might also use the pipe operator.

"hello"
|> String.split("")
|> Enum.map(fn char -> char <> char end)
|> Enum.join()

Enter your solution below.

Commit Your Progress

Run the following in your command line from the beta_curriculum folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish non enumerables section"

Up Next

Previous Next
SafeRange Comprehensions