forked from lix-project/lix
Add sort primop
This commit is contained in:
parent
50807f3dd5
commit
76cc8e97a2
|
@ -779,6 +779,29 @@ builtins.replaceStrings ["oo" "a"] ["a" "i"] "foobar"
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
|
|
||||||
|
<varlistentry><term><function>builtins.sort</function>
|
||||||
|
<replaceable>comparator</replaceable> <replaceable>list</replaceable></term>
|
||||||
|
|
||||||
|
<listitem><para>Return <replaceable>list</replaceable> in sorted
|
||||||
|
order. It repeatedly calls the function
|
||||||
|
<replaceable>comparator</replaceable> with two elements. The
|
||||||
|
comparator should return <literal>true</literal> if the first
|
||||||
|
element is less than the second, and <literal>false</literal>
|
||||||
|
otherwise. For example,
|
||||||
|
|
||||||
|
<programlisting>
|
||||||
|
builtins.sort builtins.lessThan [ 483 249 526 147 42 77 ]
|
||||||
|
</programlisting>
|
||||||
|
|
||||||
|
produces the list <literal>[ 42 77 147 249 483 526
|
||||||
|
]</literal>.</para>
|
||||||
|
|
||||||
|
<para>This is a stable sort: it preserves the relative order of
|
||||||
|
elements deemed equal by the comparator.</para></listitem>
|
||||||
|
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
|
|
||||||
<varlistentry><term><function>builtins.stringLength</function>
|
<varlistentry><term><function>builtins.stringLength</function>
|
||||||
<replaceable>e</replaceable></term>
|
<replaceable>e</replaceable></term>
|
||||||
|
|
||||||
|
|
|
@ -1364,7 +1364,6 @@ static void prim_all(EvalState & state, const Pos & pos, Value * * args, Value &
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Apply a function to every element of a list. */
|
|
||||||
static void prim_genList(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
static void prim_genList(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||||
{
|
{
|
||||||
state.forceFunction(*args[0], pos);
|
state.forceFunction(*args[0], pos);
|
||||||
|
@ -1383,6 +1382,41 @@ static void prim_genList(EvalState & state, const Pos & pos, Value * * args, Val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void prim_lessThan(EvalState & state, const Pos & pos, Value * * args, Value & v);
|
||||||
|
|
||||||
|
|
||||||
|
static void prim_sort(EvalState & state, const Pos & pos, Value * * args, Value & v)
|
||||||
|
{
|
||||||
|
state.forceFunction(*args[0], pos);
|
||||||
|
state.forceList(*args[1], pos);
|
||||||
|
|
||||||
|
auto len = args[1]->listSize();
|
||||||
|
state.mkList(v, len);
|
||||||
|
for (unsigned int n = 0; n < len; ++n) {
|
||||||
|
state.forceValue(*args[1]->listElems()[n]);
|
||||||
|
v.listElems()[n] = args[1]->listElems()[n];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
auto comparator = [&](Value * a, Value * b) {
|
||||||
|
/* Optimization: if the comparator is lessThan, bypass
|
||||||
|
callFunction. */
|
||||||
|
if (args[0]->type == tPrimOp && args[0]->primOp->fun == prim_lessThan)
|
||||||
|
return CompareValues()(a, b);
|
||||||
|
|
||||||
|
Value vTmp1, vTmp2;
|
||||||
|
state.callFunction(*args[0], *a, vTmp1, pos);
|
||||||
|
state.callFunction(vTmp1, *b, vTmp2, pos);
|
||||||
|
return state.forceBool(vTmp2);
|
||||||
|
};
|
||||||
|
|
||||||
|
/* FIXME: std::sort can segfault if the comparator is not a strict
|
||||||
|
weak ordering. What to do? std::stable_sort() seems more
|
||||||
|
resilient, but no guarantees... */
|
||||||
|
std::stable_sort(v.listElems(), v.listElems() + len, comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*************************************************************
|
/*************************************************************
|
||||||
* Integer arithmetic
|
* Integer arithmetic
|
||||||
*************************************************************/
|
*************************************************************/
|
||||||
|
@ -1779,6 +1813,7 @@ void EvalState::createBaseEnv()
|
||||||
addPrimOp("__any", 2, prim_any);
|
addPrimOp("__any", 2, prim_any);
|
||||||
addPrimOp("__all", 2, prim_all);
|
addPrimOp("__all", 2, prim_all);
|
||||||
addPrimOp("__genList", 2, prim_genList);
|
addPrimOp("__genList", 2, prim_genList);
|
||||||
|
addPrimOp("__sort", 2, prim_sort);
|
||||||
|
|
||||||
// Integer arithmetic
|
// Integer arithmetic
|
||||||
addPrimOp("__add", 2, prim_add);
|
addPrimOp("__add", 2, prim_add);
|
||||||
|
|
1
tests/lang/eval-okay-sort.exp
Normal file
1
tests/lang/eval-okay-sort.exp
Normal file
|
@ -0,0 +1 @@
|
||||||
|
[ [ 42 77 147 249 483 526 ] [ 526 483 249 147 77 42 ] [ "bar" "fnord" "foo" "xyzzy" ] [ { key = 1; value = "foo"; } { key = 1; value = "fnord"; } { key = 2; value = "bar"; } ] ]
|
8
tests/lang/eval-okay-sort.nix
Normal file
8
tests/lang/eval-okay-sort.nix
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
with builtins;
|
||||||
|
|
||||||
|
[ (sort lessThan [ 483 249 526 147 42 77 ])
|
||||||
|
(sort (x: y: y < x) [ 483 249 526 147 42 77 ])
|
||||||
|
(sort lessThan [ "foo" "bar" "xyzzy" "fnord" ])
|
||||||
|
(sort (x: y: x.key < y.key)
|
||||||
|
[ { key = 1; value = "foo"; } { key = 2; value = "bar"; } { key = 1; value = "fnord"; } ])
|
||||||
|
]
|
Loading…
Reference in a new issue