elvish_config/lib/moi/util/list.elv

297 lines
5.3 KiB
Plaintext
Raw Normal View History

use builtin
use str
2020-03-13 14:03:36 +00:00
fn -p [@argv]{
2021-08-27 20:09:05 +00:00
var c = (count $argv)
if (== $c 0) {
put [ (all) ]
} elif (== $c 1) {
put $argv[0]
} else {
fail '0 or 1 argument needed given '$c' arguments: ['(each [e]{ put '"'$e'"' } $argv | str:join ', ')']'
}
}
fn -r [step begin end]{
if (> $step 0) {
builtin:range &step=$step $begin (+ $end 1)
} else {
2021-08-27 20:09:05 +00:00
var d = (+ $begin $end)
builtin:range &step=(* $step -1) $end (+ $begin 1) | each [e]{ - $d $e }
2020-03-13 14:03:36 +00:00
}
}
fn to-list [@argv]{
try {
-p $@argv
} except e {
put $argv
2020-03-13 14:03:36 +00:00
}
}
fn range [&step=1 b @r]{
if (== $step 0) {
fail 'bad value: step must be positive, but is 0'
}
2021-08-27 20:09:05 +00:00
var c = (count $r)
if (> $c 1) {
fail 'usage: list:range &step=1 begin? end'
}
2021-08-27 20:09:05 +00:00
var e = 0
if (== $c 0) {
if (> $step 0) {
b e = $e $b
}
2020-03-13 14:03:36 +00:00
} else {
e = $r[0]
2020-03-13 14:03:36 +00:00
}
-r $step $b $e
2020-03-13 14:03:36 +00:00
}
fn indexes [&from=$false &step=1 @argv]{
2020-03-13 14:03:36 +00:00
if (== $step 0) {
fail 'bad value: step must be positive, but is 0'
2020-03-13 14:03:36 +00:00
}
2021-08-27 20:09:05 +00:00
var l = (-p $@argv)
var c = (count $l)
2020-03-13 14:03:36 +00:00
if (not $from) {
range &step=$step (- $c 1)
2020-03-13 14:03:36 +00:00
} else {
if (< $from 0) {
from = (+ $c $from)
}
if (> $step 0) {
if (< $from 0) {
from = 0
}
range &step=$step $from (- $c 1)
2020-03-13 14:03:36 +00:00
} else {
if (>= $from $c) {
from = (- $c 1)
}
range &step=$step $from 0
2020-03-13 14:03:36 +00:00
}
}
}
fn -for [&from=$false &step=1 @argv]{
2021-08-27 20:09:05 +00:00
var l = (-p $@argv)
indexes &from=$from &step=$step $l | each [i]{ put [ $i $l[$i] ] }
}
fn loop [&from=$false &step=1 &end=$false cb @argv]{
2020-03-13 14:03:36 +00:00
if (not $end) {
-for &from=$from &step=$step $@argv | each [e]{ $cb $@e }
2020-03-13 14:03:36 +00:00
} else {
-for &from=$from &step=$step $@argv | each [e]{
2021-08-27 20:09:05 +00:00
var i v = $@e
2020-03-13 14:03:36 +00:00
if ($end $i $v) {
break
}
$cb $i $v
}
}
}
fn ploop [&from=$false &step=1 cb @argv]{
-for &from=$from &step=$step $@argv | peach [e]{ $cb $@e }
2020-03-13 14:03:36 +00:00
}
fn reverse [@argv]{ loop &step=-1 [_ e]{ put $e } $@argv }
fn reach [cb @argv]{ reverse $@argv | each [e]{ cb $e } }
2020-03-13 14:03:36 +00:00
fn empty [@argv]{ == (count (-p $@argv)) 0 }
fn not-empty [@argv]{ not (empty $@argv) }
2020-03-13 14:03:36 +00:00
fn filter [cb @argv]{
2020-03-13 14:03:36 +00:00
each [v]{
if ($cb $v) {
put $v
}
} (-p $@argv)
2020-03-13 14:03:36 +00:00
}
fn filter-not [cb @argv]{ filter [v]{ not ($cb $v) } $@argv }
fn first [&from=$false &reverse=$false cb @argv]{
2021-08-27 20:09:05 +00:00
var f = [v]{
2020-03-13 14:03:36 +00:00
if ($cb $v) {
put $v
break
}
}
2021-08-27 20:09:05 +00:00
var l = (-p $@argv)
if (not $from) {
if $reverse {
reach $f $l
2020-03-13 14:03:36 +00:00
} else {
each $f $l
2020-03-13 14:03:36 +00:00
}
} else {
2021-08-27 20:09:05 +00:00
var c = (count $l)
if (< $from 0) {
from = (+ $c $from)
}
if $reverse {
2021-08-27 20:09:05 +00:00
var e = (+ $from 1)
if (> $e $c) {
e = $c
} elif (< $e 0) {
e = 0
}
reach $f $l[..$e]
2020-03-13 14:03:36 +00:00
} else {
2021-08-27 20:09:05 +00:00
var b = $from
if (> $b $c) {
b = $c
} elif (< $b 0) {
b = 0
}
each $f $l[$b..]
2020-03-13 14:03:36 +00:00
}
}
}
fn filter-index [cb @argv]{
loop [i v]{
2020-03-13 14:03:36 +00:00
if ($cb $v) {
put $i
}
} $@argv
2020-03-13 14:03:36 +00:00
}
fn filter-index-not [cb @argv]{ filter-index [v]{ not ($cb $v) } $@argv }
fn first-index [&from=$false &reverse=$false cb @argv]{
2021-08-27 20:09:05 +00:00
var idx = -1
var step = 1
if $reverse {
step = -1
}
loop &from=$from &step=$step [i v]{
2020-03-13 14:03:36 +00:00
if ($cb $v) {
idx = $i
break
}
} $@argv
2020-03-13 14:03:36 +00:00
put $idx
}
fn search [cb @argv]{
loop [i v]{
2020-03-13 14:03:36 +00:00
if ($cb $i $v) {
put $v
}
} $@argv
2020-03-13 14:03:36 +00:00
}
fn search-not [cb @argv]{ search [i v]{ not ($cb $i $v) } $@argv }
fn search-first [&from=$false &reverse=$false cb @argv]{
2021-08-27 20:09:05 +00:00
var step = 1
if $reverse {
step = -1
}
loop &from=$from &step=$step [i v]{
2020-03-13 14:03:36 +00:00
if ($cb $i $v) {
put $v
break
}
} $@argv
2020-03-13 14:03:36 +00:00
}
fn search-index [cb @argv]{
loop [i v]{
2020-03-13 14:03:36 +00:00
if ($cb $i $v) {
put $i
}
} $@argv
2020-03-13 14:03:36 +00:00
}
fn search-index-not [cb @argv]{ search-index [i v]{ not ($cb $i $v) } $@argv }
fn search-first-index [&from=$false &reverse=$false cb @argv]{
2021-08-27 20:09:05 +00:00
var idx = -1
var step = 1
if $reverse {
step = -1
}
loop &from=$from &step=$step [i v]{
2020-03-13 14:03:36 +00:00
if ($cb $i $v) {
idx = $i
break
}
} $@argv
2020-03-13 14:03:36 +00:00
put $idx
}
fn contains [cb @argv]{
2021-08-27 20:09:05 +00:00
var e = $false
2020-03-13 14:03:36 +00:00
each [v]{
if ($cb $v) {
e = $true
break
}
} (-p $@argv)
2020-03-13 14:03:36 +00:00
put $e
}
fn contains-not [cb @argv]{ contains [v]{ not ($cb $v) } $@argv }
fn exists [cb @argv]{
2021-08-27 20:09:05 +00:00
var e = $false
loop [i v]{
2020-03-13 14:03:36 +00:00
if ($cb $i $v) {
e = $true
break
}
} $@argv
2020-03-13 14:03:36 +00:00
put $e
}
fn exists-not [cb @argv]{ exists [i v]{ $cb $i $v } $@argv }
fn includes [v @argv]{ contains [e]{ is $v $e } $@argv }
fn includes-not [v @argv]{ contains-not [e]{ is $v $e } $@argv }
2020-03-13 14:03:36 +00:00
fn reduce [v cb @argv]{
each [e]{ v = ($cb $v $e) } (to-list $@argv)
put $v
}
fn remove-duplicate [@argv]{
2021-08-27 20:09:05 +00:00
var done = [&]
2020-03-13 14:03:36 +00:00
each [v]{
put [&v=$v &e=(has-key $done $v)]
done[$v] = $nil
} (-p $@argv) | each [v]{
2020-03-13 14:03:36 +00:00
assoc $v k (not $v[e])
} | each [v]{
if $v[k] {
put $v[v]
}
}
}
fn premove-duplicate [@argv]{
2021-08-27 20:09:05 +00:00
var done = [&]
2020-03-13 14:03:36 +00:00
peach [v]{
done[$v] = $nil
} (-p $@argv)
2020-03-13 14:03:36 +00:00
keys $done
}
fn swap [i j @argv]{
2021-08-27 20:09:05 +00:00
var l = (-p $@argv)
var c = (count $l)
2020-03-13 14:03:36 +00:00
i j = (-i $i $c) (-i $j $c)
if (or (== $i $c) (== $j $c)) {
2020-03-13 14:03:36 +00:00
fail 'Index out of range'
}
if (== $i $j) {
2020-07-22 09:24:43 +00:00
all $l
2020-03-13 14:03:36 +00:00
} else {
if (> $i $j) {
i j = $j $i
}
2020-03-13 14:03:36 +00:00
take $i $l
put $l[j]
all $l[(+ $i 1)..$j]
2020-03-13 14:03:36 +00:00
put $l[$i]
drop (+ $j 1) $l
}
}
2020-07-22 09:24:43 +00:00
fn less [cb]{
2021-08-27 20:09:05 +00:00
var l = [a b]{ < ($cb $a $b) 0 }
2020-07-22 09:24:43 +00:00
put $l
}
fn sort [cb @argv]{ order &less-than=(less $cb) (-p $@argv) }