#I'm misunderstanding the question - Elixir secrets adder

48 messages · Page 1 of 1 (latest)

rose owl
#

The question says i should create an adder that takes a variable and adds it to itself. The test for this passes. But then I am supposed to have it add to the previous added amount is this correct?

Because following the guide I have

  def secret_add(secret) do
    # Please implement the secret_add/1 function
    adder = fn secret ->
      secret + secret
    end
  end```

Should I be creating 2 anonymous functions. One for the first addition and any subsequent additions?

Because this fails too

```defmodule Secrets do
  def secret_add(secret) do
    # Please implement the secret_add/1 function
    adder = fn secret ->
      secret + secret
    next_add = fn secret ->
      adder + secret
      end
    end
  end```
slender rune
#

Note how you have two functions that both take one variable ... and you use the same variable name for both.

#

That makes it hard to select one vs the other, which I believe is what is supposed to happen

kindred scroll
#

What is the error message for the second version of your code that fails the tests?

rose owl
#

This is very close because the assert is correct but receives the function

#
  def secret_add(secret) do
    # Please implement the secret_add/1 function

    adder = fn secret ->
          secret + secret

    secret_add(secret)
      end
    end```
#
  1. test secret_add add 6 (SecretsTest)
    test/secrets_test.exs:13
    Assertion with === failed
    code: assert add.(9) === 15
    left: #Function<0.18572931/1 in Secrets.secret_add/1>
    right: 15
    stacktrace:
    test/secrets_test.exs:16: (test)
slender rune
#

You're doubling the second argument, not adding values

#

Or adding the second argument to itself

#

The function is supposed to add two different values

rose owl
#

It has only 1 input. The first iteration it adds to itself. But the second time its supposed to take the result of the first iteration and add the new value(secret)

#

Implement Secrets.secret_add/1. It should return a function which takes one argument and adds to it the argument passed in to secret_add.

adder = Secrets.secret_add(2)
adder.(2)

=> 4

kindred scroll
#

Those instructions don’t mention adding the first argument to itself.

rose owl
#

No but thats what the second test asserts

kindred scroll
#

I see a 9 being added to a 15 in the second test. Those are different values.

rose owl
#

Yes they add 9. This adds to the first iteration result of 6 === 15. I had expected from the assignment question the result should have been 18.

#

And I am assuming the 6 is from the first test passing result of 6 because its not called in the function anywhere

slender rune
#

9 + 6 = 15

#

If you look at the code run, it passes 9 then 6

#

Each test is entirely independent of the other tests

hybrid skiff
#

Personally I think you should re-read the introduction to properly understand closure and anonymous function

slender rune
#

(or, should be)

hybrid skiff
#

it is a bit hard to get at first. so read it until you understand it

slender rune
#

You are not on the right track 🙂

kindred scroll
#

Might be worth tweaking that second test to use a set of different numbers to avoid this misunderstanding about the two tests in the future.

rose owl
#

Apparently I need to use Biwise to get this to work

#

Bitwise

#

This is the solution. So now I will review how Bitwise is doing this as it is what is causing the problem in understandin

#
     use Bitwise
     fn n -> n + secret 
     end
  end```
slender rune
#

What makes you think you need bitwise? Plain regular addition is all you need.

#

Assuming you get the right values. 9+6=15 doesn't need bitwise.

rose owl
#

Yes I'm a bit confused. I'm going to Hex to read the docs I'm not quite getting it from the material provided

slender rune
#

The important bit here is the two distinct variables and the +

#

And how the variables are sent at different points, one of which is stored in the inner function for you.

#

Aka a "closure"

rose owl
#

Thank you. Thats the part I'm not understanding. How does n receive a value in the first run of the function. It appears to be n(not used) + secret(3) = 6 but it must be used I just need to understand how

slender rune
#

Why would n be unused?

#

It is an argument to the inner function and used to compute the return value

rose owl
#

fn n -> n + 3(received from secret variable)

#

so then n = 3

#

then it parses again to get fn n -> 3 + 3

#

which we then get 6 from

#

so n is a variable to the anonymous function that on initation has no value. Not until n + 3 makes the function var have a value of 3.

#

and no value is not 0 its just no value

grand mountain
#

Apparently I need to use Biwise to get this to work

Bitwise is only needed for steps 5, 6, and 7 of this exercise. You're stuck at step 1.

grand mountain