move less function to common + add list:unzip

This commit is contained in:
Benjamin VAUDOUR 2021-12-27 11:27:07 +00:00
parent 0e6d8dcb0d
commit f3879811c5
2 changed files with 18 additions and 5 deletions

View File

@ -25,3 +25,7 @@ fn cexec {|cond v1 v2|
put $r
}
}
fn less {|cmp|
put {|e1 e2| < ($cmp $e1 $e2 0)
}

View File

@ -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)
}