mirror of
https://github.com/NixOS/nix-pills
synced 2024-09-19 04:00:13 -04:00
Pill 5 cleanup: changed <literal>s for <code>s, fixed missing include, changed some <screen>s to <programlistings> where appropriate.
This commit is contained in:
parent
489899b8be
commit
dfc15beb0d
|
@ -12,8 +12,8 @@ xml:id="functions-and-imports">
|
||||||
linkend="basics-of-the-language">fourth pill</link> we touched the
|
linkend="basics-of-the-language">fourth pill</link> we touched the
|
||||||
Nix language for a moment. We introduced basic types and values of
|
Nix language for a moment. We introduced basic types and values of
|
||||||
the Nix language, and basic expressions such as
|
the Nix language, and basic expressions such as
|
||||||
<literal>if</literal>, <literal>with</literal> and
|
<code>if</code>, <code>with</code> and
|
||||||
<literal>let</literal>. I invite you to re-read about these
|
<code>let</code>. I invite you to re-read about these
|
||||||
expressions and play with them in the repl.
|
expressions and play with them in the repl.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
@ -27,8 +27,8 @@ xml:id="functions-and-imports">
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
I remind you how to enter the Nix environment: <literal>source
|
I remind you how to enter the Nix environment: <code>source
|
||||||
~/.nix-profile/etc/profile.d/nix.sh</literal>
|
~/.nix-profile/etc/profile.d/nix.sh</code>
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
|
@ -39,14 +39,14 @@ xml:id="functions-and-imports">
|
||||||
<para>
|
<para>
|
||||||
Functions are anonymous (lambdas), and only have a single parameter.
|
Functions are anonymous (lambdas), and only have a single parameter.
|
||||||
The syntax is extremely simple. Type the parameter name, then
|
The syntax is extremely simple. Type the parameter name, then
|
||||||
"<literal>:</literal>", then the body of the function.
|
"<code>:</code>", then the body of the function.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<screen><xi:include href="./05/anon-function.txt" parse="text" /></screen>
|
<screen><xi:include href="./05/anon-function.txt" parse="text" /></screen>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
So here we defined a function that takes a parameter
|
So here we defined a function that takes a parameter
|
||||||
<literal>x</literal>, and returns <literal>x*2</literal>. The
|
<code>x</code>, and returns <code>x*2</code>. The
|
||||||
problem is that we cannot use it in any way, because it's unnamed...
|
problem is that we cannot use it in any way, because it's unnamed...
|
||||||
joke!
|
joke!
|
||||||
</para>
|
</para>
|
||||||
|
@ -60,18 +60,18 @@ xml:id="functions-and-imports">
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
As usual, please ignore the special syntax for assignments inside
|
As usual, please ignore the special syntax for assignments inside
|
||||||
nix-repl. So, we defined a function <literal>x: x*2</literal> that
|
nix-repl. So, we defined a function <code>x: x*2</code> that
|
||||||
takes one parameter <literal>x</literal>, and returns
|
takes one parameter <code>x</code>, and returns
|
||||||
<literal>x*2</literal>. This function is then assigned to the
|
<code>x*2</code>. This function is then assigned to the
|
||||||
variable <literal>double</literal>. Finally we did our first
|
variable <code>double</code>. Finally we did our first
|
||||||
function call: <literal>double 3</literal>.
|
function call: <code>double 3</code>.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
<emphasis role="underline">Big note:</emphasis> it's not like many
|
<emphasis role="underline">Big note:</emphasis> it's not like many
|
||||||
other programming languages where you write
|
other programming languages where you write
|
||||||
<literal>double(3)</literal>. It really is <literal>double
|
<code>double(3)</code>. It really is <code>double
|
||||||
3</literal>.
|
3</code>.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
|
@ -93,12 +93,12 @@ xml:id="functions-and-imports">
|
||||||
/></screen>
|
/></screen>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
We defined a function that takes the parameter <literal>a</literal>,
|
We defined a function that takes the parameter <code>a</code>,
|
||||||
the body returns another function. This other function takes a
|
the body returns another function. This other function takes a
|
||||||
parameter <literal>b</literal> and returns <literal>a*b</literal>.
|
parameter <code>b</code> and returns <code>a*b</code>.
|
||||||
Therefore, calling <literal>mul 3</literal> returns this kind of
|
Therefore, calling <code>mul 3</code> returns this kind of
|
||||||
function: <literal>b: 3*b</literal>. In turn, we call the returned
|
function: <code>b: 3*b</code>. In turn, we call the returned
|
||||||
function with <literal>4</literal>, and get the expected result.
|
function with <code>4</code>, and get the expected result.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
|
@ -113,7 +113,7 @@ xml:id="functions-and-imports">
|
||||||
Much more readable, you don't even notice that functions only
|
Much more readable, you don't even notice that functions only
|
||||||
receive one argument. Since the argument is separated by a space, to
|
receive one argument. Since the argument is separated by a space, to
|
||||||
pass more complex expressions you need parenthesis. In other common
|
pass more complex expressions you need parenthesis. In other common
|
||||||
languages you would write <literal>mul(6+7, 8+9)</literal>.
|
languages you would write <code>mul(6+7, 8+9)</code>.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
|
@ -125,7 +125,7 @@ xml:id="functions-and-imports">
|
||||||
/></screen>
|
/></screen>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
We stored the function returned by <literal>mul 3</literal> into a
|
We stored the function returned by <code>mul 3</code> into a
|
||||||
variable foo, then reused it.
|
variable foo, then reused it.
|
||||||
</para>
|
</para>
|
||||||
</section>
|
</section>
|
||||||
|
@ -136,7 +136,7 @@ xml:id="functions-and-imports">
|
||||||
<para>
|
<para>
|
||||||
Now this is a very cool feature of Nix. It is possible to pattern
|
Now this is a very cool feature of Nix. It is possible to pattern
|
||||||
match over a set in the parameter. We write an alternative version
|
match over a set in the parameter. We write an alternative version
|
||||||
of <literal>mul = a: b: a*b</literal> first by using a set as
|
of <code>mul = a: b: a*b</code> first by using a set as
|
||||||
argument, then using pattern matching.
|
argument, then using pattern matching.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
@ -144,18 +144,18 @@ xml:id="functions-and-imports">
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
In the first case we defined a function that accepts a single
|
In the first case we defined a function that accepts a single
|
||||||
parameter. We then access attributes <literal>a</literal> and
|
parameter. We then access attributes <code>a</code> and
|
||||||
<literal>b</literal> from the given set. Note how the
|
<code>b</code> from the given set. Note how the
|
||||||
parenthesis-less syntax for function calls is very elegant in this
|
parenthesis-less syntax for function calls is very elegant in this
|
||||||
case, instead of doing <literal>mul({ a=3; b=4; })</literal> in
|
case, instead of doing <code>mul({ a=3; b=4; })</code> in
|
||||||
other languages.
|
other languages.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
In the second case we defined an arguments set. It's like defining a
|
In the second case we defined an arguments set. It's like defining a
|
||||||
set, except without values. We require that the passed set contains
|
set, except without values. We require that the passed set contains
|
||||||
the keys <literal>a</literal> and <literal>b</literal>. Then we can
|
the keys <code>a</code> and <code>b</code>. Then we can
|
||||||
use those <literal>a</literal> and <literal>b</literal> in the
|
use those <code>a</code> and <code>b</code> in the
|
||||||
function body directly.
|
function body directly.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
|
@ -184,7 +184,7 @@ xml:id="functions-and-imports">
|
||||||
role="strong">variadic</emphasis>) than the expected ones:
|
role="strong">variadic</emphasis>) than the expected ones:
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<screen><xi:include href="./05/veradic-arguments.txt" parse="text"
|
<screen><xi:include href="./05/variadic-arguments.txt" parse="text"
|
||||||
/></screen>
|
/></screen>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
|
@ -245,7 +245,7 @@ xml:id="functions-and-imports">
|
||||||
<title>Imports</title>
|
<title>Imports</title>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
The <literal>import</literal> function is built-in and provides a
|
The <code>import</code> function is built-in and provides a
|
||||||
way to parse a <filename>.nix</filename> file. The natural approach
|
way to parse a <filename>.nix</filename> file. The natural approach
|
||||||
is to define each component in a <filename>.nix</filename> file,
|
is to define each component in a <filename>.nix</filename> file,
|
||||||
then compose by importing these files.
|
then compose by importing these files.
|
||||||
|
@ -259,19 +259,19 @@ xml:id="functions-and-imports">
|
||||||
<filename>a.nix</filename>:
|
<filename>a.nix</filename>:
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<screen><xi:include href="./05/a-nix.txt" parse="text" /></screen>
|
<programlisting><xi:include href="./05/a-nix.txt" parse="text" /></programlisting>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
<filename>b.nix</filename>:
|
<filename>b.nix</filename>:
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<screen><xi:include href="./05/b-nix.txt" parse="text" /></screen>
|
<programlisting><xi:include href="./05/b-nix.txt" parse="text" /></programlisting>
|
||||||
|
|
||||||
<para>
|
<para>
|
||||||
<filename>mul.nix</filename>:
|
<filename>mul.nix</filename>:
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<screen><xi:include href="./05/mul-nix.txt" parse="text" /></screen>
|
<programlisting><xi:include href="./05/mul-nix.txt" parse="text" /></programlisting>
|
||||||
|
|
||||||
<screen><xi:include href="./05/import.txt" parse="text" /></screen>
|
<screen><xi:include href="./05/import.txt" parse="text" /></screen>
|
||||||
|
|
||||||
|
@ -285,7 +285,7 @@ xml:id="functions-and-imports">
|
||||||
<filename>test.nix</filename>:
|
<filename>test.nix</filename>:
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
<screen><xi:include href="./05/test-nix.txt" parse="text" /></screen>
|
<programlisting><xi:include href="./05/test-nix.txt" parse="text" /></programlisting>
|
||||||
|
|
||||||
<screen><xi:include href="./05/test-import.txt" parse="text" /></screen>
|
<screen><xi:include href="./05/test-import.txt" parse="text" /></screen>
|
||||||
|
|
||||||
|
@ -310,13 +310,13 @@ xml:id="functions-and-imports">
|
||||||
<para>
|
<para>
|
||||||
In <filename>test.nix</filename> we return a function.
|
In <filename>test.nix</filename> we return a function.
|
||||||
It accepts a set, with default attributes
|
It accepts a set, with default attributes
|
||||||
<literal>b</literal>, <literal>trueMsg</literal> and
|
<code>b</code>, <code>trueMsg</code> and
|
||||||
<literal>falseMsg</literal>.
|
<code>falseMsg</code>.
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>
|
<para>
|
||||||
<literal>builtins.trace</literal> is a <link
|
<code>builtins.trace</code> is a <link
|
||||||
xlink:href="https://nixos.org/nix/manual/#ssec-builtins">built-in
|
xlink:href="https://nixos.org/nix/manual/#ssec-builtins">built-in
|
||||||
function</link> that takes two arguments. The first is
|
function</link> that takes two arguments. The first is
|
||||||
the message to display, the second is the value to
|
the message to display, the second is the value to
|
||||||
|
|
Loading…
Reference in a new issue