A function is a little machine. Give it a value, and it gives you a new value.
In Nix, a colon : separates the parameter, the
local name that receives the input, from what comes back:
Right now the result says «lambda». The machine exists, but we haven’t put
anything into it yet.
Put an argument after the function. That is the value fed into its parameter. No commas, and no brackets needed.
Replace 5 with 7, so the function adds it to 10.
The value gets transformed and the whole expression turns into our result!
mynumber is a custom parameter name. It only exists inside the function.
Usually you name a function with let, so you can call it more than once.
double stays a simple multiplication function. The set can reuse it for both
money and joy.
Functions are also easy to chain. Use each one to transform the last value, then add a few more and see where it ends up.
Now for the piece of syntax you are going to type more often than any other.
${...} is interpolation. It evaluates whatever
is inside and places the result into a string or an attribute name.
Interpolation becomes especially handy inside functions:
Make the greeter say "hello, world!".
One input runs out of usefulness quickly. Write two parameter names to feed a function two values:
Read add 20 22 from left to right: give it 20, then 22. Underneath, Nix
does this one input at a time. The nice part is that calls stay pleasantly
short.
Change the second argument to 3 to shout the battle cry three times.
input: resultcreates a function.function valuecalls one.first: second: resulthandles inputs one at a time.${...}drops a calculated value into a string.- Use a
letbinding to make a function reusable.


Share your thoughts