-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
55 changed files
with
1,172 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/frontend/src/pages/Chapters/JS/ChapterConditionals/course.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Chapter 7 : Conditionals | ||
|
||
<dialog character="robot">[DROID-1242] INVALID CONDITIONAL INSTRUCTIONS. ERR %%$7834[[{23e3}]] PLEASE SPECIFY CONDITIONAL INSTRUCTIONS.</dialog> | ||
|
||
## Booleans | ||
|
||
Booleans are typed _bool_ in LIGO : | ||
|
||
``` | ||
let a: bool = true; // or false | ||
``` | ||
|
||
## Comparing Values | ||
|
||
Only values of the same type can be natively compared, i.e. int, nat, string, tez, timestamp, address, etc... However some values of the same type are not natively comparable, i.e. maps, sets or lists. You will have to write your own comparison functions for those. | ||
|
||
``` | ||
// Comparing strings | ||
let a: string = "Alice"; | ||
let b: string = "Alice"; | ||
let c: bool = (a == b); // true | ||
// Comparing numbers | ||
let a: int = 5; | ||
let b: int = 4; | ||
let c: bool = (a == b); | ||
let d: bool = (a > b); | ||
let e: bool = (a < b); | ||
let f: bool = (a <= b); | ||
let g: bool = (a >= b); | ||
let h: bool = (a != b); | ||
// Comparing tez | ||
let a: tez = 5 as mutez; | ||
let b: tez = 10 as mutez; | ||
let c: bool = (a == b); // false | ||
``` | ||
|
||
## Conditionals | ||
|
||
Conditional logic enables forking the control flow depending on the state. | ||
|
||
``` | ||
let isSmall = (n : nat) : bool => { | ||
if (n < (10 as nat)) { return true; } else { return false; }; | ||
}; | ||
``` | ||
|
||
## Your mission | ||
|
||
We want to conditionally change the engine attribute (third number) to 1 only if it is equal to 0. | ||
|
||
<!-- prettier-ignore -->1- Refactor *modified\_ship* as a variable equal to *my\_ship* | ||
|
||
<!-- prettier-ignore -->2- Then define a condition _if_ the engine attribute equal 0. Don't forget the attributes are defined as strings. | ||
|
||
<!-- prettier-ignore -->3- If the condition is met, change *modified\_ship* to it new value. Otherwise, _skip_ the instructions. | ||
|
||
<!-- prettier-ignore -->⚠️ If you have installed LIGO then you can test the execution of the *modify\_ship* function by running the following command: | ||
|
||
``` | ||
ligo run-function main.ligo modify_ship '("010433")' | ||
``` |
8 changes: 8 additions & 0 deletions
8
src/frontend/src/pages/Chapters/JS/ChapterConditionals/exercise.ligo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
type ship_code = string; | ||
let my_ship: ship_code = "020433"; | ||
my_ship = "222031"; | ||
const my_ship_price : tez = 3 as tez * 1.20; | ||
|
||
let modify_ship = (my_ship: ship_code): ship_code => { | ||
// Type your solution below | ||
} |
11 changes: 11 additions & 0 deletions
11
src/frontend/src/pages/Chapters/JS/ChapterConditionals/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* eslint import/no-webpack-loader-syntax: off */ | ||
// @ts-ignore | ||
import course from "!raw-loader!./course.md"; | ||
/* eslint import/no-webpack-loader-syntax: off */ | ||
// @ts-ignore | ||
import exercise from "!raw-loader!./exercise.ligo"; | ||
/* eslint import/no-webpack-loader-syntax: off */ | ||
// @ts-ignore | ||
import solution from "!raw-loader!./solution.ligo"; | ||
|
||
export const data = { course, exercise, solution, supports: {} }; |
16 changes: 16 additions & 0 deletions
16
src/frontend/src/pages/Chapters/JS/ChapterConditionals/solution.ligo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
type ship_code = string; | ||
let my_ship: ship_code = "020433"; | ||
my_ship = "222031"; | ||
const my_ship_price : tez = 3 as tez * 1.20; | ||
|
||
let modify_ship = (my_ship: ship_code): ship_code => { | ||
// Type your solution below | ||
let modify_ship: ship_code = my_ship; | ||
if (String.sub(2 as nat, 1 as nat, my_ship) == "0") { | ||
return "" + String.sub(0 as nat, 2 as nat, my_ship) + "1" + String.sub(3 as nat, 3 as nat, my_ship) | ||
} else { | ||
return modify_ship; | ||
} | ||
} | ||
|
||
|
73 changes: 73 additions & 0 deletions
73
src/frontend/src/pages/Chapters/JS/ChapterFunctions/course.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Chapter 6 : Functions | ||
|
||
<dialog character="mechanics">Captain, why are you trying to change the part yourself? Just write a function on the terminal and send it to a droid.</dialog> | ||
|
||
LIGO functions are the basic building block of contracts. Each entrypoint of a contract is a function and each smart contract must have at least one function named _main_ that dispatches the control flow to other functions. | ||
|
||
When calling a function, LIGO makes a copy of the arguments but also the environment variables. Therefore any modification to these will not be reflected outside the scope of the function and will be lost if not explicitly returned by the function. | ||
|
||
Functions in JsLIGO are defined using the let or const keyword, like other values. The difference is that parameters are provided after the value name, with its type, then followed by the return type. | ||
|
||
Here is how you define a basic function that sums two integers: | ||
|
||
``` | ||
let add = ([a, b]: [int, int]): int => a + b; | ||
``` | ||
|
||
If the body contains more than a single expression, you use block between braces: | ||
|
||
``` | ||
let myFun = ([x, y]: [int, int]): int => { | ||
let doubleX = x + x; | ||
let doubleY = y + y; | ||
return doubleX + doubleY; | ||
}; | ||
``` | ||
|
||
Note that JsLIGO, like JavaScript, requires the return keyword to indicate what is being returned. If return is not used, it will be the same as return unit. | ||
|
||
<!-- prettier-ignore -->By default, LIGO will warn about unused arguments inside functions. In case we do not use an argument, we can use the wildcard _\__ to prevent warnings. Either use _\__ instead of the argument identifier: | ||
|
||
``` | ||
let k = ([x, _] : [int, int]) : int => x; | ||
``` | ||
|
||
or use an identifier starting with wildcard: | ||
|
||
``` | ||
let k = ([x, _y] : [int, int]) : int => x; | ||
``` | ||
|
||
## Anonymous functions (a.k.a. lambdas) | ||
|
||
It is possible to define functions without assigning them a name. They are useful when you want to pass them as arguments, or assign them to a key in a record or a map. | ||
|
||
``` | ||
let increment = (b: int): int => ((a: int): int => a + 1) (b); | ||
let a: int = increment(1); // a == 2 | ||
``` | ||
|
||
If the example above seems contrived, here is a more common design pattern for lambdas: to be used as parameters to functions. Consider the use case of having a list of integers and mapping the increment function to all its elements. | ||
|
||
``` | ||
let incr_map = (l: list<int>): list<int> => List.map((i: int) => i + 1, l); | ||
``` | ||
|
||
## Nested functions (also known as closures) | ||
|
||
It's possible to place functions inside other functions. These functions have access to variables in the same scope. | ||
|
||
``` | ||
let closure_example = (i: int): int => { | ||
let closure = (j: int): int => i + j; | ||
return closure(i); | ||
}; | ||
``` | ||
|
||
## Your mission | ||
|
||
<!-- prettier-ignore -->1- Write an block function *modify\_ship* taking as argument *my\_ship* of type *ship\_code* and returning a varible of type *ship\_code* as well. | ||
|
||
<!-- prettier-ignore -->2- In the block, copy/cut the code from the previous chapter that modified the third attribute from 0 to 1 and assign the result to a constant *modified\_ship* | ||
|
||
<!-- prettier-ignore -->3- Return *modified\_ship* |
4 changes: 4 additions & 0 deletions
4
src/frontend/src/pages/Chapters/JS/ChapterFunctions/exercise.ligo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type ship_code = string; | ||
let my_ship: ship_code = "020433"; | ||
// Modify the code below | ||
my_ship = "" + String.sub(0 as nat, 2 as nat, my_ship) + "1" + String.sub(3 as nat, 3 as nat, my_ship) |
11 changes: 11 additions & 0 deletions
11
src/frontend/src/pages/Chapters/JS/ChapterFunctions/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* eslint import/no-webpack-loader-syntax: off */ | ||
// @ts-ignore | ||
import exercise from "!raw-loader!./exercise.ligo"; | ||
/* eslint import/no-webpack-loader-syntax: off */ | ||
// @ts-ignore | ||
import solution from "!raw-loader!./solution.ligo"; | ||
/* eslint import/no-webpack-loader-syntax: off */ | ||
// @ts-ignore | ||
import course from "!raw-loader!./course.md"; | ||
|
||
export const data = { course, exercise, solution, supports: {} }; |
4 changes: 4 additions & 0 deletions
4
src/frontend/src/pages/Chapters/JS/ChapterFunctions/solution.ligo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type ship_code = string; | ||
let my_ship: ship_code = "020433"; | ||
// Modify the code below | ||
let modify_ship = (my_ship: ship_code): ship_code => "" + String.sub(0 as nat, 2 as nat, my_ship) + "1" + String.sub(3 as nat, 3 as nat, my_ship) |
Oops, something went wrong.