-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
14 changed files
with
165 additions
and
7 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,15 +1,17 @@ | ||
<p>Gleam's <code>Int</code> type represents whole numbers.</p> | ||
<p> | ||
Gleam's <code>Int</code> type represents whole numbers. | ||
</p> | ||
<p> | ||
There are arithmetic and comparison operators for ints. | ||
There are arithmetic and comparison operators for ints, as well as the | ||
equality operator which works on all types. | ||
</p> | ||
<p> | ||
When running on the Erlang virtual machine ints have no maximum and minimum | ||
size. When running on JavaScript runtimes ints are represented using | ||
JavaScript's 64 bit floating point numbers, | ||
</p> | ||
<p> | ||
The <a href="https://hexdocs.pm/gleam_stdlib/gleam/int.html"><code>gleam/int</code></a> | ||
The | ||
<a href="https://hexdocs.pm/gleam_stdlib/gleam/int.html" | ||
><code>gleam/int</code></a | ||
> | ||
standard library module contains functions for working with ints. | ||
</p> |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import gleam/io | ||
|
||
pub fn main() { | ||
let triple = #(1, 2.2, "three") | ||
io.debug(triple) | ||
|
||
let #(a, _, _) = triple | ||
io.debug(a) | ||
io.debug(triple.1) | ||
} |
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,20 @@ | ||
<p> | ||
Lists are good for when we want a collection of one type, but sometimes we | ||
want to combine multiple values of different types. In this case tuples are a | ||
quick and convenient option. | ||
</p> | ||
<p> | ||
The tuple access syntax can be used to get elements from a tuple without | ||
pattern matching. <code>some_tuple.0</code> gets the first element, | ||
<code>some_tuple.1</code> gets the second element, etc. | ||
</p> | ||
<p> | ||
Tuples are generic types, they have type parameters for the types they | ||
contain. <code>#(1, "Hi!")</code> has the type <code>#(Int, String)</code>, | ||
and <code>#(1.4, 10, 48)</code> has the type <code>#(Float, Int, Int)</code>. | ||
</p> | ||
<p> | ||
Tuples are most commonly used to return 2 or 3 values from a function. Other | ||
times it is often is clearer to use a <em>custom type</em>, which we will | ||
cover next. | ||
</p> |
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,22 @@ | ||
import gleam/io | ||
|
||
pub type Season { | ||
Spring | ||
Summer | ||
Autumn | ||
Winter | ||
} | ||
|
||
pub fn main() { | ||
io.debug(weather(Spring)) | ||
io.debug(weather(Autumn)) | ||
} | ||
|
||
fn weather(season: Season) -> String { | ||
case season { | ||
Spring -> "Mild" | ||
Summer -> "Hot" | ||
Autumn -> "Windy" | ||
Winter -> "Cold" | ||
} | ||
} |
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,9 @@ | ||
<p> | ||
Gleam has a few built in types such as <code>Int</code>, <code>String</code>, | ||
but custom types allow the creation of entirely new types. | ||
</p> | ||
<p> | ||
A custom type is defined with the <code>type</code> keyword followed by a | ||
constructor for each <em>variant</em> of the type. | ||
</p> | ||
<p>Custom type variants can be pattern matched on using a case expression.</p> |
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,17 @@ | ||
import gleam/io | ||
|
||
pub type SchoolPerson { | ||
Teacher(name: String, subject: String) | ||
Student(String) | ||
} | ||
|
||
pub fn main() { | ||
let teacher1 = Teacher("Mr Schofield", "Physics") | ||
let teacher2 = Teacher(name: "Miss Percy", subject: "Physics") | ||
let student1 = Student("Koushiar") | ||
let student2 = Student("Naomi") | ||
let student3 = Student("Shaheer") | ||
|
||
let school = [teacher1, teacher2, student1, student2, student3] | ||
io.debug(school) | ||
} |
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,10 @@ | ||
<p>Variants of a record can hold other data within them.</p> | ||
<p> | ||
These fields can be given labels, and like function argument labels they can | ||
be optionally used when calling the record constructor. Typically labels will | ||
be used for variants that define them. | ||
</p> | ||
<p> | ||
It is common to have a custom type with one variant that holds data, this is | ||
the Gleam equivalent of a struct or object in other languages. | ||
</p> |
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,15 @@ | ||
import gleam/io | ||
|
||
pub type SchoolPerson { | ||
Teacher(name: String, subject: String) | ||
Student(name: String) | ||
} | ||
|
||
pub fn main() { | ||
let teacher = Teacher("Mr Schofield", "Physics") | ||
let student = Student("Koushiar") | ||
|
||
io.debug(teacher.name) | ||
io.debug(student.name) | ||
// io.debug(teacher.subject) | ||
} |
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,18 @@ | ||
<p> | ||
The record accessor syntax <code>record.field_label</code> can be used to get | ||
contained values from a custom type record. | ||
</p> | ||
<p> | ||
The accessor syntax can only be used for fields that are in the same position | ||
and have the same type for all variants of the custom type. | ||
</p> | ||
<p> | ||
The <code>name</code> field is in the first position and has type | ||
<code>String</code> for all variants, so it can be accessed. | ||
</p> | ||
<p> | ||
The <code>subject</code> field is absent on the <code>Student</code> variant, | ||
so it cannot be used on any variant of type <code>SchoolPerson</code>. | ||
Uncomment the <code>teacher.subject</code> line to see the compile error from | ||
trying to use this accessor. | ||
</p> |
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,15 @@ | ||
import gleam/io | ||
|
||
pub type SchoolPerson { | ||
Teacher(name: String, subject: String, floor: Int, room: Int) | ||
} | ||
|
||
pub fn main() { | ||
let teacher1 = Teacher(name: "Mr Dodd", subject: "ICT", floor: 2, room: 2) | ||
|
||
// Use the update syntax | ||
let teacher2 = Teacher(..teacher1, subject: "PE", room: 6) | ||
|
||
io.debug(teacher1) | ||
io.debug(teacher2) | ||
} |
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,12 @@ | ||
<p> | ||
The record update syntax can be used to create a new record from an existing | ||
one of the same type, but with some fields changed. | ||
</p> | ||
<p> | ||
The accessor syntax can only be used for fields that are in the same position | ||
and have the same type for all variants of the custom type. | ||
</p> | ||
<p> | ||
Gleam is an immutable language, so using the record update syntax does not | ||
mutate or otherwise change the original record. | ||
</p> |