KimberlyBrooke

KimberlyBrooke

Exercism.io Secrets module - stuck on function combiner

Hello, I am working through Exercism.io Secrets module the first 6 functions I was able to figure out just fine. The last one I am stuck on.

This is the instruction for it:

Implement Secrets.secret_combine/2 . It should return a function which takes one argument and applies to it the two functions passed in to secret_combine in order.

multiply = Secrets.secret_multiply(7)
divide = Secrets.secret_divide(3)
combined = Secrets.secret_combine(multiply, divide)

combined.(6)
# => 14

Hints and tips :

Create a function combiner

  • Return an anonymous function which composes the functions passed in to Secret.secret_combine/2.
  • Use a . before () when calling an anonymous function.

Below is where my solutions start, like I said the first 6 functions I did just fine on it’s just the last one.

defmodule Secrets do
  def secret_add(secret) do
    fn(a) -> a + secret end
  end

  def secret_subtract(secret) do
    fn(a) -> a - secret end
  end

  def secret_multiply(secret) do
    fn(a) -> a * secret end
  end

  def secret_divide(secret) do
    fn(a) -> div(a, secret) end
  end 

  def secret_and(secret) do
  use Bitwise, only_operators: true
    fn(a) -> a &&& secret end
  end

  def secret_xor(secret) do
    use Bitwise
    fn(a) -> bxor(a, secret) end
  end

  def secret_combine(secret_function1, secret_function2) do
  end
end

Marked As Solved

cmo

cmo

def secret_combine(fn1, fn2) do
  fn x -> x |> fn1.() |> fn2.() end
end

Also Liked

KimberlyBrooke

KimberlyBrooke

Thank you!

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement