Comments (5)
1. Anonymous Functions
1.2 Task: On the second line, invoke the anonymous function assigned to the variable and pass your own name as a string argument to it.
1.3
1.4
1.5
Write an anonymous function that takes one argument called number
and multiplies it by 2. Bind this function to the print_double
variable.
1.6 Running transactions
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}"
1.7 Performing Operations
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)
1.8 Shorthand Syntax
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.
from learning.
Done 👍
Can be found: https://github.com/iteles/learning/blob/master/codeschool-mixing-it-up-with-elixir.md
from learning.
Level 2
Level 3
{paradigm, language} = {:functional, "Elixir"}
IO.puts "Paradigm: #{paradigm}"
IO.puts "Language: #{language}"
3.4 Tuples in Function Signature
3.12 Nested Map Pattern Matching
from learning.
Level 4
from learning.
Level 5
from learning.
Related Issues (13)
- PostCSS - Why is everyone talking about it?
- Learn Elixir :tada: HOT 2
- Joy of Elixir HOT 3
- Build a Bank Statement CLI Application With Elixir HOT 1
- APIs and GraphQL HOT 3
- Learning Elm HOT 6
- IPFS protocol
- [time-html-mockups] Refactor so I don't pull in the Font Awesome CDN HOT 1
- Ally HOT 2
- Something like this...? HOT 1
- [hapi-edge] stubbing modules
- Images issue HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from learning.