|
| 1 | +defmodule ControlFlow do |
| 2 | + @moduledoc false |
| 3 | + use Koans |
| 4 | + |
| 5 | + @intro "Control Flow - Making decisions and choosing paths" |
| 6 | + |
| 7 | + koan "If statements evaluate conditions" do |
| 8 | + result = if true, do: "yes", else: "no" |
| 9 | + assert result == ___ |
| 10 | + end |
| 11 | + |
| 12 | + koan "If can be written in block form" do |
| 13 | + result = |
| 14 | + if 1 + 1 == 2 do |
| 15 | + "math works" |
| 16 | + else |
| 17 | + "math is broken" |
| 18 | + end |
| 19 | + |
| 20 | + assert result == ___ |
| 21 | + end |
| 22 | + |
| 23 | + koan "Unless is the opposite of if" do |
| 24 | + result = unless false, do: "will execute", else: "will not execute" |
| 25 | + assert result == ___ |
| 26 | + end |
| 27 | + |
| 28 | + koan "Nil and false are falsy, everything else is truthy" do |
| 29 | + assert if(nil, do: "truthy", else: "falsy") == ___ |
| 30 | + assert if(false, do: "truthy", else: "falsy") == ___ |
| 31 | + assert if(0, do: "truthy", else: "falsy") == ___ |
| 32 | + assert if("", do: "truthy", else: "falsy") == ___ |
| 33 | + assert if([], do: "truthy", else: "falsy") == ___ |
| 34 | + end |
| 35 | + |
| 36 | + koan "Case matches against patterns" do |
| 37 | + result = |
| 38 | + case {1, 2, 3} do |
| 39 | + {4, 5, 6} -> "no match" |
| 40 | + {1, x, 3} -> "matched with x = #{x}" |
| 41 | + end |
| 42 | + |
| 43 | + assert result == ___ |
| 44 | + end |
| 45 | + |
| 46 | + koan "Case can have multiple clauses with different patterns" do |
| 47 | + check_number = fn x -> |
| 48 | + case x do |
| 49 | + 0 -> "zero" |
| 50 | + n when n > 0 -> "positive" |
| 51 | + n when n < 0 -> "negative" |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + assert check_number.(5) == ___ |
| 56 | + assert check_number.(0) == ___ |
| 57 | + assert check_number.(-3) == ___ |
| 58 | + end |
| 59 | + |
| 60 | + koan "Case clauses are tried in order until one matches" do |
| 61 | + check_list = fn list -> |
| 62 | + case list do |
| 63 | + [] -> "empty" |
| 64 | + [_] -> "one element" |
| 65 | + [_, _] -> "two elements" |
| 66 | + _ -> "many elements" |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + assert check_list.([]) == ___ |
| 71 | + assert check_list.([:a]) == ___ |
| 72 | + assert check_list.([:a, :b]) == ___ |
| 73 | + assert check_list.([:a, :b, :c, :d]) == ___ |
| 74 | + end |
| 75 | + |
| 76 | + koan "Cond evaluates conditions until one is truthy" do |
| 77 | + temperature = 25 |
| 78 | + |
| 79 | + weather = |
| 80 | + cond do |
| 81 | + temperature < 0 -> "freezing" |
| 82 | + temperature < 10 -> "cold" |
| 83 | + temperature < 25 -> "cool" |
| 84 | + temperature < 30 -> "warm" |
| 85 | + true -> "hot" |
| 86 | + end |
| 87 | + |
| 88 | + assert weather == ___ |
| 89 | + end |
| 90 | + |
| 91 | + koan "Cond requires at least one clause to be true" do |
| 92 | + safe_divide = fn x, y -> |
| 93 | + cond do |
| 94 | + y == 0 -> {:error, "division by zero"} |
| 95 | + true -> {:ok, x / y} |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + assert safe_divide.(10, 2) == ___ |
| 100 | + assert safe_divide.(10, 0) == ___ |
| 101 | + end |
| 102 | + |
| 103 | + koan "If/unless can be used as statement modifiers" do |
| 104 | + x = 5 |
| 105 | + message = if x > 0, do: "x is positive" |
| 106 | + assert message == ___ |
| 107 | + end |
| 108 | + |
| 109 | + koan "Case can destructure complex patterns" do |
| 110 | + parse_response = fn response -> |
| 111 | + case response do |
| 112 | + {:ok, %{status: 200, body: body}} -> "Success: #{body}" |
| 113 | + {:ok, %{status: status}} when status >= 400 -> "Client error: #{status}" |
| 114 | + {:ok, %{status: status}} when status >= 500 -> "Server error: #{status}" |
| 115 | + {:error, reason} -> "Request failed: #{reason}" |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + assert parse_response.({:ok, %{status: 200, body: "Hello"}}) == ___ |
| 120 | + assert parse_response.({:ok, %{status: 404}}) == ___ |
| 121 | + assert parse_response.({:error, :timeout}) == ___ |
| 122 | + end |
| 123 | + |
| 124 | + koan "Guards in case can use complex expressions" do |
| 125 | + categorize = fn number -> |
| 126 | + case number do |
| 127 | + n when is_integer(n) and n > 0 and rem(n, 2) == 0 -> "positive even integer" |
| 128 | + n when is_integer(n) and n > 0 and rem(n, 2) == 1 -> "positive odd integer" |
| 129 | + n when is_integer(n) and n < 0 -> "negative integer" |
| 130 | + n when is_float(n) -> "float" |
| 131 | + _ -> "other" |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + assert categorize.(4) == ___ |
| 136 | + assert categorize.(3) == ___ |
| 137 | + assert categorize.(-5) == ___ |
| 138 | + assert categorize.(3.14) == ___ |
| 139 | + assert categorize.("hello") == ___ |
| 140 | + end |
| 141 | + |
| 142 | + koan "Multiple conditions can be checked in sequence" do |
| 143 | + process_user = fn user -> |
| 144 | + if user.active do |
| 145 | + if user.verified do |
| 146 | + if user.premium do |
| 147 | + "premium verified active user" |
| 148 | + else |
| 149 | + "verified active user" |
| 150 | + end |
| 151 | + else |
| 152 | + "unverified active user" |
| 153 | + end |
| 154 | + else |
| 155 | + "inactive user" |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + user = %{active: true, verified: true, premium: false} |
| 160 | + assert process_user.(user) == ___ |
| 161 | + end |
| 162 | +end |
0 commit comments