Skip to content

Commit

Permalink
Update reference example
Browse files Browse the repository at this point in the history
  • Loading branch information
TharukaCkasthuri committed Jul 25, 2024
1 parent 3566228 commit 491ddae
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 37 deletions.
85 changes: 64 additions & 21 deletions examples/reference/abilities.jac
Original file line number Diff line number Diff line change
@@ -1,34 +1,77 @@
obj Divider {
can divide(x: float, y: float) -> float {
return (x / y);
}
}
#this is an abstract class as it has the abstract method

obj Calculator {
static can : priv multiply(a: float, b: float) -> float {
return a * b;
""" Add multiple numbers """
can add(x: float, *y: tuple) -> float abs;

""" Substract second value from the first value """
can subtract(x: float, y: float) -> float abs;

""" Multiply two numbers """
can multiply(x: float, y: float) -> float abs;

""" Divide first number by the second number """
can divide(x: float, y: float) -> float abs;

""" Check if a number is positive """
static can is_positive(number: float) -> bool{
return number > 0;
}
can substract -> float abs;
can add(number: float, *a: tuple) -> float;
}

obj substractor :Calculator: {
can substract(x: float, y: float) -> float {

obj BasicCalculator :Calculator: {
can add(x: float, y: tuple) -> float {
return (x + sum(y));
}

can subtract(x: float, y: float) -> float {
return (x - y);
}

can multiply(x: float, y: float) -> float {
return (x * y);
}

can divide(x: float, y: float) -> float {
return (x / y);
}
}

:obj:Calculator:can:add
(number: float, *a: tuple) -> float {
return (number * sum(a));
obj AdvancedCalculator:BasicCalculator: {
can power(x: float, y: float) -> float {
return (x ** y);
}

can square_root(x: float) -> float {
return (x ** 0.5);
}

can cube_root(x: float) -> float {
return (x ** (1/3));
}

can remainder(x: float, y: float) -> float {
return (x % y);
}

can absolute(x: float) -> float {
return (abs(x));
}
}

with entry {
div = Divider();
sub = substractor();
print(div.divide(55, 11));
print(Calculator.multiply(9, -2));
print(sub.add(5, 20, 34, 56));
print(sub.substract(9, -2));
cal = BasicCalculator();
adv_cal = AdvancedCalculator();
print(cal.add(5, (10,11)));
print(cal.subtract(5, 10));
print(cal.multiply(5, 10));
print(cal.divide(5, 10));
print(adv_cal.power(5, 10));
print(adv_cal.square_root(25));
print(adv_cal.cube_root(27));
print(adv_cal.remainder(10, 3));
print(adv_cal.absolute(-5));

print(Calculator.is_positive(10));
print(Calculator.is_positive(-5));
}
140 changes: 124 additions & 16 deletions examples/reference/abilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,139 @@


class Calculator(ABC):
@staticmethod
def multiply(a: float, b: float) -> float:
return a * b
"""
Abstract Base Class for basic calculator operations.
"""

@abstractmethod
def add(self, x: float, y: tuple) -> float:
"""
Add two numbers.
:param x: First number
:param y: Second number
:return: The sum of x and y
"""
pass

@abstractmethod
def substract(self, x: float, y: float) -> float:
def subtract(self, x: float, y: float) -> float:
"""
Subtract two numbers.
:param x: First number
:param y: Second number
:return: The difference of x and y
"""
pass

def add(self, number: float, *a: tuple) -> str:
return str(number * sum(a))
@abstractmethod
def multiply(self, x: float, y: float) -> float:
"""
Multiply two numbers.
:param x: First number
:param y: Second number
:return: The product of x and y
"""
pass

@abstractmethod
def divide(self, x: float, y: float) -> float:
"""
Divide two numbers.
:param x: First number
:param y: Second number
:return: The quotient of x and y
"""
pass

@staticmethod
def is_positive(number: float) -> bool:
"""
Check if a number is positive.
:param number: The number to check
:return: True if the number is positive, False otherwise
"""
return number > 0

class Substractor(Calculator):
def substract(self, x: float, y: float) -> float:

class BasicCalculator(Calculator):
"""
Basic calculator implementation.
"""

def add(self, x: float, y: tuple) -> float:
return x + sum(y)

def subtract(self, x: float, y: float) -> float:
return x - y

def multiply(self, x: float, y: float) -> float:
return x * y

class Divider:
def divide(self, x: float, y: float):
def divide(self, x: float, y: float) -> float:
return x / y


sub = Substractor()
div = Divider()
print(div.divide(55, 11))
print(Calculator.multiply(9, -2))
print(sub.add(5, 20, 34, 56))
print(sub.substract(9, -2))
class AdvanceCalculator(BasicCalculator):
"""
Advance calculator implementation.
"""

def power(self, x: float, y: float) -> float:
"""
Raise x to the power of y.
"""
return x**y

def square_root(self, x: float) -> float:
"""
Compute the square root of x.
"""
return x**0.5

def cube_root(self, x: float) -> float:
"""
Compute the cube root of x.
"""
return x ** (1 / 3)

def remainder(self, x: float, y: float) -> float:
"""
Compute the remainder of x divided by y.
"""
return x % y

def absolute(self, x: float) -> float:
"""
Compute the absolute value of x.
"""
return abs(x)


cal = BasicCalculator()
adv_cal = AdvanceCalculator()


# Perform operations using BasicCalculator
print("Basic Calculator Operations:")
print("Addition:", cal.add(5, (10, 11)))
print("Subtraction:", cal.subtract(5, 10))
print("Multiplication:", cal.multiply(5, 10))
print("Division:", cal.divide(5, 10))

# Perform operations using AdvanceCalculator
print("\nAdvance Calculator Operations:")
print("Power:", adv_cal.power(5, 10))
print("Square Root:", adv_cal.square_root(25))
print("Cube Root:", adv_cal.cube_root(27))
print("Remainder:", adv_cal.remainder(10, 3))
print("Absolute Value:", adv_cal.absolute(-5))

# Use the static method from the Calculator class
print("\nStatic Method Check:")
print("Is 10 positive?", Calculator.is_positive(10))
print("Is -5 positive?", Calculator.is_positive(-5))

0 comments on commit 491ddae

Please sign in to comment.