elvish_config/lib/moi/util/map.elv

95 lines
2.0 KiB
Plaintext
Raw Normal View History

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