Skip to content

Commit 306ceb6

Browse files
committed
Iodata documentation
1 parent a2ff46b commit 306ceb6

File tree

2 files changed

+64
-21
lines changed

2 files changed

+64
-21
lines changed

src/gleam/int.gleam

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// A set of utility functions for working with `Int` values
2-
31
import gleam/order.{Order}
42
import gleam/result.{Option}
53

src/gleam/iodata.gleam

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,95 @@
1-
/// Represents Erlang's `iodata` type and the associated functions
2-
/// to work with it.
3-
1+
/// Iodata is a type used for efficiently building strings.
2+
///
3+
/// When we append one string to another the strings must be copied to a
4+
/// new location in memory so that they can sit together. This behaviour
5+
/// enables efficient reading of the string but copying can be expensive,
6+
/// especially if we want to join many strings together.
7+
///
8+
/// Iodata is different in that it can be joined together in constant time
9+
/// using minimal memory, and then can be efficiently converted to a string
10+
/// using the `to_string` function.
11+
///
412
pub external type Iodata;
513

6-
/// Prepends the string `prefix` to `to`
14+
/// Prepend a String onto the start of some Iodata.
15+
///
16+
/// Runs in constant time.
717
///
818
pub external fn prepend(to: Iodata, prefix: String) -> Iodata =
919
"gleam_stdlib" "iodata_prepend";
1020

11-
/// Appends the string `suffix` to `to`
21+
/// Append a String onto the end of some Iodata.
22+
///
23+
/// Runs in constant time.
1224
///
1325
pub external fn append(to: Iodata, suffix: String) -> Iodata =
1426
"gleam_stdlib" "iodata_append";
1527

16-
/// Prepends the Iodata `prefix` to `to`
28+
/// Prepend some Iodata onto the start of another.
29+
///
30+
/// Runs in constant time.
1731
///
1832
pub external fn prepend_iodata(to: Iodata, prefix: Iodata) -> Iodata =
1933
"gleam_stdlib" "iodata_prepend";
2034

21-
/// Prepends the Iodata `prefix` to `to`
35+
/// Append some Iodata onto the end of another.
36+
///
37+
/// Runs in constant time.
2238
///
2339
pub external fn append_iodata(to: Iodata, suffix: Iodata) -> Iodata =
2440
"gleam_stdlib" "iodata_append";
2541

26-
/// Builds an Iodata value from the supplied list of strings
42+
/// Convert a list of strings into iodata.
43+
///
44+
/// Runs in constant time.
2745
///
2846
pub external fn from_strings(List(String)) -> Iodata =
2947
"gleam_stdlib" "identity";
3048

31-
/// Builds a new Iodata out of a list of Iodata
49+
/// Joins a list of iodata into a single iodata.
50+
///
51+
/// Runs in constant time.
3252
///
3353
pub external fn concat(List(Iodata)) -> Iodata =
3454
"gleam_stdlib" "identity";
3555

36-
/// Turns a `String` into an `Iodata`
56+
/// Convert a string into iodata.
57+
///
58+
/// Runs in constant time.
3759
///
3860
pub external fn new(String) -> Iodata =
3961
"gleam_stdlib" "identity";
4062

4163
/// Turns an `Iodata` into a `String`
4264
///
65+
/// This function is implemented natively by the virtual machine and is highly
66+
/// optimised.
67+
///
4368
pub external fn to_string(Iodata) -> String =
4469
"erlang" "iolist_to_binary";
4570

46-
/// Returns the size of the Iodata in bytes
71+
/// Returns the size of the Iodata in bytes.
4772
///
4873
pub external fn byte_size(Iodata) -> Int =
4974
"erlang" "iolist_size";
5075

51-
/// Creates an `Iodata` value from a `Float` value.
76+
/// Creates textual representation of the given float as iodata.
5277
///
5378
pub external fn from_float(Float) -> Iodata =
5479
"io_lib_format" "fwrite_g";
5580

5681
/// Converts Iodata to a new Iodata where valid UTF-8 string data is
57-
/// lowercased
82+
/// lowercased.
5883
///
5984
pub external fn lowercase(Iodata) -> Iodata = "string" "lowercase"
6085

6186
/// Converts Iodata to a new Iodata where valid UTF-8 string data is
62-
/// uppercased
87+
/// uppercased.
6388
///
6489
pub external fn uppercase(Iodata) -> Iodata = "string" "uppercase"
6590

6691
/// Converts Iodata to a new Iodata where valid UTF-8 string data is
67-
/// reversed
92+
/// reversed.
6893
///
6994
pub external fn reverse(Iodata) -> Iodata = "string" "reverse"
7095

@@ -75,7 +100,7 @@ type Direction {
75100
external fn erl_split(Iodata, String, Direction) -> List(Iodata) =
76101
"string" "split"
77102

78-
/// Splits the supplied Iodata by the pattern `on`
103+
/// Splits iodata on a given pattern into a list of iodata.
79104
///
80105
pub fn split(iodata: Iodata, on pattern: String) -> List(Iodata) {
81106
erl_split(iodata, pattern, All)
@@ -84,7 +109,7 @@ pub fn split(iodata: Iodata, on pattern: String) -> List(Iodata) {
84109
external fn erl_replace(Iodata, String, String, Direction) -> Iodata =
85110
"string" "replace"
86111

87-
/// Replaces all instances of `all` with the string `with`
112+
/// Replaces all instances of a pattern with a given string substitute.
88113
///
89114
pub fn replace(
90115
in iodata: Iodata,
@@ -94,10 +119,30 @@ pub fn replace(
94119
erl_replace(iodata, pattern, substitute, All)
95120
}
96121

97-
/// Prepends the string `prefix` to `to`
122+
/// Compare two pieces of iodata to determine if they have the same textual
123+
/// content.
124+
///
125+
/// Comparing two iodata using the `==` operator may return False even if they
126+
/// have the same content as they may have been build in different ways, so
127+
/// using this function is often preferred.
128+
///
129+
/// ## Examples
130+
///
131+
/// ```
132+
/// from_strings(["a", "b"]) == new("ab") == False
133+
/// is_equal(from_strings(["a", "b"]), new("ab")) == True
134+
/// ```
98135
///
99136
pub external fn is_equal(Iodata, Iodata) -> Bool = "string" "equal"
100137

101-
/// Prepends the string `prefix` to `to`
138+
/// Inspect some iodata to determine if it is equivalent to an empty string.
139+
///
140+
/// ## Examples
141+
///
142+
/// ```
143+
/// new("ok") |> is_empty == False
144+
/// new("") |> is_empty == True
145+
/// from_strings([]) |> is_empty == True
146+
/// ```
102147
///
103148
pub external fn is_empty(Iodata) -> Bool = "string" "is_empty"

0 commit comments

Comments
 (0)