Closed iteles closed 7 years ago
Write an anonymous function that takes one argument called number
and multiplies it by 2. Bind this function to the print_double
variable.
Add a third argument to the run_transaction
function definition called transaction
.
deposit = fn(balance, amount) -> balance + amount end
withdrawal = fn(balance, amount) -> balance - amount end
defmodule Account do
def run_transaction(balance, amount, transaction ) do
end
end
deposit_result = Account.run_transaction(1000, 500, deposit)
IO.puts "New balance: US$#{deposit_result}"
withdrawal_result = Account.run_transaction(1000, 30, withdrawal)
IO.puts "New balance: US$#{withdrawal_result}"
Task 2: Inside the function, define an if statement that checks if amount is greater than 10000. When that occurs, return the string "Cannot perform transaction".
defmodule Account do
def run_transaction(balance, amount, transaction ) do
if amount > 10000 do
"Cannot perform transaction"
end
end
end
Task 3: When amount is NOT greater than 10000, invoke the function assigned to the transaction variable, giving it the arguments balance and amount.
deposit = fn(balance, amount) -> balance + amount end
withdrawal = fn(balance, amount) -> balance - amount end
defmodule Account do
def run_transaction(balance, amount, transaction ) do
if amount > 10000 do
"Cannot perform transaction"
else
transaction.(balance, amount)
end
end
end
deposit_result = Account.run_transaction(1000, 500, deposit)
IO.puts "New balance: US$#{deposit_result}"
withdrawal_result = Account.run_transaction(1000, 30, withdrawal)
IO.puts "New balance: US$#{withdrawal_result}"
Complete each clause with the appropriate atom so that the code behaves as expected.
# ITC: This task also uses the shorthand for the anonymous function `&`, where each argument
# also has to be preceded by a `&`
perform_operation = fn
(values, :addition ) -> Enum.reduce(values, &(&1 + &2))
(values, :multiplication) -> Enum.reduce(values, &(&1 * &2))
end
IO.puts perform_operation.([1, 2, 3, 4], :addition)
IO.puts perform_operation.([1, 2, 3, 4], :multiplication)
On line 2, rewrite the anonymous function from the previous line using the shorthand syntax.
# print_double = fn(number) -> number * 2 end
print_double = &(&1 * 2)
IO.puts print_double.(30)
Caught me out (CMO): the arguments have to be referred to by their number (first argument =1, second =2 and so on) and cannot be referred to in the shorthand by a name such as number
in this case.
2.2
2.3
2.4
3.2
3.3
{paradigm, language} = {:functional, "Elixir"}
IO.puts "Paradigm: #{paradigm}"
IO.puts "Language: #{language}"
3.4 Tuples in Function Signature
3.6
3.7
3.9 Account Map
3.10 Person Map
3.11 Map Pattern Matching
3.12 Nested Map Pattern Matching
4.2 case
Tests
4.6 cond
checks
5.3 Calculating expenses
5.5 File Extensions
5.7 Declaring dependencies
5.10 Mixing data
This weekend, all CodeSchool courses are free, so I plan to try out https://www.codeschool.com/courses/mixing-it-up-with-elixir
This should give me a little boost because it uses active examples throughout and examples are how I learn best.