elvish_config/lib/moi/util/map.elv

95 lines
1.9 KiB
Plaintext
Raw Normal View History

2020-03-13 14:03:36 +00:00
fn empty [container]{ == (count $container) 0 }
fn values [container]{ keys $container | each [v]{ put $container[$v] } }
fn pvalues [container]{ keys $container | peach [v]{ put $container[$v] } }
fn value-of [container k &default=$nil]{
if (has-key $container $k) {
put $container[$k]
} else {
put $default
}
}
fn unzip [container]{
2021-08-27 20:06:49 +00:00
var lkeys lvalues = [] []
2020-03-13 14:03:36 +00:00
keys $container | each [k]{
2021-08-27 20:06:49 +00:00
lkeys = [(all $lkeys) $k]
2020-07-22 09:24:43 +00:00
lvalues = [(all $lvalues) $container[$k]]
2020-03-13 14:03:36 +00:00
}
put $lkeys $lvalues
}
fn zip [lkeys lvalues]{
2021-08-27 20:06:49 +00:00
var ck cv = (count $lkeys) (count $lvalues)
var c = $ck
if (> $ck $cv) {
c = $cv
}
2021-08-27 20:06:49 +00:00
var result = [&]
2020-03-13 14:03:36 +00:00
range $c | peach [i]{
put [&k=$lkeys[$i] &i=$i]
} | peach [e]{
assoc $e v $lvalues[$e[i]]
} | each [e]{
result[$e[k]] = $e[v]
}
put $result
}
fn to-map [@argv]{
use ./list
2021-08-27 20:06:49 +00:00
var container = (list:to-list $@argv)
2020-03-13 14:03:36 +00:00
zip [(range (count $container))] $container
}
fn to-set [@argv]{
use ./list
2021-08-27 20:06:49 +00:00
var m = [&]
2020-03-13 14:03:36 +00:00
each [k]{ m[$k] = $nil } (list:to-list $@argv)
put $m
}
fn mdissoc [container @argv]{
each [k]{
container = (dissoc $container $k)
} $argv
put $container
}
fn massoc [&replace=$true container @argv]{
each [e]{
2021-08-27 20:06:49 +00:00
var k v = $@e
2020-03-13 14:03:36 +00:00
if (or $replace (not (has-key $container $k))) {
container[$k] = $v
}
} $argv
put $container
}
fn add [container key @values]{
use ./list
2021-08-27 20:06:49 +00:00
var values = (list:to-list $values)
2020-03-13 14:03:36 +00:00
if (has-key $container $key) {
2020-07-22 09:24:43 +00:00
container[$key] = [(all $container[$key]) $@values]
2020-03-13 14:03:36 +00:00
} else {
container[$key] = $values
}
put $container
}
2020-07-22 09:24:43 +00:00
fn join [container container2]{
keys $container2 | each [k]{
container[$k] = $container2[$k]
}
put $container
}
fn sorted-keys [&reverse=$false &less-than=$nil container]{
keys $container | order &reverse=$reverse &less-than=$less-than
}
fn sorted-values [&reverse=$false &less-than=$nil container]{
values $container | order &reverse=$reverse &less-than=$less-than
}