From 5ad9c72b7fb720492e1e0311f6a5147311aab97c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20V=C3=A9rit=C3=A9?= Date: Wed, 7 Aug 2024 16:43:59 +0200 Subject: [PATCH 1/2] Adds builtin functions Tree/reverse and Tree/to_list --- src/fun/builtins.bend | 33 ++++++++++++++++--- tests/golden_tests/run_file/tree_to_list.bend | 4 +++ .../run_file__tree_to_list.bend.snap | 9 +++++ 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 tests/golden_tests/run_file/tree_to_list.bend create mode 100644 tests/snapshots/run_file__tree_to_list.bend.snap diff --git a/src/fun/builtins.bend b/src/fun/builtins.bend index 89c04fd15..d426b5774 100644 --- a/src/fun/builtins.bend +++ b/src/fun/builtins.bend @@ -76,10 +76,20 @@ String/split s delim = (String/split.go s delim (List/Cons String/Nil List/Nil)) # Create a new difference list DiffList/new = λx x +# DiffList/wrap(head: T) -> (List(T) -> List(T) +# Creates a new difference list with just the given value. +def DiffList/wrap(head): + return lambda tail: List/Cons(head, tail) + # DiffList/append(diff: List(T) -> List(T), val: T) -> (List(T) -> List(T)) # Append a value to the end of the difference list DiffList/append diff val = λx (diff (List/Cons val x)) +# DiffList/append(left: List(T) -> List(T), right: List(T) -> List(T)) -> (List(T) -> List(T)) +# Concatenates two difference lists. +def DiffList/concat(left, right): + return lambda x: left(right(x)) + # DiffList/cons(diff: List(T) -> List(T), val: T) -> (List(T) -> List(T)) # Append a value to the beginning of the difference list DiffList/cons diff val = λx (List/Cons val (diff x)) @@ -96,7 +106,22 @@ type Tree: Node { ~left, ~right } Leaf { value } - +# Returns a List converted from a Tree. +def Tree/to_list(tree): + fold tree: + case Tree/Leaf: + list = DiffList/wrap(tree.value) + case Tree/Node: + list = DiffList/concat(tree.left, tree.right) + return DiffList/to_list(list) + +# Reverses a tree swapping right and left leaves. +def Tree/reverse(tree): + fold tree: + case Tree/Leaf: + return !tree.value + case Tree/Node: + return ![tree.right, tree.left] # MAP Impl type Map = (Node value ~left ~right) | (Leaf) @@ -217,11 +242,11 @@ IO/FS/STDOUT = 1 IO/FS/STDERR = 2 ### Seek modes -# Seek from start of file +# Seek from start of file. IO/FS/SEEK_SET = +0 -# Seek from current position +# Seek from current position. IO/FS/SEEK_CUR = +1 -# Seek from end of file +# Seek from end of file. IO/FS/SEEK_END = +2 ### File utilities diff --git a/tests/golden_tests/run_file/tree_to_list.bend b/tests/golden_tests/run_file/tree_to_list.bend new file mode 100644 index 000000000..afcd27eec --- /dev/null +++ b/tests/golden_tests/run_file/tree_to_list.bend @@ -0,0 +1,4 @@ +def main: + var = Tree/reverse(![![!1, !2],![!3, !4]]) + return Tree/to_list(var) + \ No newline at end of file diff --git a/tests/snapshots/run_file__tree_to_list.bend.snap b/tests/snapshots/run_file__tree_to_list.bend.snap new file mode 100644 index 000000000..9da109da0 --- /dev/null +++ b/tests/snapshots/run_file__tree_to_list.bend.snap @@ -0,0 +1,9 @@ +--- +source: tests/golden_tests.rs +input_file: tests/golden_tests/run_file/tree_to_list.bend +--- +NumScott: +[4, 3, 2, 1] + +Scott: +[4, 3, 2, 1] From 63ba5d22755e85f715651be18d2a8fd0f66035e2 Mon Sep 17 00:00:00 2001 From: Nicolas Abril Date: Wed, 7 Aug 2024 17:24:30 +0200 Subject: [PATCH 2/2] Fix spacing in builtins file --- src/fun/builtins.bend | 1 + 1 file changed, 1 insertion(+) diff --git a/src/fun/builtins.bend b/src/fun/builtins.bend index d426b5774..2a5f90cc1 100644 --- a/src/fun/builtins.bend +++ b/src/fun/builtins.bend @@ -122,6 +122,7 @@ def Tree/reverse(tree): return !tree.value case Tree/Node: return ![tree.right, tree.left] + # MAP Impl type Map = (Node value ~left ~right) | (Leaf)