Skip to content

Commit 077aaf3

Browse files
committed
Int and float modules
1 parent afdabad commit 077aaf3

15 files changed

+180
-149
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
`unique`, and `zip` functions.
77
- `map_dict:Map` renamed to `map_dict:MapDict`.
88
- `map_dict` module gains `drop`, and `take` functions.
9-
- `str` module gains `append` function.
9+
- `str` module gains `append` function and loses `from_int`, `parse_int`,
10+
`from_float`, `parse_float`, and `base_from_int`.
11+
- `int` module created with `parse`, `to_string`, and `to_base_string`.
12+
- `float` module created with `parse`, and `to_string`.
1013

1114
## v0.1.1 - 2019-04-17
1215

gen/src/float.erl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-module(float).
2+
-compile(no_auto_import).
3+
4+
-export([parse/1, to_string/1]).
5+
6+
parse(A) ->
7+
gleam__stdlib:parse_float(A).
8+
9+
to_string(F) ->
10+
iodata:to_string(iodata:from_float(F)).

gen/src/int.erl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-module(int).
2+
-compile(no_auto_import).
3+
4+
-export([parse/1, to_string/1, to_base_string/2]).
5+
6+
parse(A) ->
7+
gleam__stdlib:parse_int(A).
8+
9+
to_string(A) ->
10+
erlang:integer_to_binary(A).
11+
12+
to_base_string(A, B) ->
13+
erlang:integer_to_binary(A, B).

gen/src/str.erl

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-module(str).
22
-compile(no_auto_import).
33

4-
-export([length/1, lowercase/1, uppercase/1, reverse/1, split/2, replace/3, append/2, from_int/1, parse_int/1, parse_float/1, base_from_int/2, from_float/1]).
4+
-export([length/1, lowercase/1, uppercase/1, reverse/1, split/2, replace/3, append/2]).
55

66
length(A) ->
77
string:length(A).
@@ -23,18 +23,3 @@ replace(String, Pattern, With) ->
2323

2424
append(S1, S2) ->
2525
iodata:to_string(iodata:append(iodata:new(S1), S2)).
26-
27-
from_int(A) ->
28-
erlang:integer_to_binary(A).
29-
30-
parse_int(A) ->
31-
gleam__stdlib:parse_int(A).
32-
33-
parse_float(A) ->
34-
gleam__stdlib:parse_float(A).
35-
36-
base_from_int(A, B) ->
37-
erlang:integer_to_binary(A, B).
38-
39-
from_float(F) ->
40-
iodata:to_string(iodata:from_float(F)).

gen/test/float_test.erl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-module(float_test).
2+
-compile(no_auto_import).
3+
4+
-export([parse_test/0, to_string_test/0]).
5+
6+
parse_test() ->
7+
expect:equal(float:parse(<<"1.23">>), {ok, 1.23}),
8+
expect:equal(float:parse(<<"5.0">>), {ok, 5.0}),
9+
expect:equal(float:parse(<<"0.123456789">>), {ok, 0.123456789}),
10+
expect:is_error(float:parse(<<"">>)),
11+
expect:is_error(float:parse(<<"what">>)),
12+
expect:is_error(float:parse(<<"1">>)).
13+
14+
to_string_test() ->
15+
expect:equal(float:to_string(123.0), <<"123.0">>),
16+
expect:equal(float:to_string(-8.1), <<"-8.1">>).

gen/test/int_test.erl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-module(int_test).
2+
-compile(no_auto_import).
3+
4+
-export([to_string/0, parse/0, to_base_string/0]).
5+
6+
to_string() ->
7+
expect:equal(int:to_string(123), <<"123">>),
8+
expect:equal(int:to_string(-123), <<"-123">>),
9+
expect:equal(int:to_string(123), <<"123">>).
10+
11+
parse() ->
12+
expect:equal(int:parse(<<"123">>), {ok, 123}),
13+
expect:equal(int:parse(<<"-123">>), {ok, -123}),
14+
expect:equal(int:parse(<<"0123">>), {ok, 123}),
15+
expect:is_error(int:parse(<<"">>)),
16+
expect:is_error(int:parse(<<"what">>)),
17+
expect:is_error(int:parse(<<"1.23">>)).
18+
19+
to_base_string() ->
20+
expect:equal(int:to_base_string(100, 16), <<"64">>),
21+
expect:equal(int:to_base_string(-100, 16), <<"-64">>).

gen/test/list_test.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,6 @@ sort_test() ->
136136
index_map_test() ->
137137
expect:equal(list:index_map([3, 4, 5], fun(I, X) -> {I, X} end),
138138
[{0, 3}, {1, 4}, {2, 5}]),
139-
F = fun(I, X) -> str:append(X, str:from_int(I)) end,
139+
F = fun(I, X) -> str:append(X, int:to_string(I)) end,
140140
expect:equal(list:index_map([<<"a">>, <<"b">>, <<"c">>], F),
141141
[<<"a0">>, <<"b1">>, <<"c2">>]).

gen/test/str_test.erl

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-module(str_test).
22
-compile(no_auto_import).
33

4-
-export([length_test/0, lowercase_test/0, uppercase_test/0, reverse_test/0, split_test/0, replace_test/0, append_test/0, from_int_test/0, parse_int_test/0, parse_float_test/0, base_from_int_test/0, from_float_test/0]).
4+
-export([length_test/0, lowercase_test/0, uppercase_test/0, reverse_test/0, split_test/0, replace_test/0, append_test/0]).
55

66
length_test() ->
77
expect:equal(str:length(<<"ß↑e̊">>), 3),
@@ -29,32 +29,3 @@ replace_test() ->
2929

3030
append_test() ->
3131
expect:equal(str:append(<<"Test">>, <<" Me">>), <<"Test Me">>).
32-
33-
from_int_test() ->
34-
expect:equal(str:from_int(123), <<"123">>),
35-
expect:equal(str:from_int(-123), <<"-123">>),
36-
expect:equal(str:from_int(123), <<"123">>).
37-
38-
parse_int_test() ->
39-
expect:equal(str:parse_int(<<"123">>), {ok, 123}),
40-
expect:equal(str:parse_int(<<"-123">>), {ok, -123}),
41-
expect:equal(str:parse_int(<<"0123">>), {ok, 123}),
42-
expect:is_error(str:parse_int(<<"">>)),
43-
expect:is_error(str:parse_int(<<"what">>)),
44-
expect:is_error(str:parse_int(<<"1.23">>)).
45-
46-
parse_float_test() ->
47-
expect:equal(str:parse_float(<<"1.23">>), {ok, 1.23}),
48-
expect:equal(str:parse_float(<<"5.0">>), {ok, 5.0}),
49-
expect:equal(str:parse_float(<<"0.123456789">>), {ok, 0.123456789}),
50-
expect:is_error(str:parse_float(<<"">>)),
51-
expect:is_error(str:parse_float(<<"what">>)),
52-
expect:is_error(str:parse_float(<<"1">>)).
53-
54-
base_from_int_test() ->
55-
expect:equal(str:base_from_int(100, 16), <<"64">>),
56-
expect:equal(str:base_from_int(-100, 16), <<"-64">>).
57-
58-
from_float_test() ->
59-
expect:equal(str:from_float(123.0), <<"123.0">>),
60-
expect:equal(str:from_float(-8.1), <<"-8.1">>).

src/float.gleam

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import iodata
2+
3+
pub enum NotAFloat =
4+
| NotAFloat
5+
6+
pub external fn parse(String) -> Result(Float, NotAFloat) =
7+
"gleam__stdlib" "parse_float";
8+
9+
pub fn to_string(f) {
10+
f
11+
|> iodata:from_float
12+
|> iodata:to_string
13+
}

src/int.gleam

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub enum NotAnInt =
2+
| NotAnInt
3+
4+
pub external fn parse(String) -> Result(Int, NotAnInt) = "gleam__stdlib" "parse_int";
5+
6+
pub external fn to_string(Int) -> String = "erlang" "integer_to_binary"
7+
8+
pub external fn to_base_string(Int, Int) -> String = "erlang" "integer_to_binary"

0 commit comments

Comments
 (0)