Nouvelle config (passage à elvish 0.16.1)

This commit is contained in:
Benjamin VAUDOUR 2021-08-24 20:44:01 +02:00
parent 0ebaf3b5a0
commit ff9cefd28f
69 changed files with 423 additions and 1018 deletions

Binary file not shown.

View file

@ -1,4 +1,4 @@
fn set [c t f]{
fn cset [c t f]{
if $c {
put $t
} else {
@ -7,11 +7,11 @@ fn set [c t f]{
}
fn mset [c t f]{
all (set $c $t $f)
all (cset $c $t $f)
}
fn call [c t f @argv]{
local:v = (set $c $t $f)
local:v = (cset $c $t $f)
if (is (kind-of $v) fn) {
$v $@argv
} else {

View file

@ -1,5 +1,9 @@
use str
use ./list
use ./map
search-type = [
&exact= [m]{ e:ls $m 2>/dev/null }
&exact= [m]{ ls $m 2>/dev/null }
&match= [m]{ put *$m* }
&prefix= [m]{ put $m* }
&suffix= [m]{ put *$m }
@ -9,7 +13,6 @@ search-type = [
]
fn -less [f1 f2]{
use str
local:fl1 local:fl2 = (str:to-lower $f1) (str:to-lower $f2)
local:c = (str:compare $fl1 $fl2)
if (== $c 0) {
@ -27,11 +30,9 @@ fn -get-results [sort result]{
}
fn -search [&sort=$false &type=exact @motive]{
use ./list
local:f = $search-type[$type]
local:result = [&]
if (list:empty $motive) {
use ./map
result = (put * | map:to-set)
} else {
peach [m]{
@ -50,11 +51,10 @@ fn -search [&sort=$false &type=exact @motive]{
fn match-files [&sort=$false &type=prefix @motive]{ -search &sort=$sort &type=$type $@motive }
fn match-extensions [&sort=$false &type=deep-prefix motive @extensions]{
use ./list
result = [&]
-search &type=$type $motive | peach [f]{
local:f = $f
if (list:contains [e]{ has-suffix $f .$e } $extensions) {
if (list:contains [e]{ str:has-suffix $f .$e } $extensions) {
result[$f] = $nil
}
}

View file

@ -40,14 +40,13 @@ fn -long-part6 [p]{
fn -middle-part6 [p]{
while (and (> (count $p) 1) (eq $p[0] 0)) {
p = $p[1:]
p = $p[1..]
}
put $p
}
fn -find-max0 [parts]{
use ./list
use ./condition
local:idx local:s = -1 1
local:ci local:cs local:f = -1 0 $false
list:loop [i p]{
@ -78,7 +77,7 @@ fn long6 [ip]{
if (> $c 0) {
local:i = (str:index $ip '::')
local:z = (repeat $c ':' | str:join '')
ip = (str:join '' [$ip[:$i] $z $ip[{$i}:]])
ip = (str:join '' [$ip[..$i] $z $ip[{$i}..]])
}
str:split ':' $ip | each $-long-part6~ | str:join ':'
}
@ -91,7 +90,7 @@ fn short6 [ip]{
local:parts = [(str:split ':' (middle6 $ip))]
local:i local:s = (-find-max0 $parts)
if (>= $i 0) {
local:left local:right = $parts[:$i] $parts[(+ $i $s):]
local:left local:right = $parts[..$i] $parts[(+ $i $s)..]
if (== (count $left) 0) {
left = ['']
}
@ -111,7 +110,7 @@ fn -cmp [e1 e2]{
local:c = 0
list:loop [i p1]{
local:p2 = $e2[$i]
c = (condition:set (< $p1 $p2) -1 (condition:set (> $p1 $p2) 1 0))
c = (condition:cset (< $p1 $p2) -1 (condition:cset (> $p1 $p2) 1 0))
if (!= $c 0) {
break
}
@ -149,6 +148,6 @@ fn cmp [ip1 ip2]{
put -1
}
} else {
fali 'Not an IP: '$ip1' → '$ip2
fail 'Not an IP: '$ip1' → '$ip2
}
}

View file

@ -1,230 +1,23 @@
use ./condition
fn -i [&reverse=false i c]{
if (and (== $c 0) (or (== $i 0) ($i -1))) {
put 0
return
}
if (< $i 0) {
i = (+ $c $i)
}
if (or (and (>= $i 0) (< $i $c)) (and (== $i -1) $reverse) (and (== $i $c) (not $reverse))) {
put $i
} else {
fail 'Index out of range'
}
}
fn -indexes [&from=$false &step=1 l]{
if (== $step 0) {
fail 'Step must be ≠ 0'
}
local:c local:r = (count $l) (< $step 0)
if (not $from) {
if $r {
range &step=$step[1:] $c | each [i]{ - $c $i 1 }
} else {
range &step=$step $c
}
} else {
from = (-i &reverse=$r $from $c)
if $r {
c = (+ $from 1)
range &step=$step[1:] $c | each [i]{ - $c $i 1 }
} else {
range &step=$step $from $c
}
}
}
fn -loop [&from=$false &step=1 &end=$false cb l]{
if (not $end) {
-indexes &from=$from &step=$step $l | each [i]{
put [$i $l[$i]]
} | each [e]{
$cb $@e
}
} else {
-indexes &from=$from &step=$step $l | each [i]{
put [$i $l[$i]]
} | each [e]{
local:i local:v = $@e
if ($end $i $v) {
break
}
$cb $i $v
}
}
}
fn -ploop [&from=$false &step=1 cb l]{
-indexes &from=$from &step=$step $l | peach [i]{
put [$i $l[$i]]
} | peach [e]{
$cb $@e
}
}
fn -reverse [l]{
-indexes &step=-1 $l | each [i]{ put $l[$i] }
}
fn -reach [cb l]{
-reverse $l | each [v]{ $cb $v }
}
fn -empty [l]{ eq (count $l) 0 }
fn -filter [cb l]{
each [v]{
if ($cb $v) {
put $v
}
} $l
}
fn -first [&from=$false &reverse=$false cb l]{
local:f = [v]{
if ($cb $v) {
put $v
break
}
}
if $reverse {
if $from {
local:end = (-i $from (count $l))
-reach $f $l[:(+ $end 1)]
} else {
-reach $f $l
}
} else {
if $from {
from = (-i $from (count $l))
drop $from $l | each $f
} else {
each $f $l
}
}
}
fn -filter-index [cb l]{
-loop [i v]{
if ($cb $v) {
put $i
}
} $l
}
fn -first-index [&from=$false &reverse=$false cb l]{
local:idx = -1
-loop &from=$from &step=(condition:set $reverse -1 1) [i v]{
if ($cb $v) {
idx = $i
break
}
} $l
put $idx
}
fn -search [cb l]{
-loop [i v]{
if ($cb $i $v) {
put $v
}
} $l
}
fn -search-first [&from=$false &reverse=$false cb l]{
-loop &from=$from &step=(condition:set $reverse -1 1) [i v]{
if ($cb $i $v) {
put $v
break
}
} $l
}
fn -search-index [cb l]{
-loop [i v]{
if ($cb $i $v) {
put $i
}
} $l
}
fn -search-first-index [&from=$false &reverse=$false cb l]{
local:idx = -1
-loop &from=$from &step=(condition:set $reverse -1 1) [i v]{
if ($cb $i $v) {
idx = $i
break
}
}
put $idx
}
fn -contains [cb l]{
local:e = $false
each [v]{
if ($cb $v) {
e = $true
break
}
} $l
put $e
}
fn -exists [cb l]{
local:e = $false
-loop [i v]{
if ($cb $i $v) {
e = $true
break
}
} $l
put $e
}
fn -remove-duplicate [l]{
local:done = [&]
each [v]{
put [&v=$v &e=(has-key $done $v)]
done[$v] = $nil
} $l | each [v]{
assoc $v k (not $v[e])
} | each [v]{
if $v[k] {
put $v[v]
}
}
}
fn -premove-duplicate [l]{
local:done= [&]
peach [v]{
local:v = $v
done[$v] = $nil
}
keys $done
}
fn -swap [i j l]{
local:c = (count $l)
i j = (-i $i $c) (-i $j $c)
if (or (eq $i $c) (eq $j $c)) {
fail 'Index out of range'
}
if (eq $i $j) {
all $l
} else {
i j = (condition:mset (< $i $j) [$i $j] [$j $i])
take $i $l
put $l[j]
all $l[(+ $i 1):$j]
put $l[$i]
drop (+ $j 1) $l
}
}
use builtin
use str
fn -p [@argv]{
local:c = (count $argv)
if (eq $c 0) {
put [(all)]
} elif (eq $c 1) {
if (== $c 0) {
put [ (all) ]
} elif (== $c 1) {
put $argv[0]
} else {
fail '0 or 1 argument needed'
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 {
local:d = (+ $begin $end)
builtin:range &step=(* $step -1) $end (+ $begin 1) | each [e]{ - $d $e }
}
}
@ -236,38 +29,214 @@ fn to-list [@argv]{
}
}
fn indexes [&from=$false &step=1 @argv]{ -indexes &from=$from &step=1 (-p $@argv) }
fn range [&step=1 b @r]{
if (== $step 0) {
fail 'bad value: step must be positive, but is 0'
}
local:c = (count $r)
if (> $c 1) {
fail 'usage: list:range &step=1 begin? end'
}
local:e = 0
if (== $c 0) {
if (> $step 0) {
b e = $e $b
}
} else {
e = $r[0]
}
-r $step $b $e
}
fn loop [&from=$false &step=1 &end=$false cb @argv]{ -loop &from=$from &step=$step &end=$end $cb (-p $@argv) }
fn rloop [&end=$false cb @argv]{ loop &step=-1 &end=$end $cb $@argv }
fn ploop [&from=$false &step=1 cb @argv]{ -ploop &from=$from &step=$step $cb (-p $@argv) }
fn indexes [&from=$false &step=1 @argv]{
if (== $step 0) {
fail 'bad value: step must be positive, but is 0'
}
local:l = (-p $@argv)
local:c = (count $l)
if (not $from) {
range &step=$step (- $c 1)
} else {
if (< $from 0) {
from = (+ $c $from)
}
if (> $step 0) {
if (< $from 0) {
from = 0
}
range &step=$step $from (- $c 1)
} else {
if (>= $from $c) {
from = (- $c 1)
}
range &step=$step $from 0
}
}
}
fn reverse [@argv]{ -reverse (-p $@argv) }
fn reach [cb @argv]{ -reach $cb (-p $@argv) }
fn -for [&from=$false &step=1 @argv]{
local:l = (-p $@argv)
indexes &from=$from &step=$step $l | each [i]{ put [ $i $l[$i] ] }
}
fn empty [@argv]{ -empty (-p $@argv) }
fn loop [&from=$false &step=1 &end=$false cb @argv]{
if (not $end) {
-for &from=$from &step=$step $@argv | each [e]{ $cb $@e }
} else {
-for &from=$from &step=$step $@argv | each [e]{
local:i local:v = $@e
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 }
}
fn reverse [@argv]{ loop &step=-1 [_ e]{ put $e } $@argv }
fn reach [cb @argv]{ reverse $@argv | each [e]{ cb $e } }
fn empty [@argv]{ == (count (-p $@argv)) 0 }
fn not-empty [@argv]{ not (empty $@argv) }
fn filter [cb @argv]{ -filter $cb (-p $@argv) }
fn filter [cb @argv]{
each [v]{
if ($cb $v) {
put $v
}
} (-p $@argv)
}
fn filter-not [cb @argv]{ filter [v]{ not ($cb $v) } $@argv }
fn first [&from=$false &reverse=$false cb @argv]{ -first &from=$from &reverse=$reverse $cb (-p $@argv) }
fn first [&from=$false &reverse=$false cb @argv]{
local:f = [v]{
if ($cb $v) {
put $v
break
}
}
local:l = (-p $@argv)
if (not $from) {
if $reverse {
reach $f $l
} else {
each $f $l
}
} else {
local:c = (count $l)
if (< $from 0) {
from = (+ $c $from)
}
if $reverse {
local:e = (+ $from 1)
if (> $e $c) {
e = $c
} elif (< $e 0) {
e = 0
}
reach $f $l[..$e]
} else {
local:b = $from
if (> $b $c) {
b = $c
} elif (< $b 0) {
b = 0
}
each $f $l[$b..]
}
}
}
fn filter-index [cb @argv]{ -filter-index $cb (-p $@argv) }
fn filter-index [cb @argv]{
loop [i v]{
if ($cb $v) {
put $i
}
} $@argv
}
fn filter-index-not [cb @argv]{ filter-index [v]{ not ($cb $v) } $@argv }
fn first-index [&from=$false &reverse=$false cb @argv]{ -first-index &from=$from &reverse=$reverse $cb (-p $@argv) }
fn first-index [&from=$false &reverse=$false cb @argv]{
local:idx = -1
local:step = 1
if $reverse {
step = -1
}
loop &from=$from &step=$step [i v]{
if ($cb $v) {
idx = $i
break
}
} $@argv
put $idx
}
fn search [cb @argv]{ -search $cb (-p $@argv) }
fn search [cb @argv]{
loop [i v]{
if ($cb $i $v) {
put $v
}
} $@argv
}
fn search-not [cb @argv]{ search [i v]{ not ($cb $i $v) } $@argv }
fn search-first [&from=$false &reverse=$false cb @argv]{ -search-first &from=$from &reverse=$reverse $cb (-p $@argv) }
fn search-first [&from=$false &reverse=$false cb @argv]{
local:step = 1
if $reverse {
step = -1
}
loop &from=$from &step=$step [i v]{
if ($cb $i $v) {
put $v
break
}
} $@argv
}
fn search-index [cb @argv]{ -search-index $cb (-p $@argv) }
fn search-index [cb @argv]{
loop [i v]{
if ($cb $i $v) {
put $i
}
} $@argv
}
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]{ -search-first-index &from=$from &reverse=$reverse $cb (-p $@argv) }
fn search-first-index [&from=$false &reverse=$false cb @argv]{
local:idx = -1
local:step = 1
if $reverse {
step = -1
}
loop &from=$from &step=$step [i v]{
if ($cb $i $v) {
idx = $i
break
}
} $@argv
put $idx
}
fn contains [cb @argv]{ -contains $cb (-p $@argv) }
fn contains [cb @argv]{
local:e = $false
each [v]{
if ($cb $v) {
e = $true
break
}
} (-p $@argv)
put $e
}
fn contains-not [cb @argv]{ contains [v]{ not ($cb $v) } $@argv }
fn exists [cb @argv]{ -exists $cb (-p $@argv) }
fn exists [cb @argv]{
local:e = $false
loop [i v]{
if ($cb $i $v) {
e = $true
break
}
} $@argv
put $e
}
fn exists-not [cb @argv]{ exists [i v]{ $cb $i $v } $@argv }
fn includes [v @argv]{ contains [e]{ is $v $e } $@argv }
@ -278,24 +247,46 @@ fn reduce [v cb @argv]{
put $v
}
fn remove-duplicate [@argv]{ -remove-duplicate (-p $@argv) }
fn premove-duplicate [@argv]{ -premove-duplicate (-p $@argv) }
fn remove-duplicate [@argv]{
local:done = [&]
each [v]{
put [&v=$v &e=(has-key $done $v)]
done[$v] = $nil
} (-p $@argv) | each [v]{
assoc $v k (not $v[e])
} | each [v]{
if $v[k] {
put $v[v]
}
}
}
fn premove-duplicate [@argv]{
local:done= [&]
peach [v]{
local:v = $v
done[$v] = $nil
} (-p $@argv)
keys $done
}
fn swap [i j @argv]{ -swap $i $j (-p $@argv) }
fn -s1 [cb l e]{
fn swap [i j @argv]{
local:l = (-p $@argv)
local:c = (count $l)
local:i = (first-index [v]{ $cb $e $v } $l)
if (eq $i 0) {
put $e
i j = (-i $i $c) (-i $j $c)
if (or (== $i $c) (== $j $c)) {
fail 'Index out of range'
}
if (== $i $j) {
all $l
} elif (< $i 0) {
all $l
put $e
} else {
all $l[:$i]
put $e
all $l[{$i}:]
if (> $i $j) {
i j = $j $i
}
take $i $l
put $l[j]
all $l[(+ $i 1)..$j]
put $l[$i]
drop (+ $j 1) $l
}
}
@ -303,9 +294,4 @@ fn less [cb]{
local:l = [a b]{ < ($cb $a $b) 0 }
put $l
}
fn -sort [cb l]{
order &less-than=(less $cb) $l
}
fn sort [cb @argv]{ -sort $cb (-p $@argv) }
fn sort [cb @argv]{ order &less-than=(less $cb) (-p $@argv) }

View file

@ -21,9 +21,11 @@ fn unzip [container]{
}
fn zip [lkeys lvalues]{
use ./condition
local:ck local:cv = (count $lkeys) (count $lvalues)
local:c = (condition:set (> $ck $cv) $cv $ck)
local:c = $ck
if (> $ck $cv) {
c = $cv
}
local:result = [&]
range $c | peach [i]{
put [&k=$lkeys[$i] &i=$i]

View file

@ -1,40 +0,0 @@
fn sign [n]{
if (> $n 0) {
put 1
} elif (< $n 0) {
put -1
} else {
put 0
}
}
fn negative [n]{ < $n 0 }
fn positive [n]{ >= $n 0 }
fn ++ [n]{ to-string (+ $n 1) }
fn -- [n]{ to-string (+ $n 1) }
fn neg [n]{ to-string (* $n -1) }
fn abs [n]{
use ./condition
condition:call (negative $n) $neg~ $put~ $n
}
fn sum [@numbers]{
use ./list
to-string (list:reduce 0 $+~ $numbers)
}
fn -minmax [t numbers]{
if (== (count $numbers) 0) {
return
}
use ./list
use ./condition
f = [c v]{ condition:set ($t $v $c) $v $c }
list:reduce $numbers[0] $f $numbers[1:]
}
fn min [@numbers]{ -minmax $<~ $numbers }
fn max [@numbers]{ -minmax $>~ $numbers }