In this exercise, you've been tasked with writing the software for an encryption device that works by performing transformations on data. You need a way to flexibly create complicated functions by combining simpler functions together.
For each task, make use of a closure and return a function that can be invoked from the calling scope.
All functions should expect integer parameters. Integers are also suitable for performing bitwise operations in Elixir.
You have seven tasks:
Implement Secrets.secret_add/1
, have it return a function which takes one parameter and adds it to the parameter passed in to secret_add
.
adder = Secrets.secret_add(2)
adder.(2)
# => 4
Implement Secrets.secret_subtract/1
, have it return a function which takes one parameter and subtracts the parameter passed in to secret_subtract
.
subtractor = Secrets.secret_subtract(2)
subtractor.(3)
# => 1
Implement Secrets.secret_multiply/1
, have it return a function which takes one parameter and multiplies it by the parameter passed in to secret_multiply
.
multiplier = Secrets.secret_multiply(7)
multiplier.(3)
# => 21
Implement Secrets.secret_divide/1
, have it return a function which takes one parameter and divides it by the parameter passed in to secret_divide
.
divider = Secrets.secret_divide(3)
divider.(32)
# => 10
Make use of integer division.
Implement Secrets.secret_and/1
, have it return a function which takes one parameter and performs a bitwise and operation to the parameter passed in to secret_and
.
ander = Secrets.secret_and(1)
ander.(2)
# => 0
Implement Secrets.secret_xor/1
, have it return a function which takes one parameter and performs a bitwise xor operation to the parameter passed in to secret_xor
.
xorer = Secrets.secret_xor(1)
xorer.(3)
# => 2
Implement Secrets.secret_combine/2
, have it return a function which applies the functions parameter passed in to secret_combine
in sequence.
multiply = Secrets.secret_multiply(7)
divide = Secrets.secret_divide(3)
combined = Secrets.secret_combine(multiply, divide)
combined.(6)
# => 14