From f3879811c585e567e5b961159ceb4068f90bf911 Mon Sep 17 00:00:00 2001 From: Benjamin VAUDOUR <60374-benjamin.vaudour@users.noreply.framagit.org> Date: Mon, 27 Dec 2021 11:27:07 +0000 Subject: [PATCH] move less function to common + add list:unzip --- mods/common.elv | 4 ++++ mods/list.elv | 19 ++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/mods/common.elv b/mods/common.elv index f5843d3..7daccbf 100644 --- a/mods/common.elv +++ b/mods/common.elv @@ -25,3 +25,7 @@ fn cexec {|cond v1 v2| put $r } } + +fn less {|cmp| + put {|e1 e2| < ($cmp $e1 $e2 0) +} diff --git a/mods/list.elv b/mods/list.elv index 3d9052a..1a71e63 100644 --- a/mods/list.elv +++ b/mods/list.elv @@ -126,6 +126,19 @@ fn first {|&from=$nil &reverse=$false cb @argv| } } +fn unzip {|cb @argv| + var a1 a2 = [] [] + loop $@argv | each {|e| + var i v = $@e + if ($cb $i $v) { + set @a1 = $@a1 $v + } else { + set @a2 = $@a2 $v + } + } + put $a1 $a2 +} + fn reduce {|acc cb @argv| each {|e| set acc = ($cb $acc $e) @@ -169,10 +182,6 @@ fn swap {|i j @argv| } } -fn less {|cb| - put {|a b| < ($cb $a $b) 0 } -} - fn sort {|cb @argv| - order &less-than=(less $cb) (-l $@argv) + order &less-than=(common:less $cb) (-l $@argv) }