aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gleam/io.gleam70
-rw-r--r--src/gleam_stdlib.erl6
-rw-r--r--src/gleam_stdlib.mjs20
3 files changed, 41 insertions, 55 deletions
diff --git a/src/gleam/io.gleam b/src/gleam/io.gleam
index f9b093b..558de94 100644
--- a/src/gleam/io.gleam
+++ b/src/gleam/io.gleam
@@ -33,23 +33,23 @@ if javascript {
/// ## Example
///
/// ```
-/// > io.eprint("Hi pop")
+/// > io.print_error("Hi pop")
/// // -> Hi pop
/// Nil
/// ```
///
-pub fn eprint(string: String) -> Nil {
- do_eprint(string)
+pub fn print_error(string: String) -> Nil {
+ do_print_error(string)
}
if erlang {
- external fn do_eprint(string: String) -> Nil =
- "gleam_stdlib" "eprint"
+ external fn do_print_error(string: String) -> Nil =
+ "gleam_stdlib" "print_error"
}
if javascript {
- external fn do_eprint(String) -> Nil =
- "../gleam_stdlib.mjs" "eprint"
+ external fn do_print_error(String) -> Nil =
+ "../gleam_stdlib.mjs" "print_error"
}
/// Writes a string to standard output, appending a newline to the end.
@@ -73,7 +73,7 @@ if erlang {
if javascript {
external fn do_println(String) -> Nil =
- "../gleam_stdlib.mjs" "log"
+ "../gleam_stdlib.mjs" "console_log"
}
/// Writes a string to standard error, appending a newline to the end.
@@ -81,26 +81,26 @@ if javascript {
/// ## Example
///
/// ```gleam
-/// > io.eprintln("Hi pop")
+/// > io.println_error("Hi pop")
/// // -> Hi mum
/// Nil
/// ```
///
-pub fn eprintln(string: String) -> Nil {
- do_eprintln(string)
+pub fn println_error(string: String) -> Nil {
+ do_println_error(string)
}
if erlang {
- external fn do_eprintln(string: String) -> Nil =
- "gleam_stdlib" "eprintln"
+ external fn do_println_error(string: String) -> Nil =
+ "gleam_stdlib" "println_error"
}
if javascript {
- external fn do_eprintln(String) -> Nil =
- "../gleam_stdlib.mjs" "error"
+ external fn do_println_error(String) -> Nil =
+ "../gleam_stdlib.mjs" "console_error"
}
-/// Prints a value to standard output (stdout) yielding Gleam syntax.
+/// Prints a value to standard error (stderr) yielding Gleam syntax.
///
/// The value is returned after being printed so it can be used in pipelines.
///
@@ -131,39 +131,17 @@ if javascript {
pub fn debug(term: anything) -> anything {
term
|> string.inspect
- |> println
+ |> do_debug_println
term
}
-/// Prints a value to standard error (stderr) yielding Gleam syntax.
-///
-/// The value is returned after being printed so it can be used in pipelines.
-///
-/// ## Example
-///
-/// ```gleam
-/// > io.edebug("Hi pop")
-/// // -> <<"Hi pop">>
-/// "Hi pop"
-///
-/// > io.edebug(Ok(1))
-/// // -> {ok, 1}
-/// Ok(1)
-///
-/// > import list
-/// > [1, 2]
-/// > |> list.map(fn(x) { x + 1 })
-/// > |> io.edebug
-/// > |> list.map(fn(x) { x * 2 })
-/// // -> [2, 3]
-/// [4, 6]
-/// ```
-///
-pub fn edebug(term: anything) -> anything {
- term
- |> string.inspect
- |> eprintln
+if erlang {
+ external fn do_debug_println(string: String) -> Nil =
+ "gleam_stdlib" "println_error"
+}
- term
+if javascript {
+ external fn do_debug_println(String) -> Nil =
+ "../gleam_stdlib.mjs" "print_debug"
}
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 1ed963c..39721f1 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -10,7 +10,7 @@
percent_encode/1, percent_decode/1, regex_check/2, regex_split/2,
base_decode64/1, parse_query/1, bit_string_concat/1, size_of_tuple/1,
decode_tuple/1, tuple_get/2, classify_dynamic/1, print/1, println/1,
- eprint/1, eprintln/1, inspect/1, float_to_string/1]).
+ print_error/1, println_error/1, inspect/1, float_to_string/1]).
%% Taken from OTP's uri_string module
-define(DEC2HEX(X),
@@ -319,11 +319,11 @@ println(String) ->
io:put_chars([String, $\n]),
nil.
-eprint(String) ->
+print_error(String) ->
io:put_chars(standard_error, String),
nil.
-eprintln(String) ->
+println_error(String) ->
io:put_chars(standard_error, [String, $\n]),
nil.
diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs
index 8a2d88f..a3e6c13 100644
--- a/src/gleam_stdlib.mjs
+++ b/src/gleam_stdlib.mjs
@@ -95,7 +95,7 @@ export function string_length(string) {
export function graphemes(string) {
return List.fromArray(
- Array.from(graphemes_iterator(string)).map(((item) => item.segment ))
+ Array.from(graphemes_iterator(string)).map((item) => item.segment)
);
}
@@ -199,12 +199,12 @@ export function bit_string_concat(bit_strings) {
return toBitString(bit_strings.toArray().map((b) => b.buffer));
}
-export function log(term) {
+export function console_log(term) {
console.log(term);
}
-export function debug(term) {
- console.log(inspect(term));
+export function console_error(term) {
+ console.error(term);
}
export function crash(message) {
@@ -228,7 +228,7 @@ export function print(string) {
}
}
-export function eprint(string) {
+export function print_error(string) {
if (typeof process === "object") {
process.stderr.write(string); // We can write without a trailing newline
} else {
@@ -236,6 +236,14 @@ export function eprint(string) {
}
}
+export function print_debug(string) {
+ if (typeof process === "object") {
+ process.stderr.write(string + "\n"); // If we're in Node.js, use `stderr`
+ } else {
+ console.log(string); // Otherwise, use `console.log` (so that it doesn't look like an error)
+ }
+}
+
export function ceiling(float) {
return Math.ceil(float);
}
@@ -259,7 +267,7 @@ export function power(base, exponent) {
// the result will essentially be division by zero).
// It can thus be assumed that valid input is passed to the Math.pow
// function and a NaN or Infinity value will not be produced.
- return Math.pow(base, exponent)
+ return Math.pow(base, exponent);
}
export function random_uniform() {