Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed all "CoconutSyntaxWarning"s that appeared during compilation #1014

Merged
merged 3 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contents/barnsley/code/coconut/barnsley.coco
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions contents/euclidean_algorithm/code/coconut/euclidean.coco
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
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:
return euclid_sub(a - b, b)
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:')
Expand Down
12 changes: 6 additions & 6 deletions contents/flood_fill/code/coconut/flood_fill.coco
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ 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__))

yield from possible_neighbours |> filter$(x -> (inbounds(canvas.shape, x)
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

Expand All @@ -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

Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions contents/huffman_encoding/code/coconut/huffman.coco
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion contents/jarvis_march/code/coconut/jarvis_march.coco
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions contents/tree_traversal/code/coconut/tree_traversal.coco
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(Node(node)):
"""A breadth first search approach for printing out all values in a tree."""
queue = deque([node])
while queue:
Expand Down
Loading