Giter Site home page Giter Site logo

Comments (5)

iteles avatar iteles commented on August 15, 2024 1

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.

screen shot 2017-08-18 at 19 41 12

1.3

screen shot 2017-08-18 at 19 42 06

1.4

screen shot 2017-08-18 at 19 42 06

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.
screen shot 2017-08-18 at 19 44 42

1.6 Running transactions

Add a third argument to the run_transaction function definition called transaction.
screen shot 2017-08-18 at 19 45 49

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.
screen shot 2017-08-18 at 19 53 23

# 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.

iteles avatar iteles commented on August 15, 2024 1

Done 👍
Can be found: https://github.com/iteles/learning/blob/master/codeschool-mixing-it-up-with-elixir.md

from learning.

iteles avatar iteles commented on August 15, 2024

Level 2

2.2
screen shot 2017-08-19 at 10 24 13

2.3
screen shot 2017-08-19 at 10 26 03

2.4
screen shot 2017-08-19 at 10 37 01

Level 3

3.2
screen shot 2017-08-19 at 10 46 50

3.3
screen shot 2017-08-19 at 10 48 07

{paradigm, language}  = {:functional, "Elixir"}
IO.puts "Paradigm: #{paradigm}"
IO.puts "Language: #{language}"

3.4 Tuples in Function Signature
screen shot 2017-08-19 at 10 49 52

3.6
screen shot 2017-08-19 at 11 20 30

3.7
screen shot 2017-08-19 at 11 22 23

3.9 Account Map
screen shot 2017-08-19 at 12 13 26

3.10 Person Map
screen shot 2017-08-19 at 12 23 32

3.11 Map Pattern Matching
screen shot 2017-08-19 at 12 27 52

3.12 Nested Map Pattern Matching
screen shot 2017-08-19 at 14 28 15

from learning.

iteles avatar iteles commented on August 15, 2024

Level 4

4.2 case Tests
screen shot 2017-08-19 at 14 47 23

4.6 cond checks
screen shot 2017-08-19 at 15 14 46

from learning.

iteles avatar iteles commented on August 15, 2024

Level 5

5.3 Calculating expenses
screen shot 2017-08-19 at 15 43 55

5.5 File Extensions
screen shot 2017-08-19 at 15 51 14

5.7 Declaring dependencies
screen shot 2017-08-19 at 16 21 37

5.10 Mixing data
screen shot 2017-08-19 at 17 44 00

from learning.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.