From eca420f4290f1628caec3084ea2a29e7c1eb8743 Mon Sep 17 00:00:00 2001 From: PastMoments <3858420+PastMoments@users.noreply.github.com> Date: Fri, 10 May 2024 20:31:38 -0700 Subject: [PATCH] Rename Bit Strings to Bit Arrays --- concepts/bit-arrays/.meta/config.json | 7 ++ concepts/bit-arrays/about.md | 93 +++++++++++++++++++ concepts/bit-arrays/introduction.md | 93 +++++++++++++++++++ .../{bit-strings => bit-arrays}/links.json | 0 concepts/bit-strings/.meta/config.json | 8 -- concepts/bit-strings/about.md | 93 ------------------- concepts/bit-strings/introduction.md | 93 ------------------- config.json | 10 +- exercises/concept/dna-encoding/.docs/hints.md | 6 +- .../dna-encoding/.docs/introduction.md | 2 +- .../dna-encoding/.docs/introduction.md.tpl | 2 +- .../concept/dna-encoding/.meta/config.json | 4 +- .../concept/dna-encoding/.meta/design.md | 10 +- exercises/concept/log-levels/.meta/design.md | 2 +- 14 files changed, 211 insertions(+), 212 deletions(-) create mode 100644 concepts/bit-arrays/.meta/config.json create mode 100644 concepts/bit-arrays/about.md create mode 100644 concepts/bit-arrays/introduction.md rename concepts/{bit-strings => bit-arrays}/links.json (100%) delete mode 100644 concepts/bit-strings/.meta/config.json delete mode 100644 concepts/bit-strings/about.md delete mode 100644 concepts/bit-strings/introduction.md diff --git a/concepts/bit-arrays/.meta/config.json b/concepts/bit-arrays/.meta/config.json new file mode 100644 index 000000000..24adfacbd --- /dev/null +++ b/concepts/bit-arrays/.meta/config.json @@ -0,0 +1,7 @@ +{ + "blurb": "A bit array is a contiguous sequence of bits in memory.", + "authors": [ + "lpil" + ], + "contributors": [] +} \ No newline at end of file diff --git a/concepts/bit-arrays/about.md b/concepts/bit-arrays/about.md new file mode 100644 index 000000000..cc407d7ca --- /dev/null +++ b/concepts/bit-arrays/about.md @@ -0,0 +1,93 @@ +# Introduction + +Working with binary data can be tricky, so Gleam provides a `BitArray` type and accompanying syntax to construct and to pattern match on binary data. + +Bit array literals are defined using the `<<>>` syntax. When defining a bit array literal, it is defined in segments. Each segment has a value 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 array with three segments of a single bit each +<<0:1, 1:1, 0:1>> + +// This defines a bit array 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 array using the bit array syntax. The `:bits` annotation must be used for the existing bit array. + +```gleam +let value = <<0b110:3, 0b001:3>> +let new_value = <<0b011:3, value:bits, 0b000:3>> +// -> <<120, 8:size(4)>> +``` + +## Concatenating + +We can concatenate bit arrays stored in variables using the syntax. The `:bits` annotation must be used when concatenating two bit arrays of variable sizes. + +```gleam +let first = <<0b110:3>> +let second = <<0b001:3>> +let concatenated = <> +// -> <<49:size(6)>> +``` + +## Pattern matching + +Pattern matching can also be done to obtain values from the bit array. You have to know the number of bits for each fragment you want to capture, with one exception: the `:bits` annotation can be used to pattern match on a bit array of an unknown size, but this can only be used for the last fragment. + +```gleam +let assert <> = <<0b01101001:8>> +value == 0b0110 +// -> true +``` + +## Inspecting bit arrays + +~~~~exercism/note +Bit arrays might be printed in a different format than the format that was used +to create them. This often causes confusion when learning bit arrays. +~~~~ + +By default, bit arrays 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 array 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" +``` diff --git a/concepts/bit-arrays/introduction.md b/concepts/bit-arrays/introduction.md new file mode 100644 index 000000000..cc407d7ca --- /dev/null +++ b/concepts/bit-arrays/introduction.md @@ -0,0 +1,93 @@ +# Introduction + +Working with binary data can be tricky, so Gleam provides a `BitArray` type and accompanying syntax to construct and to pattern match on binary data. + +Bit array literals are defined using the `<<>>` syntax. When defining a bit array literal, it is defined in segments. Each segment has a value 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 array with three segments of a single bit each +<<0:1, 1:1, 0:1>> + +// This defines a bit array 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 array using the bit array syntax. The `:bits` annotation must be used for the existing bit array. + +```gleam +let value = <<0b110:3, 0b001:3>> +let new_value = <<0b011:3, value:bits, 0b000:3>> +// -> <<120, 8:size(4)>> +``` + +## Concatenating + +We can concatenate bit arrays stored in variables using the syntax. The `:bits` annotation must be used when concatenating two bit arrays of variable sizes. + +```gleam +let first = <<0b110:3>> +let second = <<0b001:3>> +let concatenated = <> +// -> <<49:size(6)>> +``` + +## Pattern matching + +Pattern matching can also be done to obtain values from the bit array. You have to know the number of bits for each fragment you want to capture, with one exception: the `:bits` annotation can be used to pattern match on a bit array of an unknown size, but this can only be used for the last fragment. + +```gleam +let assert <> = <<0b01101001:8>> +value == 0b0110 +// -> true +``` + +## Inspecting bit arrays + +~~~~exercism/note +Bit arrays might be printed in a different format than the format that was used +to create them. This often causes confusion when learning bit arrays. +~~~~ + +By default, bit arrays 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 array 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" +``` diff --git a/concepts/bit-strings/links.json b/concepts/bit-arrays/links.json similarity index 100% rename from concepts/bit-strings/links.json rename to concepts/bit-arrays/links.json diff --git a/concepts/bit-strings/.meta/config.json b/concepts/bit-strings/.meta/config.json deleted file mode 100644 index 531301237..000000000 --- a/concepts/bit-strings/.meta/config.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "blurb": "A bit string is a contiguous sequence of bits in memory.", - "authors": [ - "lpil" - ], - "contributors": [ - ] -} diff --git a/concepts/bit-strings/about.md b/concepts/bit-strings/about.md deleted file mode 100644 index 95f738b38..000000000 --- a/concepts/bit-strings/about.md +++ /dev/null @@ -1,93 +0,0 @@ -# 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 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 = <> -// -> <<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 <> = <<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" -``` diff --git a/concepts/bit-strings/introduction.md b/concepts/bit-strings/introduction.md deleted file mode 100644 index 95f738b38..000000000 --- a/concepts/bit-strings/introduction.md +++ /dev/null @@ -1,93 +0,0 @@ -# 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 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 = <> -// -> <<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 <> = <<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" -``` diff --git a/config.json b/config.json index ddfac3a94..b01bb8e8f 100644 --- a/config.json +++ b/config.json @@ -339,7 +339,7 @@ "name": "DNA Encoding", "uuid": "e8b77421-e3da-4875-9f9d-8d263df019f2", "concepts": [ - "bit-strings" + "bit-arrays" ], "prerequisites": [ "lists", @@ -1746,7 +1746,7 @@ "uuid": "f7e33664-ea30-4885-8961-4aa984a1341d", "practices": [], "prerequisites": [ - "bit-strings", + "bit-arrays", "case-expressions", "lists", "recursion", @@ -1964,8 +1964,8 @@ }, { "uuid": "8a61a160-c08d-4565-88ba-cab37fc90945", - "slug": "bit-strings", - "name": "Bit Strings" + "slug": "bit-arrays", + "name": "Bit Arrays" }, { "uuid": "6e5cd6a5-9fd8-4d16-9500-99850894fad9", @@ -2067,4 +2067,4 @@ "used_for/scripts", "used_for/web_development" ] -} +} \ No newline at end of file diff --git a/exercises/concept/dna-encoding/.docs/hints.md b/exercises/concept/dna-encoding/.docs/hints.md index 039e47555..3c44c5296 100644 --- a/exercises/concept/dna-encoding/.docs/hints.md +++ b/exercises/concept/dna-encoding/.docs/hints.md @@ -18,8 +18,8 @@ - The `encode_nucleotide` function can be used to encode a single nucleotide. -## 4. Decode a DNA bitstring +## 4. Decode a DNA bitarray -- The binary int syntax can be used within a bit string pattern. -- Return `Error(Nil)` if the remaining bit string is not long enough to contain +- The binary int syntax can be used within a bit array pattern. +- Return `Error(Nil)` if the remaining bit array is not long enough to contain a nucleotide. diff --git a/exercises/concept/dna-encoding/.docs/introduction.md b/exercises/concept/dna-encoding/.docs/introduction.md index 82656da03..19a892348 100644 --- a/exercises/concept/dna-encoding/.docs/introduction.md +++ b/exercises/concept/dna-encoding/.docs/introduction.md @@ -51,7 +51,7 @@ let new_value = <<0b011:3, value:bits, 0b000:3>> ### Concatenating -We can concatenate bit arrays stored in variables using the bit array syntax. The `:bits` annotation must be used when concatenating two bit strings of variable sizes. +We can concatenate bit arrays stored in variables using the bit array syntax. The `:bits` annotation must be used when concatenating two bit arrays of variable sizes. ```gleam let first = <<0b110:3>> diff --git a/exercises/concept/dna-encoding/.docs/introduction.md.tpl b/exercises/concept/dna-encoding/.docs/introduction.md.tpl index 123a55859..7513f6335 100644 --- a/exercises/concept/dna-encoding/.docs/introduction.md.tpl +++ b/exercises/concept/dna-encoding/.docs/introduction.md.tpl @@ -1,3 +1,3 @@ # Introduction -%{concept:bit-strings} +%{concept:bit-arrays} diff --git a/exercises/concept/dna-encoding/.meta/config.json b/exercises/concept/dna-encoding/.meta/config.json index e22edba61..c67446195 100644 --- a/exercises/concept/dna-encoding/.meta/config.json +++ b/exercises/concept/dna-encoding/.meta/config.json @@ -20,5 +20,5 @@ "forked_from": [ "elixir/dna-encoding" ], - "blurb": "Learn about bit strings by encoding DNA sequences as binary data." -} + "blurb": "Learn about bit arrays by encoding DNA sequences as binary data." +} \ No newline at end of file diff --git a/exercises/concept/dna-encoding/.meta/design.md b/exercises/concept/dna-encoding/.meta/design.md index 7a69159b2..3b5dbb629 100644 --- a/exercises/concept/dna-encoding/.meta/design.md +++ b/exercises/concept/dna-encoding/.meta/design.md @@ -2,11 +2,11 @@ ## Learning objectives -- Know what a bit string is. -- Know how to create a bit string literal. +- Know what a bit array is. +- Know how to create a bit array literal. - Know about the `:` binary size modifier. -- Know how to construct a bit string. -- Know how to pattern match a bit string. +- Know how to construct a bit array. +- Know how to pattern match a bit array. ## Out of scope @@ -16,7 +16,7 @@ ## Concepts -- `bit-strings` +- `bit-arrays` ## Prerequisites diff --git a/exercises/concept/log-levels/.meta/design.md b/exercises/concept/log-levels/.meta/design.md index dcde7461d..3b551f60c 100644 --- a/exercises/concept/log-levels/.meta/design.md +++ b/exercises/concept/log-levels/.meta/design.md @@ -15,7 +15,7 @@ - String implementation on Erlang and JavaScript. - String builders. -- Bit strings. +- Bit arrays. - Memory and performance characteristics. ## Concepts