-
-
Notifications
You must be signed in to change notification settings - Fork 81
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' of https://github.com/massivefermion/exercism-gleam
- Loading branch information
Showing
777 changed files
with
16,283 additions
and
1,321 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
gleam 0.30.5 |
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,8 @@ | ||
{ | ||
"blurb": "Gleam has first class functions, meaning they can be assigned to variables and used like any other value.", | ||
"authors": [ | ||
"lpil" | ||
], | ||
"contributors": [ | ||
] | ||
} |
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,56 @@ | ||
# About | ||
|
||
Gleam has first class functions, meaning functions are normal values that can be assigned to variables, passed as arguments, and returned from other functions. | ||
|
||
A named function defined in a module can be referenced by its name. | ||
|
||
```gleam | ||
pub fn main() { | ||
// Assign the function to a variable | ||
let f = add_one | ||
// Invoke the function | ||
f(1) // -> 2 | ||
f(2) // -> 3 | ||
} | ||
fn add_one(x) { | ||
x + 1 | ||
} | ||
``` | ||
|
||
Gleam also has anonymous functions, which are defined within other functions. | ||
|
||
```gleam | ||
// Define the function | ||
let f = fn(x) { x + 1 } | ||
// Invoke the function | ||
f(1) // -> 2 | ||
f(2) // -> 3 | ||
``` | ||
|
||
The type of a function is written using a similar syntax to anonymous functions. The type of a function that takes an `Int` and a `Float` and returns an `Int` is written like this: | ||
|
||
```gleam | ||
fn(Int, Float) -> Int | ||
``` | ||
|
||
Anonymous functions can reference variables that were in scope when they were defined, making them _closures_. | ||
|
||
```gleam | ||
let secret_number = 42 | ||
// This function always returns 42 | ||
fn() { secret_number } | ||
``` | ||
|
||
The _function capture_ syntax provides a convenient shorthand for creating anonymous functions that pass a single argument to a function. These two expressions are equivalent: | ||
|
||
```gleam | ||
// Anonymous function syntax | ||
let f = fn(x) { my_function(1, 2, x) } | ||
// Function capture syntax | ||
let f = my_function(1, 2, _) | ||
``` |
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,56 @@ | ||
# Introduction | ||
|
||
Gleam has first class functions, meaning functions are normal values that can be assigned to variables, passed as arguments, and returned from other functions. | ||
|
||
A named function defined in a module can be referenced by its name. | ||
|
||
```gleam | ||
pub fn main() { | ||
// Assign the function to a variable | ||
let f = add_one | ||
// Invoke the function | ||
f(1) // -> 2 | ||
f(2) // -> 3 | ||
} | ||
fn add_one(x) { | ||
x + 1 | ||
} | ||
``` | ||
|
||
Gleam also has anonymous functions, which are defined within other functions. | ||
|
||
```gleam | ||
// Define the function | ||
let f = fn(x) { x + 1 } | ||
// Invoke the function | ||
f(1) // -> 2 | ||
f(2) // -> 3 | ||
``` | ||
|
||
The type of a function is written using a similar syntax to anonymous functions. The type of a function that takes an `Int` and a `Float` and returns an `Int` is written like this: | ||
|
||
```gleam | ||
fn(Int, Float) -> Int | ||
``` | ||
|
||
Anonymous functions can reference variables that were in scope when they were defined, making them _closures_. | ||
|
||
```gleam | ||
let secret_number = 42 | ||
// This function always returns 42 | ||
fn() { secret_number } | ||
``` | ||
|
||
The _function capture_ syntax provides a convenient shorthand for creating anonymous functions that pass a single argument to a function. These two expressions are equivalent: | ||
|
||
```gleam | ||
// Anonymous function syntax | ||
let f = fn(x) { my_function(1, 2, x) } | ||
// Function capture syntax | ||
let f = my_function(1, 2, _) | ||
``` |
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,2 @@ | ||
[ | ||
] |
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,8 @@ | ||
{ | ||
"blurb": "A bit string is a contiguous sequence of bits in memory.", | ||
"authors": [ | ||
"lpil" | ||
], | ||
"contributors": [ | ||
] | ||
} |
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,93 @@ | ||
# Introduction | ||
|
||
Working with binary data can be tricky, so Gleam provides a `BitString` type and accompanying syntax to construct and to pattern match on binary data. | ||
|
||
Bit string literals are defined using the `<<>>` syntax. When defining a bit string literal, it is defined in segments. Each segment has a value and and annotation, separated by a `:`. The annotation specifies how many bits will be used to encode the value, and can be omitted completely, which will default to a 8-bit integer value. | ||
|
||
```gleam | ||
// This defines a bit string with three segments of a single bit each | ||
<<0:1, 1:1, 0:1>> | ||
// This defines a bit string with three segments of 8 bits each | ||
<<0, 1, 0>> | ||
``` | ||
|
||
Specifying the type as `:1` is a shorthand for writing `:size(1)`. You need to use the longer syntax if the bit size comes from a variable. | ||
|
||
```gleam | ||
let segment_size = 1 | ||
<<0:size(segment_size), 1:size(segment_size), 0:size(segment_size)>> | ||
``` | ||
|
||
## Binary | ||
|
||
When writing binary integer literals, we can write them directly in base-2 notation by prefixing the literal with `0b`. Note that they will be displayed as decimal numbers when printed in tests or in your program. | ||
|
||
```gleam | ||
<<0b1011:4>> == <<11:4>> | ||
// -> True | ||
``` | ||
|
||
## Truncating | ||
|
||
If the value of the segment overflows the capacity of the segment's type, it will be truncated from the left. | ||
|
||
```gleam | ||
<<0b1011:3>> == <<0b0011:3>> | ||
// -> True | ||
``` | ||
|
||
## Prepending and appending | ||
|
||
You can both prepend and append to an existing bit string using the bit string syntax. The `:bit_string` annotation must be used for the existing bit string. | ||
|
||
```gleam | ||
let value = <<0b110:3, 0b001:3>> | ||
let new_value = <<0b011:3, value:bit_string, 0b000:3>> | ||
// -> <<120, 8:size(4)>> | ||
``` | ||
|
||
## Concatenating | ||
|
||
We can concatenate bit strings stored in variables using the syntax. The `:bit_string` annotation must be used when concatenating two bit strings of variable sizes. | ||
|
||
```gleam | ||
let first = <<0b110:3>> | ||
let second = <<0b001:3>> | ||
let concatenated = <<first:bit_string, second:bit_string>> | ||
// -> <<49:size(6)>> | ||
``` | ||
|
||
## Pattern matching | ||
|
||
Pattern matching can also be done to obtain values from the bit string. You have to know the number of bits for each fragment you want to capture, with one exception: the `:bit_string` annotation can be used to pattern match on a bit string of an unknown size, but this can only be used for the last fragment. | ||
|
||
```gleam | ||
let assert <<value:4, rest:bit_string>> = <<0b01101001:8>> | ||
value == 0b0110 | ||
// -> true | ||
``` | ||
|
||
## Inspecting bit strings | ||
|
||
~~~~exercism/note | ||
Bit strings might be printed in a different format than the format that was used | ||
to create them. This often causes confusion when learning bit strings. | ||
~~~~ | ||
|
||
By default, bit strings are displayed in fragments of 8 bits (a byte), even if you created them with fragments of a different size. | ||
|
||
```gleam | ||
<<2011:11>> | ||
// -> <<251, 3:size(3)>> | ||
``` | ||
|
||
If you create a bit string that represents a printable UTF-8 encoded string, it may displayed as a string by functions such as `io.debug`. This is due to an implementation detail of how Gleam represents strings internally. | ||
|
||
```gleam | ||
<<>> | ||
// -> "" | ||
<<65, 66, 67>> | ||
// -> "ABC" | ||
``` |
Oops, something went wrong.