Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Echo to language tour #132

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
with:
otp-version: false
# Ensure you update the bin/download-compiler Gleam version to match this
gleam-version: "1.6.0"
gleam-version: "1.7.0"
- run: ./bin/download-compiler
- run: gleam deps download
- run: gleam format --check src test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ import gleam/io
pub fn main() {
io.println("My lucky number is:")
// io.println(4)
// 👆️ Uncomment this line
// 👆️ Uncomment this line to see the error

// echo 4
// 👆️ You can use `echo` to debug print a value of any type!
}
7 changes: 2 additions & 5 deletions src/content/chapter0_basics/lesson04_type_checking/en.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
function only works with strings, not ints.
</p>
<p>
To fix the code change the code to call the
<a href="https://hexdocs.pm/gleam_stdlib/gleam/io.html#debug" target="_blank">
<code>io.debug</code>
</a>
function instead, as it will print a value of any type.
If you need to debug print something you can use the <code>echo</code>
keyword instead, as it will print a value of any type.
</p>
<p>
Gleam has no <code>null</code>, no implicit conversions, no exceptions, and
Expand Down
27 changes: 13 additions & 14 deletions src/content/chapter0_basics/lesson05_ints/code.gleam
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import gleam/int
import gleam/io

pub fn main() {
// Int arithmetic
io.debug(1 + 1)
io.debug(5 - 1)
io.debug(5 / 2)
io.debug(3 * 3)
io.debug(5 % 2)
echo 1 + 1
echo 5 - 1
echo 5 / 2
echo 3 * 3
echo 5 % 2

// Int comparisons
io.debug(2 > 1)
io.debug(2 < 1)
io.debug(2 >= 1)
io.debug(2 <= 1)
echo 2 > 1
echo 2 < 1
echo 2 >= 1
echo 2 <= 1

// Equality works for any type
io.debug(1 == 1)
io.debug(2 == 1)
echo 1 == 1
echo 2 == 1

// Standard library int functions
io.debug(int.max(42, 77))
io.debug(int.clamp(5, 10, 20))
echo int.max(42, 77)
echo int.clamp(5, 10, 20)
}
27 changes: 13 additions & 14 deletions src/content/chapter0_basics/lesson06_floats/code.gleam
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import gleam/float
import gleam/io

pub fn main() {
// Float arithmetic
io.debug(1.0 +. 1.5)
io.debug(5.0 -. 1.5)
io.debug(5.0 /. 2.5)
io.debug(3.0 *. 3.5)
echo 1.0 +. 1.5
echo 5.0 -. 1.5
echo 5.0 /. 2.5
echo 3.0 *. 3.5

// Float comparisons
io.debug(2.2 >. 1.3)
io.debug(2.2 <. 1.3)
io.debug(2.2 >=. 1.3)
io.debug(2.2 <=. 1.3)
echo 2.2 >. 1.3
echo 2.2 <. 1.3
echo 2.2 >=. 1.3
echo 2.2 <=. 1.3

// Equality works for any type
io.debug(1.1 == 1.1)
io.debug(2.1 == 1.2)
echo 1.1 == 1.1
echo 2.1 == 1.2

// Division by zero is not an error
io.debug(3.14 /. 0.0)
echo 3.14 /. 0.0

// Standard library float functions
io.debug(float.max(2.0, 9.5))
io.debug(float.ceiling(5.4))
echo float.max(2.0, 9.5)
echo float.ceiling(5.4)
}
16 changes: 7 additions & 9 deletions src/content/chapter0_basics/lesson07_number_formats/code.gleam
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import gleam/io

pub fn main() {
// Underscores
io.debug(1_000_000)
io.debug(10_000.01)
echo 1_000_000
echo 10_000.01

// Binary, octal, and hex Int literals
io.debug(0b00001111)
io.debug(0o17)
io.debug(0xF)
echo 0b00001111
echo 0o17
echo 0xF

// Scientific notation Float literals
io.debug(7.0e7)
io.debug(3.0e-4)
echo 7.0e7
echo 3.0e-4
}
6 changes: 2 additions & 4 deletions src/content/chapter0_basics/lesson08_equality/code.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import gleam/io

pub fn main() {
io.debug(100 == 100)
io.debug(1.5 != 0.1)
echo 100 == 100
echo 1.5 != 0.1
}
12 changes: 6 additions & 6 deletions src/content/chapter0_basics/lesson09_strings/code.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import gleam/string

pub fn main() {
// String literals
io.debug("👩‍💻 こんにちは Gleam 🏳️‍🌈")
io.debug(
io.println("👩‍💻 こんにちは Gleam 🏳️‍🌈")
io.println(
"multi
line
string",
)
io.debug("\u{1F600}")
io.println("\u{1F600}")

// Double quote can be escaped
io.println("\"X\" marks the spot")

// String concatenation
io.debug("One " <> "Two")
io.println("One " <> "Two")

// String functions
io.debug(string.reverse("1 2 3 4 5"))
io.debug(string.append("abc", "def"))
io.println(string.reverse("1 2 3 4 5"))
io.println(string.append("abc", "def"))
}
13 changes: 6 additions & 7 deletions src/content/chapter0_basics/lesson10_bools/code.gleam
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import gleam/bool
import gleam/io

pub fn main() {
// Bool operators
io.debug(True && False)
io.debug(True && True)
io.debug(False || False)
io.debug(False || True)
echo True && False
echo True && True
echo False || False
echo False || True

// Bool functions
io.debug(bool.to_string(True))
io.debug(bool.to_int(False))
echo bool.to_string(True)
echo bool.to_int(False)
}
8 changes: 4 additions & 4 deletions src/content/chapter0_basics/lesson11_assignments/code.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import gleam/io

pub fn main() {
let x = "Original"
io.debug(x)
io.println(x)

// Assign `y` to the value of `x`
let y = x
io.debug(y)
io.println(y)

// Assign `x` to a new value
let x = "New"
io.debug(x)
io.println(x)

// The `y` still refers to the original value
io.debug(y)
io.println(y)
}
4 changes: 1 addition & 3 deletions src/content/chapter0_basics/lesson15_type_aliases/code.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import gleam/io

pub type UserId =
Int

Expand All @@ -8,5 +6,5 @@ pub fn main() {
let two: Int = 2

// UserId and Int are the same type
io.debug(one == two)
echo one == two
}
6 changes: 2 additions & 4 deletions src/content/chapter0_basics/lesson16_blocks/code.gleam
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import gleam/io

pub fn main() {
let fahrenheit = {
let degrees = 64
degrees
}
// io.debug(degrees) // <- This will not compile
// echo degrees // <- This will not compile

// Changing order of evaluation
let celsius = { fahrenheit - 32 } * 5 / 9
io.debug(celsius)
echo celsius
}
7 changes: 2 additions & 5 deletions src/content/chapter0_basics/lesson16_blocks/en.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
Any variables assigned within the block can only be used within the block.
</p>
<p>
Try uncommenting
<a href="https://hexdocs.pm/gleam_stdlib/gleam/io.html#debug" target="_blank">
<code>io.debug(degrees)</code>
</a>
to see the compile error from trying to use a variable that is not in scope.
Try uncommenting <code>echo degrees</code> to see the compile error from
trying to use a variable that is not in scope.
</p>
<p>
Blocks can also be used to change the order of evaluation of binary operators
Expand Down
10 changes: 4 additions & 6 deletions src/content/chapter0_basics/lesson17_lists/code.gleam
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import gleam/io

pub fn main() {
let ints = [1, 2, 3]

io.debug(ints)
echo ints

// Immutably prepend
io.debug([-1, 0, ..ints])
echo [-1, 0, ..ints]

// Uncomment this to see the error
// io.debug(["zero", ..ints])
// echo ["zero", ..ints]

// The original lists are unchanged
io.debug(ints)
echo ints
}
10 changes: 4 additions & 6 deletions src/content/chapter0_basics/lesson18_constants/code.gleam
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import gleam/io

const ints: List(Int) = [1, 2, 3]

const floats = [1.0, 2.0, 3.0]

pub fn main() {
io.debug(ints)
io.debug(ints == [1, 2, 3])
echo ints
echo ints == [1, 2, 3]

io.debug(floats)
io.debug(floats == [1.0, 2.0, 3.0])
echo floats
echo floats == [1.0, 2.0, 3.0]
}
4 changes: 1 addition & 3 deletions src/content/chapter1_functions/lesson00_functions/code.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import gleam/io

pub fn main() {
io.debug(double(10))
echo double(10)
}

fn double(a: Int) -> Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import gleam/io

pub fn main() {
// Call a function with another function
io.debug(twice(1, add_one))
echo twice(1, add_one)

// Functions can be assigned to variables
let my_function = add_one
io.debug(my_function(100))
echo my_function(100)
}

fn twice(argument: Int, passed_function: fn(Int) -> Int) -> Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import gleam/io

pub fn main() {
// Assign an anonymous function to a variable
let add_one = fn(a) { a + 1 }
io.debug(twice(1, add_one))
echo twice(1, add_one)

// Pass an anonymous function as an argument
io.debug(twice(1, fn(a) { a * 2 }))
echo twice(1, fn(a) { a * 2 })

let secret_number = 42
// This anonymous function always returns 42
let secret = fn() { secret_number }
io.debug(secret())
echo secret()
}

fn twice(argument: Int, my_function: fn(Int) -> Int) -> Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import gleam/io

pub fn main() {
// These two statements are equivalent
let add_one_v1 = fn(x) { add(1, x) }
let add_one_v2 = add(1, _)

io.debug(add_one_v1(10))
io.debug(add_one_v2(10))
echo add_one_v1(10)
echo add_one_v2(10)
}

fn add(a: Int, b: Int) -> Int {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import gleam/io

pub fn main() {
let add_one = fn(x) { x + 1 }
let exclaim = fn(x) { x <> "!" }
Expand All @@ -8,10 +6,10 @@ pub fn main() {
// twice(10, exclaim)

// Here the type variable is replaced by the type Int
io.debug(twice(10, add_one))
echo twice(10, add_one)

// Here the type variable is replaced by the type String
io.debug(twice("Hello", exclaim))
echo twice("Hello", exclaim)
}

// The name `value` refers to the same type multiple times
Expand Down
8 changes: 5 additions & 3 deletions src/content/chapter1_functions/lesson07_pipelines/code.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import gleam/string

pub fn main() {
// Without the pipe operator
io.debug(string.drop_left(string.drop_right("Hello, Joe!", 1), 7))
io.println(string.drop_left(string.drop_right("Hello, Joe!", 1), 7))

// With the pipe operator
"Hello, Mike!"
|> string.drop_right(1)
|> echo
|> string.drop_left(7)
|> io.debug
|> io.println

// Changing order with function capturing
"1"
|> string.append("2")
|> echo
|> string.append("3", _)
|> io.debug
|> io.println
}
Loading
Loading