diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-07-13 14:02:59 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-07-13 14:02:59 +0100 |
commit | b9200d18f54a6f7aa3bf2fbda2782ab84a2289f9 (patch) | |
tree | ccbcb2d12c2e0bd806fb11afcf0a808d132ea7db | |
parent | 87fdb8cceea00a20245d1ce23ef9ca5be263c132 (diff) | |
download | gleam_stdlib-b9200d18f54a6f7aa3bf2fbda2782ab84a2289f9.tar.gz gleam_stdlib-b9200d18f54a6f7aa3bf2fbda2782ab84a2289f9.zip |
map.update uses option
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | src/gleam/iterator.gleam | 13 | ||||
-rw-r--r-- | src/gleam/map.gleam | 4 | ||||
-rw-r--r-- | test/gleam/map_test.gleam | 5 |
4 files changed, 17 insertions, 9 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 39cf839..3e7306f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- The `map.update` function now uses `Option` rather than `Result`. + ## v0.16.0 - 2021-06-17 - The `list` module gains the `interleave`, `flat_map` and `transpose` functions. diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam index ff2e020..25cc441 100644 --- a/src/gleam/iterator.gleam +++ b/src/gleam/iterator.gleam @@ -1,6 +1,7 @@ if erlang { import gleam/list import gleam/map.{Map} + import gleam/option.{None, Option, Some} // Internal private representation of an Iterator type Action(element) { @@ -712,7 +713,7 @@ if erlang { type SizedChunk(element) { Another(List(element), fn() -> Action(element)) Last(List(element)) - None + NoMore } fn next_sized_chunk( @@ -723,7 +724,7 @@ if erlang { case continuation() { Stop -> case current_chunk { - [] -> None + [] -> NoMore remaining -> Last(list.reverse(remaining)) } Continue(e, next) -> { @@ -742,7 +743,7 @@ if erlang { ) -> fn() -> Action(List(element)) { fn() { case next_sized_chunk(continuation, count, []) { - None -> Stop + NoMore -> Stop Last(chunk) -> Continue(chunk, stop) Another(chunk, next_element) -> Continue(chunk, do_sized_chunk(next_element, count)) @@ -888,11 +889,11 @@ if erlang { fn update_group_with( el: element, - ) -> fn(Result(List(element), Nil)) -> List(element) { + ) -> fn(Option(List(element))) -> List(element) { fn(maybe_group) { case maybe_group { - Ok(group) -> [el, ..group] - Error(Nil) -> [el] + Some(group) -> [el, ..group] + None -> [el] } } } diff --git a/src/gleam/map.gleam b/src/gleam/map.gleam index ff8281d..7fd363f 100644 --- a/src/gleam/map.gleam +++ b/src/gleam/map.gleam @@ -1,5 +1,6 @@ if erlang { import gleam/result + import gleam/option.{Option} import gleam/list /// A dictionary of keys and values. @@ -279,10 +280,11 @@ if erlang { pub fn update( in map: Map(k, v), update key: k, - with fun: fn(Result(v, Nil)) -> v, + with fun: fn(Option(v)) -> v, ) -> Map(k, v) { map |> get(key) + |> option.from_result |> fun |> insert(map, key, _) } diff --git a/test/gleam/map_test.gleam b/test/gleam/map_test.gleam index 6f32c8f..d09a00a 100644 --- a/test/gleam/map_test.gleam +++ b/test/gleam/map_test.gleam @@ -2,6 +2,7 @@ if erlang { import gleam/string import gleam/should import gleam/map + import gleam/option.{None, Some} pub fn from_list_test() { [#(4, 0), #(1, 0)] @@ -131,8 +132,8 @@ if erlang { let inc_or_zero = fn(x) { case x { - Ok(i) -> i + 1 - Error(_) -> 0 + Some(i) -> i + 1 + None -> 0 } } |