Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into micah/file-errors
- Loading branch information
Showing
13 changed files
with
147 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,28 @@ | ||
# Blocks | ||
|
||
If statements | ||
Let bindings with blocks | ||
|
||
```gleam | ||
if condition { | ||
} else { | ||
let x = 3 | ||
let z = { | ||
let y = 2 | ||
x + y | ||
} | ||
``` | ||
|
||
Case Patterns | ||
|
||
```gleam | ||
when color is { | ||
Green -> "Success." | ||
Blue -> "Warning." | ||
Red -> "Error!" | ||
} | ||
``` | ||
A block can be thought of as calling an anonymous function with no arguments. They can be used anywhere a value is. | ||
Since everything is secretly a function, the last statement in any block is implicitly its return. | ||
|
||
Let bindings with blocks | ||
|
||
Blocks within a where-if clause | ||
```gleam | ||
let num = -5 | ||
let absNum = if num>=0 {num} else {-num} | ||
let message = when color is { | ||
Green -> "Success." | ||
Blue -> "Warning." | ||
Red -> "Error!" | ||
let name: Option(String) = someFunction() | ||
let suffix = "" | ||
when name is { | ||
Some(s)->{ | ||
let combined = s + suffix | ||
Some(combined) | ||
} | ||
None->None | ||
} | ||
``` | ||
|
||
Since everything is secretly a function, the last statement in any block is implicitly its return. | ||
``` |
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 |
---|---|---|
@@ -1,19 +1,39 @@ | ||
# Bool | ||
|
||
Bools are True or False | ||
Bools (short for booleans) are True or False. They correspond to the plutus bool primitive type. | ||
There are logical disjunctions (True || False) or conjunctions (True && True). | ||
```gleam | ||
False || False -- -> False | ||
True || False -- -> True | ||
False || True -- -> True | ||
True || True -- -> True | ||
False && False -- -> False | ||
True && False -- -> False | ||
False && True -- -> False | ||
True && True -- -> True | ||
``` | ||
|
||
There are logical conjunctions (True && True) or disjunctions (True || False). | ||
These are implemented using the plutus ifThenElse primitive. | ||
```gleam | ||
a || b -- if a {True} else {b} -- ifThenElse(a, True, b) | ||
a && b -- if a {b} else {False} -- ifThenElse(a, b, False) | ||
``` | ||
|
||
An if statement decides on a boolean value. | ||
```gleam | ||
fn negate(b: Bool)->Bool { | ||
fn negate(b: Bool) -> Bool { | ||
if b { | ||
False | ||
}else{ | ||
True | ||
} | ||
} | ||
``` | ||
|
||
fn and(b: Bool, c: Bool, d: Bool)->Bool{ | ||
The && operator in a function | ||
```gleam | ||
fn and(b: Bool, c: Bool, d: Bool) -> Bool{ | ||
b && c && d | ||
} | ||
``` |
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 |
---|---|---|
@@ -1 +1,8 @@ | ||
# Check | ||
|
||
Check is slower than assert but has stronger guarantees. | ||
You can unpack (1-match) data in a check. | ||
|
||
```gleam | ||
check Some(x) = Option(Int) | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
# Matching | ||
|
||
Where-if Patterns (also known as case in lamer languages) | ||
|
||
```gleam | ||
when color is { | ||
Green -> "Success." | ||
Blue -> "Warning." | ||
Red -> "Error!" | ||
} | ||
``` |
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 |
---|---|---|
@@ -1 +1,15 @@ | ||
# String | ||
|
||
There are Strings and ByteArrays (plutus bytestrings) | ||
|
||
The default representation using double quotes is a string. | ||
``` | ||
let mystring = "Hello World!" | ||
``` | ||
Strings may be appended and compared. | ||
|
||
For char operations, ByteArrays must be used. | ||
ByteArrays have efficient indexing, slicing, and can be compared or concatenated. | ||
ByteArrays can also be useful for non-text data (hence why we call them arrays not strings.) | ||
|
||
Due to their fixed byte width, you may find them not suitable for complex structures. |
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 |
---|---|---|
@@ -1 +1,12 @@ | ||
# Todo | ||
|
||
A 'todo' type errors on evaluation but casts to any type. | ||
It can be useful to let your project typecheck while you are still working on parts of it. | ||
|
||
``` | ||
fn notImplementedYet(){ | ||
todo | ||
} | ||
``` | ||
|
||
It is also good practice to use instead of a todo comment. |
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 |
---|---|---|
@@ -1 +1,9 @@ | ||
# Tuple | ||
|
||
Tuples are anonymous product types. They are useful for passing combinations of data between functions. | ||
|
||
``` | ||
let x = (1, 2) | ||
let y = (3, 4) | ||
pairAdder(x, y) -- --> (4, 6) | ||
``` |
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 |
---|---|---|
@@ -1 +1,24 @@ | ||
# Type aliases | ||
|
||
A type alias lets you create a name which is identical to another type, without any additional information. | ||
We like type names (including type alias names) to be PascalCase. | ||
|
||
```gleam | ||
type MyNumber = Integer | ||
``` | ||
|
||
I imagine them like variables for types. You could use this to simplify your type signatures for tuples. | ||
|
||
```gleam | ||
type Person = (String, Integer) | ||
fn createPerson(name: String, age: Integer) -> Person { | ||
(name, age) | ||
} | ||
``` | ||
|
||
If you want the type-alias to be accessible as a module, you should pub it. | ||
|
||
``` | ||
pub type MyVector3 = (Integer, Integer, Integer) | ||
``` |
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