Skip to content

Commit bac2d26

Browse files
committed
io.{prinln, debug}
1 parent ddd8960 commit bac2d26

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Unreleased
44

55
- Created the `set` module with the `new`, `insert`, and `contains` functions.
6-
- Created the `io` module with the `print` function.
6+
- Created the `io` module with the `print`, `println`, and `debug` functions.
77
- Created the `queue` module with the `new`, `from_list`, `to_list`,
88
`is_empty`, `length`, `push_back`, `push_front`, `pop_back`, `pop_front`,
99
`reverse`, `is_logically_equal`, and `is_equal` functions.
@@ -18,7 +18,8 @@
1818
`pop_grapheme` and `to_graphemes' functions.
1919
- `uri` module created with `parse`, `parse_query`, `path_segments`,
2020
`query_to_string` and `to_string`.
21-
- The `dynamic` module gains the `map`, `opaque_list`, `tuple2`, and `tuple2_of` functions.
21+
- The `dynamic` module gains the `map`, `opaque_list`, `tuple2`, and
22+
`tuple2_of` functions.
2223
- The `list` module gains the `filter_map` function.
2324
- The `list.contains` label `has` has been changed to `any`.
2425

src/gleam/io.gleam

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,57 @@
11
external type DoNotLeak
22

3-
external fn erl_print(String) -> DoNotLeak =
3+
external fn erl_print(String, List(a)) -> DoNotLeak =
44
"io" "fwrite"
55

66
/// Writes a string to standard output.
77
///
88
/// ## Example
99
///
1010
/// > io.print("Hi mum")
11+
/// // -> Hi mum
1112
/// Nil
12-
/// //=> Hi mum
13-
///
13+
///
1414
pub fn print(string: String) -> Nil {
15-
erl_print(string)
15+
erl_print(string, [])
16+
Nil
17+
}
18+
19+
/// Writes a string to standard output, appending a newline to the end.
20+
///
21+
/// ## Example
22+
///
23+
/// > io.println("Hi mum")
24+
/// // -> Hi mum
25+
/// Nil
26+
///
27+
pub fn println(string: String) -> Nil {
28+
erl_print("~ts\n", [string])
29+
Nil
30+
}
31+
32+
/// Print a value to standard output using Erlang syntax.
33+
///
34+
/// The value is returned after being printed so it can be used in pipelines.
35+
///
36+
/// ## Example
37+
///
38+
/// > io.debug("Hi mum")
39+
/// // -> <<"Hi mum">>
40+
/// "Hi mum"
41+
///
42+
/// > io.debug(Ok(1))
43+
/// // -> {ok, 1}
44+
/// Ok(1)
45+
///
46+
/// > import list
47+
/// > [1, 2]
48+
/// > |> list.map(fn(x) { x + 1 })
49+
/// > |> io.debug
50+
/// > |> list.map(fn(x) { x * 2 })
51+
/// // -> [2, 3]
52+
/// [4, 6]
53+
///
54+
pub fn debug(term: anything) -> Nil {
55+
erl_print("~tp\n", [term])
1656
Nil
1757
}

0 commit comments

Comments
 (0)