diff options
author | Matthew McMillan <matt@matthewmcmillan.me> | 2021-08-30 00:24:24 -0400 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-09-01 20:10:33 +0100 |
commit | 9f064a8497872b976c9ed089a21b47a3d92295ab (patch) | |
tree | 110373b761f41b00e05eb23173952b98248c65c1 /src/gleam_stdlib.js | |
parent | 0dc4a0aed4cbe8b9a2b3a575f112bbd30f0de39d (diff) | |
download | gleam_stdlib-9f064a8497872b976c9ed089a21b47a3d92295ab.tar.gz gleam_stdlib-9f064a8497872b976c9ed089a21b47a3d92295ab.zip |
First attempt at map implementation
Diffstat (limited to 'src/gleam_stdlib.js')
-rw-r--r-- | src/gleam_stdlib.js | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index 652b7b5..0cb2417 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -258,3 +258,67 @@ export function regex_scan(regex, string) { }); return List.fromArray(matches); } + +export function new_map() { + return new Map(); +} + +export function map_size(map) { + return map.size; +} + +export function map_to_list(map) { + const result = []; + map.forEach(([a, b]) => result.push([JSON.parse(a), b])) +} + +export function map_from_list(list) { + return new Map(list.map(([a, b]) => [JSON.stringify(a), b])); +} + +export function map_has_key(k, map) { + return map.contains(JSON.stringify(k)); +} + +export function map_remove(k, map) { + new Map(map).remove(JSON.stringify(k)); +} + +export function map_filter(f, map) { + const result = new Map(); + map.entries.forEach(([a, b]) => { + if (f(JSON.parse(a))) { + result.set(a, b); + } + }) +} + +export function map_get(from, get) { + from.get(JSON.stringify(get)); +} + +export function map_insert(key, value, map) { + return new Map(map).set(JSON.stringify(key), value); +} + +export function map_keys(map) { + return [...map.keys()].map(key => JSON.parse(key)); +} + +export function map_values(map) { + return [...map.values()]; +} + +export function map_map_values(fn, map) { + const result = new Map(); + map.forEach(([a, b]) => result.set(JSON.stringify(fn(JSON.parse(a))), b)); +} + +export function map_merge(into, merge) { + return new Map(...into, ...merge); +} + +export function map_take(keys, map) { + const result = new Map(); + keys.forEach(key => result.set(JSON.stringify(key), map.get(JSON.stringify(key)))); +}
\ No newline at end of file |