From c2797c81335f4911fed57db3be06f8085c27836e Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Wed, 23 Aug 2023 00:13:21 +0200 Subject: [PATCH 1/2] Removed all "CoconutSyntaxWarning"s that appeared during compilation --- contents/barnsley/code/coconut/barnsley.coco | 2 +- .../euclidean_algorithm/code/coconut/euclidean.coco | 12 ++++++------ contents/flood_fill/code/coconut/flood_fill.coco | 12 ++++++------ contents/huffman_encoding/code/coconut/huffman.coco | 6 +++--- contents/jarvis_march/code/coconut/jarvis_march.coco | 2 +- .../code/coconut/monte_carlo.coco | 2 +- .../tree_traversal/code/coconut/tree_traversal.coco | 12 ++++++------ 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/contents/barnsley/code/coconut/barnsley.coco b/contents/barnsley/code/coconut/barnsley.coco index a27fc7c5c..1fd87c4d7 100644 --- a/contents/barnsley/code/coconut/barnsley.coco +++ b/contents/barnsley/code/coconut/barnsley.coco @@ -8,7 +8,7 @@ data Point(x=0, y=0): return Point(x, y) -def chaos_game(initial_location is Point, hutchinson_op, probabilities): +def chaos_game(Point(initial_location), hutchinson_op, probabilities): point = initial_location while True: yield (point := choices(hutchinson_op, probabilities) @ point) diff --git a/contents/euclidean_algorithm/code/coconut/euclidean.coco b/contents/euclidean_algorithm/code/coconut/euclidean.coco index 08cb851a1..17c592cf0 100644 --- a/contents/euclidean_algorithm/code/coconut/euclidean.coco +++ b/contents/euclidean_algorithm/code/coconut/euclidean.coco @@ -1,7 +1,7 @@ -def euclid_sub(a is int, 0) = a -addpattern def euclid_sub(0, b is int) = b +def euclid_sub(int(a), 0) = a +addpattern def euclid_sub(0, int(b)) = b -addpattern def euclid_sub(a is int, b is int): +addpattern def euclid_sub(int(a), int(b)): if a < b: return euclid_sub(a, b - a) elif b < a: @@ -9,10 +9,10 @@ addpattern def euclid_sub(a is int, b is int): return a -def euclid_mod(a is int, 0) = a -addpattern def euclid_mod(0, b is int) = b +def euclid_mod(int(a), 0) = a +addpattern def euclid_mod(0, int(b)) = b -addpattern def euclid_mod(a is int, b is int) = euclid_mod(b, a % b) +addpattern def euclid_mod(int(a), int(b)) = euclid_mod(b, a % b) if __name__ == '__main__': print('[#]\nModulus-based euclidean algorithm result:') diff --git a/contents/flood_fill/code/coconut/flood_fill.coco b/contents/flood_fill/code/coconut/flood_fill.coco index f2580a608..861d4d04b 100644 --- a/contents/flood_fill/code/coconut/flood_fill.coco +++ b/contents/flood_fill/code/coconut/flood_fill.coco @@ -3,16 +3,16 @@ import numpy as np data Point(x, y): - def __add__(self, other is Point) = Point(self.x + other.x, self.y + other.y) + def __add__(self, Point(other)) = Point(self.x + other.x, self.y + other.y) # This function is necessary, because negative indices wrap around the # array in Coconut. -def inbounds(canvas_shape, location is Point) = +def inbounds(canvas_shape, Point(location)) = min(location) >= 0 and location.x < canvas_shape[0] and location.y < canvas_shape[1] -def find_neighbours(canvas, location is Point, old_value): +def find_neighbours(canvas, Point(location), old_value): possible_neighbours = ((Point(0, 1), Point(1, 0), Point(0, -1), Point(-1, 0)) |> map$(location.__add__)) @@ -20,7 +20,7 @@ def find_neighbours(canvas, location is Point, old_value): and canvas[x] == old_value)) -def stack_fill(canvas, location is Point, old_value, new_value): +def stack_fill(canvas, Point(location), old_value, new_value): if new_value == old_value or not inbounds(canvas.shape, location): return @@ -33,7 +33,7 @@ def stack_fill(canvas, location is Point, old_value, new_value): stack.extend(find_neighbours(canvas, current_location, old_value)) -def queue_fill(canvas, location is Point, old_value, new_value): +def queue_fill(canvas, Point(location), old_value, new_value): if new_value == old_value or not inbounds(canvas.shape, location): return @@ -49,7 +49,7 @@ def queue_fill(canvas, location is Point, old_value, new_value): queue.append(neighbour) -def recursive_fill(canvas, location is Point, old_value, new_value): +def recursive_fill(canvas, Point(location), old_value, new_value): if new_value == old_value or not inbounds(canvas.shape, location): return diff --git a/contents/huffman_encoding/code/coconut/huffman.coco b/contents/huffman_encoding/code/coconut/huffman.coco index 640112167..7d1b0a936 100644 --- a/contents/huffman_encoding/code/coconut/huffman.coco +++ b/contents/huffman_encoding/code/coconut/huffman.coco @@ -4,13 +4,13 @@ from bisect import bisect class Tree data Empty() from Tree -data Leaf(char, n is int) from Tree: +data Leaf(char, int(n)) from Tree: def __str__(self): return f'Leaf({self.char}, {self.n})' __repr__ = __str__ -data Node(left is Tree, right is Tree) from Tree: +data Node(Tree(left), Tree(right)) from Tree: def __str__(self): return f'Node({str(self.left)}, {str(self.right)})' __repr__ = __str__ @@ -52,7 +52,7 @@ def build_huffman_tree(message): def build_codebook(Empty(), code='') = [] addpattern def build_codebook(Leaf(char, n), code='') = [(char, code)] -addpattern def build_codebook(Node(left, right), code='') = +addpattern def build_codebook(Node(left, right), code='') = build_codebook(left, code+'0') + build_codebook(right, code+'1') def huffman_encode(codebook, message): diff --git a/contents/jarvis_march/code/coconut/jarvis_march.coco b/contents/jarvis_march/code/coconut/jarvis_march.coco index f54aa4336..7ec55b371 100644 --- a/contents/jarvis_march/code/coconut/jarvis_march.coco +++ b/contents/jarvis_march/code/coconut/jarvis_march.coco @@ -3,7 +3,7 @@ data point(x=0, y=0): return f'({self.x}, {self.y})' # Is the turn counter-clockwise? -def counter_clockwise(p1 is point, p2 is point, p3 is point) = +def counter_clockwise(point(p1), point(p2), point(p3)) = (p3.y - p1.y) * (p2.x - p1.x) >= (p2.y - p1.y) * (p3.x - p1.x) diff --git a/contents/monte_carlo_integration/code/coconut/monte_carlo.coco b/contents/monte_carlo_integration/code/coconut/monte_carlo.coco index a1a1d9975..7d58c077c 100644 --- a/contents/monte_carlo_integration/code/coconut/monte_carlo.coco +++ b/contents/monte_carlo_integration/code/coconut/monte_carlo.coco @@ -4,7 +4,7 @@ import random data point(x, y): def __abs__(self) = (self.x, self.y) |> map$(pow$(?, 2)) |> sum |> math.sqrt -def in_circle(p is point, radius = 1): +def in_circle(point(p), radius = 1): """Return True if the point is in the circle and False otherwise.""" return abs(p) < radius diff --git a/contents/tree_traversal/code/coconut/tree_traversal.coco b/contents/tree_traversal/code/coconut/tree_traversal.coco index 776708f6e..1dd1f3798 100644 --- a/contents/tree_traversal/code/coconut/tree_traversal.coco +++ b/contents/tree_traversal/code/coconut/tree_traversal.coco @@ -16,20 +16,20 @@ def dfs_recursive_postorder(Node(value, children)): def dfs_recursive_inorder_btree(Node(value, children)): """A depth first search approach for printing all values in a binary tree.""" - case len(children): - match 2: + match len(children): + case 2: dfs_recursive_inorder_btree(children[0]) print(value, end=' ') dfs_recursive_inorder_btree(children[1]) - match 1: + case 1: dfs_recursive_inorder_btree(children[0]) print(value, end=' ') - match 0: + case 0: print(value, end=' ') else: print('Invalid binary tree') -def dfs_stack(node is Node): +def dfs_stack(Node(node)): """A depth first approach for printing out all values in a tree using a stack.""" stack = [node] while stack: @@ -38,7 +38,7 @@ def dfs_stack(node is Node): for child in current_node.children: stack.append(child) -def bfs_queue(node is Node): +def bfs_queue(Nonde(node)): """A breadth first search approach for printing out all values in a tree.""" queue = deque([node]) while queue: From 57b6ce7523a76d5bc1b85cbd3300157d6d158a00 Mon Sep 17 00:00:00 2001 From: Sammy Plat Date: Wed, 23 Aug 2023 16:26:44 +0200 Subject: [PATCH 2/2] Fix typo --- contents/tree_traversal/code/coconut/tree_traversal.coco | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/tree_traversal/code/coconut/tree_traversal.coco b/contents/tree_traversal/code/coconut/tree_traversal.coco index 1dd1f3798..a5ba371b6 100644 --- a/contents/tree_traversal/code/coconut/tree_traversal.coco +++ b/contents/tree_traversal/code/coconut/tree_traversal.coco @@ -38,7 +38,7 @@ def dfs_stack(Node(node)): for child in current_node.children: stack.append(child) -def bfs_queue(Nonde(node)): +def bfs_queue(Node(node)): """A breadth first search approach for printing out all values in a tree.""" queue = deque([node]) while queue: