Skip to content

Commit

Permalink
Changes made on 09-12-2024, includes fixing requests from HigherOrder…
Browse files Browse the repository at this point in the history
  • Loading branch information
In-Veritas committed Dec 9, 2024
1 parent 8f67636 commit 6354a5e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/fun/builtins.bend
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def Tree/reverse(tree: Tree(T)) -> Tree(T):
return !tree.value
case Tree/Node:
return ![tree.right, tree.left]

# MAYBE Impl

type Maybe(T):
Expand Down Expand Up @@ -210,17 +210,19 @@ def Map/get (map: Map(T), key: u24) -> (T, Map(T)):
return (unreachable(), map)

# Checks if a node has a value on a given key, returning Maybe/Some if it does, Maybe/None otherwise
def Map/get_check (map: Map(T), key: u24) -> Maybe(T):
def Map/get_check (map: Map(T), key: u24) -> (Map(T),Maybe(T)):
match map:
case Map/Leaf:
return Maybe/None
return (map, Maybe/None)
case Map/Node:
if (0 == key):
return map.value
elif ((key % 2) == 0):
return Map/get_check(map.left, (key / 2))
return (map, map.value)
elif (key % 2 == 0):
(rest, got) = Map/get(map.left, (key / 2))
return (Map/Node(map.value, got, map.right), map.value)
else:
return Map/get_check(map.right, (key / 2))
(rest, got) = Map/get(map.right, (key / 2))
return (Map/Node(map.value, map.left, got), map.value)

# Sets a value on a Map
def Map/set (map: Map(T), key: u24, value: T) -> Map(T):
Expand Down Expand Up @@ -253,15 +255,17 @@ def Map/contains (map: Map(T), key: u24) -> u24:
case Maybe/None:
return 0
elif ((key % 2) == 0):
return Map/contains(map.left, (key / 2))
(value, got) = Map/get(map.left, (key / 2))
return Map/contains(got, (key / 2))
else:
return Map/contains(map.right, (key / 2))
(value, got) = Map/get(map.right, (key / 2))
return Map/contains(got, (key / 2))

# Applies a funtion to a value on a Map
def Map/map (map: Map(T), key: u24, f: T -> T) -> Map(T):
match map:
case Map/Leaf:
return unreachable()
return Map/Leaf
case Map/Node:
if (0 == key):
return Map/Node(Maybe/Some(f(Maybe/unwrap(map.value))), map.left, map.right)
Expand Down
3 changes: 2 additions & 1 deletion tests/golden_tests/prelude/map_tests.bend
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def test2(m: Map(T), xs: List(u24), ys: List(T)) -> Map(T):

# Sets a value if the given node is empty, otherwise returns the map
def test3(m: Map(T), x: u24, v: T) -> Map(T):
match Map/get_check(m, x):
(map, val) = Map/get_check(m, x)
match val:
case Maybe/Some:
return m
case Maybe/None:
Expand Down

0 comments on commit 6354a5e

Please sign in to comment.