Skip to content

Commit

Permalink
new code and modified old
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryptonean committed Sep 16, 2024
1 parent 531ac45 commit fdb9a85
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 110 deletions.
46 changes: 22 additions & 24 deletions validators/activity_1.ak
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
// - Activity 001 - Create the substraction (-) function and test it

fn sub (x:Int , y:Int) -> Int{
x-y
fn sub(x: Int, y: Int) -> Int {
x - y
}

test sub_test(){
sub(5,2) ==3
test sub_test() {
sub(5, 2) == 3
}

fn sum(x,y) -> Int {
x+y
fn sum(x, y) -> Int {
x + y
}


//- Activity 002 - Create the multiplication (*) function and test it

fn mul(x:Int, y :Int) -> Int{
x*y
fn mul(x: Int, y: Int) -> Int {
x * y
}

test mul_test(){
mul(5,2) == 10
test mul_test() {
mul(5, 2) == 10
}

// - Activity 003 - Create the division (/) function and test it


fn divd(x,y) {
x/y
fn divd(x, y) {
x / y
}

test divd_test(){
divd(10,5) == 2
test divd_test() {
divd(10, 5) == 2
}

//- Activity 004 - Modify the main function that uses the sum function and the new functions created in the previous practices and test it

fn main() {
let result = sum (2,3)
let result = sub(result,1)
let result = mul(result,2)
let result = divd (result,2)
result
let result = sum(2, 3)
let result = sub(result, 1)
let result = mul(result, 2)
let result = divd(result, 2)
result
}

test main_test(){
main()==4
}
test main_test() {
main() == 4
}
111 changes: 58 additions & 53 deletions validators/aiken_language_tour.ak
Original file line number Diff line number Diff line change
@@ -1,100 +1,106 @@

// Functions are private by default. They can be exported with the 'pub' keyword.
pub fn add (x:Int,y:Int)->Int {
x+y
pub fn add(x: Int, y: Int) -> Int {
x + y
}

// using |> , used to pass output of 1 fucntion as input to another funtion

fn double (x:Int)->Int{
x*2
fn double(x: Int) -> Int {
x * 2
}

// code explanation
// Initially, x = 10.
// x |> f is double(10) → 20.
// 20 |> f is double(20) → 40.
// here f:fn(x)->x is double function we are passing
fn add_twice(x,f:fn(x)->x){
x
fn add_twice(x, f: fn(x) -> x) {
x
|> f
|> f
}

test add_twice_test(){
add_twice(10,double) == 40
test add_twice_test() {
add_twice(10, double) == 40
}



// code explanation
// Initially, x = 2.
// x |> f is double(4) → 4.
// 4 |> f is double(16) → 16.
// here f:fn(x)->x is mul function we are passing
fn multiply(x, f: fn(x)->x){
x
fn multiply(x, f: fn(x) -> x) {
x
|> f
|> f
}

fn mul(x:Int)->Int{
x*x
fn mul(x: Int) -> Int {
x * x
}

test multiply_test(){
multiply(2,mul) == 16
test multiply_test() {
multiply(2, mul) == 16
}



//intermidate question on |>

fn add1(x:Int)->Int{
x+1
fn add1(x: Int) -> Int {
x + 1
}

fn sub(x:Int)->Int{
x-1
fn sub(x: Int) -> Int {
x - 1
}

fn perform_operation(x:Int, f: fn(Int) -> Int) -> Int {
x
fn perform_operation(x: Int, f: fn(Int) -> Int) -> Int {
x
|> add1 // Add 1
|> mul // Multiply by 2
|> sub // Subtract 3
|> double // Double the result
|> mul // Multiply by 2
|> sub // Subtract 3
|> double
// Double the result
}

test perform_opeartion_test(){
let x = 2
let result = perform_operation(2,add1)
result == 16
test perform_opeartion_test() {
let x = 2
let result = perform_operation(2, add1)
result == 16
}

fn add2 (x:Int)->Int {
x+2
fn add2(x: Int) -> Int {
x + 2
}

fn square (x:Int)-> Int{
x*x
fn square(x: Int) -> Int {
x * x
}

fn practice(x:Int, f:fn(Int)->Int)->Int{
x
fn practice(x: Int, f: fn(Int) -> Int) -> Int {
x
|> add2
|> square
}
}

test practice_test (){
practice(2,add2) ==16
test practice_test() {
practice(2, add2) == 16
}

//--------------------------------------------------
validator always_succeeds {
spend(_datum: Option<Data>, _redeemer: Data, _output_reference: Data, _context: Data) {
spend(
_datum: Option<Data>,
_redeemer: Data,
_output_reference: Data,
_context: Data,
) {
True
}
}

else(_) {
fail
}
}

// ---------------------------------------------------------
// PATTERN Matching
Expand All @@ -106,15 +112,14 @@ validator always_succeeds {
// }
// }

fn greater(x:Int)->Int{
if x <0{
x
}
else{
x+10
}
fn greater(x: Int) -> Int {
if x < 0 {
x
} else {
x + 10
}
}

test greater_test(){
greater(2) == 12
}
test greater_test() {
greater(2) == 12
}
4 changes: 4 additions & 0 deletions validators/always_succeeds.ak
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ validator alwaysSucceeds {
) {
True
}

else(_) {
fail
}
}

test tes_always_successful() {
Expand Down
28 changes: 14 additions & 14 deletions validators/basic01.ak
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
fn main() {
let x =5
let y =6
let z =x+y
x + y +z
let x = 5
let y = 6
let z = x + y
x + y + z
}

fn sum (x ,y)-> Int{
x+y
fn sum(x, y) -> Int {
x + y
}

fn sub (a: Int, b: Int)-> Int{
a-b
fn sub(a: Int, b: Int) -> Int {
a - b
}

test main_test() {
main() ==22
main() == 22
}

test sum_test(){
sum(10,11) == 21
test sum_test() {
sum(10, 11) == 21
}

//we are failing this by not putting the expected value , (5-3=2, we put 21, so it failing)
test sub_test(){
sub(5,3) == 2
}
test sub_test() {
sub(5, 3) == 2
}
75 changes: 75 additions & 0 deletions validators/basic03.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
fn even_odd_bool(x: Int) -> Bool {
if x % 2 == 0 {
True
} else {
False
}
}

test even_odd_bool_test() {
even_odd_bool(10) == True
}

fn even_odd_number(x: Int) -> Int {
if x % 2 == 0 {
1
} else {
0
}
}

test even_odd_number_test() {
even_odd_number(10) == 1
}

fn even_odd_when(x: Int) -> Bool {
when x % 2 is {
0 -> True
_ -> False
}
}

test even_odd_when_test() {
even_odd_when(2)
}

fn even(x: Int) -> Bool {
x % 2 == 0
}

fn odd(x: Int) -> Bool {
!even(x)
}

test even_odd_test_optimise() {
let number = 3

if even(number) {
True
} else {
odd(number)
}
}

test even_odd_test() {
let number = 3

let is_even_or_odd = even(number) || odd(number)

expect is_even_or_odd
}

// compares prices vs payment and answer True if payment >= price,
//and False if not.

fn price(price: Int, payment: Int) -> Bool {
payment >= price
}

test price_test_pass() {
price(10, 12)
}

test price_test_fail() {
price(100, 12)
}
Loading

0 comments on commit fdb9a85

Please sign in to comment.