Eliminate some calls to ‘tail’

This commit is contained in:
Eelco Dolstra 2012-08-13 14:19:31 -04:00
parent 431c55cbf1
commit c0a483632c
5 changed files with 12 additions and 12 deletions

View file

@ -169,11 +169,11 @@ rec {
# order. The implementation does a quick-sort. # order. The implementation does a quick-sort.
sort = strictLess: list: sort = strictLess: list:
let let
# This implementation only have one element lists on the left hand # This implementation only has one element list on the left hand
# side of the concatenation operator. # side of the concatenation operator.
qs = l: concat: qs = l: concat:
if l == [] then concat if l == [] then concat
else if tail l == [] then l ++ concat else if length l == 1 then l ++ concat
else let else let
part = partition (strictLess (head l)) (tail l); part = partition (strictLess (head l)) (tail l);
in in

View file

@ -2,7 +2,7 @@
let lib = import ./default.nix; in let lib = import ./default.nix; in
with { inherit (builtins) head tail; }; with { inherit (builtins) head; };
with import ./trivial.nix; with import ./trivial.nix;
with import ./lists.nix; with import ./lists.nix;
with import ./misc.nix; with import ./misc.nix;
@ -64,7 +64,7 @@ rec {
config = getConfig; config = getConfig;
} // ( } // (
if getImportedSets != [] then if getImportedSets != [] then
assert tail getImportedSets == []; assert length getImportedSets == 1;
{ options = head getImportedSets; } { options = head getImportedSets; }
else else
{} {}

View file

@ -2,7 +2,7 @@
let lib = import ./default.nix; in let lib = import ./default.nix; in
with { inherit (builtins) head tail; }; with { inherit (builtins) head length; };
with import ./trivial.nix; with import ./trivial.nix;
with import ./lists.nix; with import ./lists.nix;
with import ./misc.nix; with import ./misc.nix;
@ -133,7 +133,7 @@ rec {
# separate the merge & apply fields from the interface. # separate the merge & apply fields from the interface.
mergeOptionDecls = opts: mergeOptionDecls = opts:
if opts == [] then {} if opts == [] then {}
else if tail opts == [] then else if length opts == 1 then
let opt = head opts; in let opt = head opts; in
if opt ? options then if opt ? options then
opt // { options = toList opt.options; } opt // { options = toList opt.options; }
@ -189,7 +189,7 @@ rec {
) (attrNames defs)); ) (attrNames defs));
mergeDefaultOption = list: mergeDefaultOption = list:
if list != [] && tail list == [] then head list if length list == 1 then head list
else if all builtins.isFunction list then x: mergeDefaultOption (map (f: f x) list) else if all builtins.isFunction list then x: mergeDefaultOption (map (f: f x) list)
else if all isList list then concatLists list else if all isList list then concatLists list
else if all isAttrs list then fold lib.mergeAttrs {} list else if all isAttrs list then fold lib.mergeAttrs {} list
@ -214,7 +214,7 @@ rec {
mergeOneOption = list: mergeOneOption = list:
if list == [] then abort "This case should never happen." if list == [] then abort "This case should never happen."
else if tail list != [] then throw "Multiple definitions. Only one is allowed for this option." else if length list != 1 then throw "Multiple definitions. Only one is allowed for this option."
else head list; else head list;

View file

@ -2,12 +2,12 @@
let lib = import ./default.nix; let lib = import ./default.nix;
inherit (builtins) add sub lessThan; inherit (builtins) add sub lessThan length;
in in
rec { rec {
inherit (builtins) stringLength substring head tail lessThan sub; inherit (builtins) stringLength substring head tail;
# Concatenate a list of strings. # Concatenate a list of strings.
@ -22,7 +22,7 @@ rec {
# Place an element between each element of a list, e.g., # Place an element between each element of a list, e.g.,
# `intersperse "," ["a" "b" "c"]' returns ["a" "," "b" "," "c"]. # `intersperse "," ["a" "b" "c"]' returns ["a" "," "b" "," "c"].
intersperse = separator: list: intersperse = separator: list:
if list == [] || tail list == [] if list == [] || length list == 1
then list then list
else [(head list) separator] else [(head list) separator]
++ (intersperse separator (tail list)); ++ (intersperse separator (tail list));

View file

@ -157,7 +157,7 @@ rec {
uniq = elemType: mkOptionType { uniq = elemType: mkOptionType {
inherit (elemType) name check iter fold docPath hasOptions; inherit (elemType) name check iter fold docPath hasOptions;
merge = list: merge = list:
if tail list == [] then if length list == 1 then
head list head list
else else
throw "Multiple definitions. Only one is allowed for this option."; throw "Multiple definitions. Only one is allowed for this option.";