Skip to content

Commit

Permalink
Switch get_or_else implementation with get to make it more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Sep 21, 2022
1 parent 3044ba6 commit 96b65d2
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Data/Map.enso
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,7 @@ type Map
example_get = Examples.map.get 1
get : Any -> Any ! No_Value_For_Key_Error
get self key =
go map = case map of
Tip -> Error.throw (No_Value_For_Key_Error_Data key)
Bin _ k v l r ->
if k == key then v else
if k > key then @Tail_Call go l else @Tail_Call go r
result = go self
result
self.get_or_else key (Error.throw (No_Value_For_Key_Error_Data key))

## Gets the value associated with `key` in this map, or returns `other` if
it isn't present.
Expand All @@ -217,7 +211,13 @@ type Map
example_get_or_else = Examples.map.get_or_else 2 "zero"
get_or_else : Any -> Any -> Any
get_or_else self key ~other =
self.get key . catch No_Value_For_Key_Error_Data (_ -> other)
go map = case map of
Tip -> other
Bin _ k v l r ->
if k == key then v else
if k > key then @Tail_Call go l else @Tail_Call go r
result = go self
result

## Transforms the map's keys and values to create a new map.

Expand Down

0 comments on commit 96b65d2

Please sign in to comment.