1
0
Fork 0
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:
Matan Bendix Shenhav 2017-08-13 16:25:41 +03:00
parent 489899b8be
commit dfc15beb0d

View file

@ -12,8 +12,8 @@ xml:id="functions-and-imports">
linkend="basics-of-the-language">fourth pill</link> we touched the
Nix language for a moment. We introduced basic types and values of
the Nix language, and basic expressions such as
<literal>if</literal>, <literal>with</literal> and
<literal>let</literal>. I invite you to re-read about these
<code>if</code>, <code>with</code> and
<code>let</code>. I invite you to re-read about these
expressions and play with them in the repl.
</para>
@ -27,8 +27,8 @@ xml:id="functions-and-imports">
</para>
<para>
I remind you how to enter the Nix environment: <literal>source
~/.nix-profile/etc/profile.d/nix.sh</literal>
I remind you how to enter the Nix environment: <code>source
~/.nix-profile/etc/profile.d/nix.sh</code>
</para>
</section>
@ -39,14 +39,14 @@ xml:id="functions-and-imports">
<para>
Functions are anonymous (lambdas), and only have a single parameter.
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>
<screen><xi:include href="./05/anon-function.txt" parse="text" /></screen>
<para>
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...
joke!
</para>
@ -60,18 +60,18 @@ xml:id="functions-and-imports">
<para>
As usual, please ignore the special syntax for assignments inside
nix-repl. So, we defined a function <literal>x: x*2</literal> that
takes one parameter <literal>x</literal>, and returns
<literal>x*2</literal>. This function is then assigned to the
variable <literal>double</literal>. Finally we did our first
function call: <literal>double 3</literal>.
nix-repl. So, we defined a function <code>x: x*2</code> that
takes one parameter <code>x</code>, and returns
<code>x*2</code>. This function is then assigned to the
variable <code>double</code>. Finally we did our first
function call: <code>double 3</code>.
</para>
<para>
<emphasis role="underline">Big note:</emphasis> it's not like many
other programming languages where you write
<literal>double(3)</literal>. It really is <literal>double
3</literal>.
<code>double(3)</code>. It really is <code>double
3</code>.
</para>
<para>
@ -93,12 +93,12 @@ xml:id="functions-and-imports">
/></screen>
<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
parameter <literal>b</literal> and returns <literal>a*b</literal>.
Therefore, calling <literal>mul 3</literal> returns this kind of
function: <literal>b: 3*b</literal>. In turn, we call the returned
function with <literal>4</literal>, and get the expected result.
parameter <code>b</code> and returns <code>a*b</code>.
Therefore, calling <code>mul 3</code> returns this kind of
function: <code>b: 3*b</code>. In turn, we call the returned
function with <code>4</code>, and get the expected result.
</para>
<para>
@ -113,7 +113,7 @@ xml:id="functions-and-imports">
Much more readable, you don't even notice that functions only
receive one argument. Since the argument is separated by a space, to
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>
@ -125,7 +125,7 @@ xml:id="functions-and-imports">
/></screen>
<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.
</para>
</section>
@ -136,7 +136,7 @@ xml:id="functions-and-imports">
<para>
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
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.
</para>
@ -144,18 +144,18 @@ xml:id="functions-and-imports">
<para>
In the first case we defined a function that accepts a single
parameter. We then access attributes <literal>a</literal> and
<literal>b</literal> from the given set. Note how the
parameter. We then access attributes <code>a</code> and
<code>b</code> from the given set. Note how the
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.
</para>
<para>
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
the keys <literal>a</literal> and <literal>b</literal>. Then we can
use those <literal>a</literal> and <literal>b</literal> in the
the keys <code>a</code> and <code>b</code>. Then we can
use those <code>a</code> and <code>b</code> in the
function body directly.
</para>
@ -184,7 +184,7 @@ xml:id="functions-and-imports">
role="strong">variadic</emphasis>) than the expected ones:
</para>
<screen><xi:include href="./05/veradic-arguments.txt" parse="text"
<screen><xi:include href="./05/variadic-arguments.txt" parse="text"
/></screen>
<para>
@ -245,7 +245,7 @@ xml:id="functions-and-imports">
<title>Imports</title>
<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
is to define each component in a <filename>.nix</filename> file,
then compose by importing these files.
@ -259,19 +259,19 @@ xml:id="functions-and-imports">
<filename>a.nix</filename>:
</para>
<screen><xi:include href="./05/a-nix.txt" parse="text" /></screen>
<programlisting><xi:include href="./05/a-nix.txt" parse="text" /></programlisting>
<para>
<filename>b.nix</filename>:
</para>
<screen><xi:include href="./05/b-nix.txt" parse="text" /></screen>
<programlisting><xi:include href="./05/b-nix.txt" parse="text" /></programlisting>
<para>
<filename>mul.nix</filename>:
</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>
@ -285,7 +285,7 @@ xml:id="functions-and-imports">
<filename>test.nix</filename>:
</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>
@ -310,13 +310,13 @@ xml:id="functions-and-imports">
<para>
In <filename>test.nix</filename> we return a function.
It accepts a set, with default attributes
<literal>b</literal>, <literal>trueMsg</literal> and
<literal>falseMsg</literal>.
<code>b</code>, <code>trueMsg</code> and
<code>falseMsg</code>.
</para>
</listitem>
<listitem>
<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
function</link> that takes two arguments. The first is
the message to display, the second is the value to