bigmachine-io / taking-off

93 stars 27 forks source link

Named tuple in func signature? What is this? #3

Closed trosel closed 7 years ago

trosel commented 7 years ago

{:inches, inches} = val

What is that doing? Is it a named tuple? You do not use val in the to_light_seconds function, so I'm not sure why it's there at all. And you can simply pass the result to round_up without having it be the name of val, so I just don't understand the point of it in the code. Yet, it's consistently in this entire chapter up until the very end.

def to_light_seconds({:inches, inches} = val)  do
    (inches * 8.472522095734715723e-11) |> round_up
end

def round_up(val) when is_float(val), do: trunc(val)
tomcola512 commented 7 years ago

the passed "val" (assumed to be a tuple) is being "unboxed"/"destructured"/"pattern matched" such that val's second element is bound to the alias/variable inches, so long as its first element matches the atom :inches

clearjs commented 7 years ago

val is the same as the tuple on the left. It is unused, so redundant here. But it could be useful if the function needed the whole tuple, not only its part, e.g., to pass it to another function.