iteles / learning

A repo for various exercises as I'm learning new technologies
19 stars 5 forks source link

CodeSchool Mixing it up with Elixir #17

Closed iteles closed 7 years ago

iteles commented 7 years ago

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.

iteles commented 7 years ago

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.

iteles commented 7 years ago

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
iteles commented 7 years ago

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
iteles commented 7 years ago

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
iteles commented 7 years ago

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