Skip to content

Commit

Permalink
Merge pull request #1002 from devloglogan/sentence_fix
Browse files Browse the repository at this point in the history
Enum chapter fixes
  • Loading branch information
BrooklinJazz authored Jul 12, 2023
2 parents b8d195f + 82477d6 commit 1b07ae2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions reading/enum.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Enum.map([1, 2, 3, 4, 5], fn element -> element * 2 end)

### Enumerable

Certain data types in Elixir implement the [Enumerable](https://hexdocs.pm/elixir/Enumerable.html) protocol. That means these we can enumerate through elements in these enumerable data types.
Certain data types in Elixir implement the [Enumerable](https://hexdocs.pm/elixir/Enumerable.html) protocol. That means we can enumerate through elements in these enumerable data types.

Most enumerables are collections, but not all of them. For example a range doesn't contain other elements, but it is enumerable.

Expand Down Expand Up @@ -296,7 +296,7 @@ end)

The [Enum](https://hexdocs.pm/elixir/Enum.html) module also provides other useful functions. Here are a handful of the most used.

* [Enum.all/2](https://hexdocs.pm/elixir/Enum.html#all/2) check if all elements in a collection match some condition.
* [Enum.all?/2](https://hexdocs.pm/elixir/Enum.html#all?/2) check if all elements in a collection match some condition.
* [Enum.any?/2](https://hexdocs.pm/elixir/Enum.html#any?/2) check if any elements in a collection match some condition.
* [Enum.count/2](https://hexdocs.pm/elixir/Enum.html#count/2) return the number of elements in a collection collection.
* [Enum.find/3](https://hexdocs.pm/elixir/Enum.html#find/3) return an element in a collection that matches some condition.
Expand Down Expand Up @@ -549,9 +549,9 @@ Enter your solution below.

## Enum.all?/2

[Enum.all/2](https://hexdocs.pm/elixir/Enum.html#all/2) checks that all elements in a collection match some condition.
[Enum.all?/2](https://hexdocs.pm/elixir/Enum.html#all?/2) checks that all elements in a collection match some condition.

[Enum.all/2](https://hexdocs.pm/elixir/Enum.html#all/2) executes the callback function provided on each element. If every element returns truthy, then [Enum.all/2](https://hexdocs.pm/elixir/Enum.html#all/2) returns `true`.
[Enum.all?/2](https://hexdocs.pm/elixir/Enum.html#all?/2) executes the callback function provided on each element. If every element returns truthy, then [Enum.all?/2](https://hexdocs.pm/elixir/Enum.html#all?/2) returns `true`.

<!-- livebook:{"break_markdown":true} -->

Expand All @@ -567,21 +567,21 @@ E --> Boolean
Enum.all?([1, 2, 3], fn integer -> is_integer(integer) end)
```

If a single element returns a falsy value, then [Enum.all/2](https://hexdocs.pm/elixir/Enum.html#all/2) returns `false`.
If a single element returns a falsy value, then [Enum.all?/2](https://hexdocs.pm/elixir/Enum.html#all?/2) returns `false`.

```elixir
Enum.all?([1, 2, 3, "4"], fn element -> is_integer(element) end)
```

For performance reasons, [Enum.all/2](https://hexdocs.pm/elixir/Enum.html#all/2) will complete as soon as it finds a single element that returns a falsey value when called with the function.
For performance reasons, [Enum.all?/2](https://hexdocs.pm/elixir/Enum.html#all?/2) will complete as soon as it finds a single element that returns a falsey value when called with the function.

Notice the code below should finish very quickly because the very first element fails the condition.

```elixir
Enum.all?(1..10_000_000, fn integer -> is_bitstring(integer) end)
```

If [Enum.all/2](https://hexdocs.pm/elixir/Enum.html#all/2) must traverse the entire collection if all elements pass the condition, or if a failing element is towards the end of the list.
If [Enum.all?/2](https://hexdocs.pm/elixir/Enum.html#all?/2) must traverse the entire collection if all elements pass the condition, or if a failing element is towards the end of the list.

Notice the code below should take awhile to finish running because every element passes the condition.

Expand All @@ -591,7 +591,7 @@ Enum.all?(1..10_000_000, fn integer -> is_integer(integer) end)

### Your Turn

Use [Enum.all/2](https://hexdocs.pm/elixir/Enum.html#all/2) to determine if all of the `colors` in this list of colors are `:green`. You may change the value of `colors` to experiment with [Enum.all/2](https://hexdocs.pm/elixir/Enum.html#all/2).
Use [Enum.all?/2](https://hexdocs.pm/elixir/Enum.html#all?/2) to determine if all of the `colors` in this list of colors are `:green`. You may change the value of `colors` to experiment with [Enum.all?/2](https://hexdocs.pm/elixir/Enum.html#all?/2).

```elixir
colors = [:green, :green, :red]
Expand Down
2 changes: 1 addition & 1 deletion reading/non_enumerables.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ 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.
We can split a string into a list of characters by splitting on every empty space `""` This does create an empty string at the start and the end of the list.

```elixir
String.split("abcde", "")
Expand Down

0 comments on commit 1b07ae2

Please sign in to comment.