diff options
author | Louis Pilfold <louis@lpil.uk> | 2018-08-09 16:43:35 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2018-08-09 16:46:41 +0100 |
commit | 012273c4186317171f3405cc794ff9b3085c0e39 (patch) | |
tree | 838960bc358f3ba8fdb12c0f2a9bf493694e5017 /src | |
parent | 817d9006dd4aaa1793fee68089ef53f9e188af3f (diff) | |
download | gleam_stdlib-012273c4186317171f3405cc794ff9b3085c0e39.tar.gz gleam_stdlib-012273c4186317171f3405cc794ff9b3085c0e39.zip |
Unify function and closure syntax
I feel the `|x| x` syntax is less obvious than the `fn name(x) { x }`
syntax used by top level named functions. Using the same syntax
everywhere should be clearer.
Diffstat (limited to 'src')
-rw-r--r-- | src/Foreign.gleam | 4 | ||||
-rw-r--r-- | src/List.gleam | 50 | ||||
-rw-r--r-- | src/Maybe.gleam | 13 | ||||
-rw-r--r-- | src/Result.gleam | 39 |
4 files changed, 74 insertions, 32 deletions
diff --git a/src/Foreign.gleam b/src/Foreign.gleam index ccd71f4..1be59d0 100644 --- a/src/Foreign.gleam +++ b/src/Foreign.gleam @@ -12,7 +12,7 @@ external type Foreign doc """ Convert any Gleam data into Foreign data. """ -external new : |a| -> Foreign = :gleam.identity +external new : fn(a) { Foreign } = :gleam :identity doc """ Unsafely cast any type into any other type.o @@ -20,4 +20,4 @@ Unsafely cast any type into any other type.o This is an escape hatch for the type system that may be useful when wrapping native Erlang APIs. It is to be used as a last measure only. """ -external unsafeCoerce : |a| -> b = :gleam.identity +external unsafeCoerce : fn(a) { b } = :gleam :identity diff --git a/src/List.gleam b/src/List.gleam index 1ca291b..5f49866 100644 --- a/src/List.gleam +++ b/src/List.gleam @@ -6,7 +6,7 @@ import Maybe exposing Maybe(..) // Using the Erlang C BIF implementation. // -external length : |List(a)| -> Int = :erlang.length +external length : fn(List(a)) { Int } = :erlang :length test length { length([]) |> Assert.equal(_, 0) @@ -17,7 +17,7 @@ test length { // Using the Erlang C BIF implementation. // -external reverse : |List(a)| -> List(a) = :erlang.reverse +external reverse : fn(List(a)) { List(a) } = :erlang :reverse test reverse { length([]) |> Assert.equal(_, []) @@ -77,10 +77,18 @@ fn filter(list, fun) { } test filter { - filter([], |x| True) |> Assert.equal(_, []) - filter([0, 4, 5, 7, 3], |x| True) |> Assert.equal(_, [0, 4, 5, 7, 3]) - filter([0, 4, 5, 7, 3], |x| x > 4) |> Assert.equal(_, [5, 7]) - filter([0, 4, 5, 7, 3], |x| x < 4) |> Assert.equal(_, [0, 3]) + [] + |> filter(_, fn(x) { True }) + |> Assert.equal(_, []) + [0, 4, 5, 7, 3] + |> filter(_, fn(x) { True }) + |> Assert.equal(_, [0, 4, 5, 7, 3]) + [0, 4, 5, 7, 3] + |> filter(_, fn(x) { x > 4 }) + |> Assert.equal(_, [5, 7]) + [0, 4, 5, 7, 3] + |> filter(_, fn(x) { x < 4 }) + |> Assert.equal(_, [0, 3]) } fn filter(list, fun, acc) { @@ -101,8 +109,12 @@ fn map(list, fun) { } test map { - map([], |x| * 2) |> Assert.equal(_, []) - map([0, 4, 5, 7, 3], |x| x * 2) |> Assert.equal(_, [0, 8, 10, 14, 6]) + [] + |> map(_, fn(x) { x * 2 }) + |> Assert.equal(_, []) + [0, 4, 5, 7, 3] + |> map(_, fn(x) { x * 2 }) + |> Assert.equal(_, [0, 8, 10, 14, 6]) } fn map(list, fun, acc) { @@ -124,8 +136,12 @@ fn drop(list, n) { } test drop/2 { - drop([], 5) |> Assert.equal(_, []) - drop([1, 2, 3, 4, 5, 6, 7, 8], 5) |> Assert.equal(_, [6, 7, 8]) + [] + |> drop(_, 5) + |> Assert.equal(_, []) + [1, 2, 3, 4, 5, 6, 7, 8] + |> drop(_, 5) + |> Assert.equal(_, [6, 7, 8]) } fn take(list, n) { @@ -144,8 +160,12 @@ fn take(list, n, acc) { } test take { - take([], 5) |> Assert.equal(_, []) - take([1, 2, 3, 4, 5, 6, 7, 8], 5) |> Assert.equal(_, [1, 2, 3, 4, 5]) + [] + |> take(_, 5) + |> Assert.equal(_, []) + [1, 2, 3, 4, 5, 6, 7, 8] + |> take(_, 5) + |> Assert.equal(_, [1, 2, 3, 4, 5]) } fn of(x) { @@ -191,7 +211,8 @@ fn foldl(list, acc, fun) { } test foldl() { - foldl([1, 2, 3], [], |x, acc| x :: acc) + [1, 2, 3] + |> foldl(_, [], fn(x, acc) { x :: acc }) |> Assert.equal(_, [3, 2, 1]) } @@ -203,6 +224,7 @@ fn foldr(list, acc, fun) { } test foldr() { - foldr([1, 2, 3], [], |x, acc| x :: acc) + [1, 2, 3] + |> foldr(_, [], |x, acc| x :: acc) |> Assert.equal(_, [1, 2, 3]) } diff --git a/src/Maybe.gleam b/src/Maybe.gleam index c89f0e0..8ae6e7c 100644 --- a/src/Maybe.gleam +++ b/src/Maybe.gleam @@ -40,8 +40,8 @@ fn map(maybe, fun) { } test map() { - map(Just(1), |x| x + 1) |> Assert.equal(_, Just(2)) - map(Nothing, |x| x + 1) |> Assert.equal(Nothing) + map(Just(1), fn(x) { x + 1 }) |> Assert.equal(_, Just(2)) + map(Nothing, fn(x) { x + 1 }) |> Assert.equal(Nothing) } fn flatten(maybe) { @@ -62,9 +62,12 @@ fn flat_map(maybe, fun) { } test flat_map() { - flat_map(Nothing, |x| Just(x + 1)) |> Assert.equal(Nothing) - flat_map(Just(1), |x| Just(x + 1)) |> Assert.equal(Just(2)) - flat_map(Just(1), |_| Nothing) |> Assert.equal(Nothing) + flat_map(Nothing, fn(x) { Just(x + 1) }) + |> Assert.equal(_, Nothing) + flat_map(Just(1), fn(x) { Just(x + 1) }) + |> Assert.equal(_, Just(2)) + flat_map(Just(1), fn(_) { Nothing }) + |> Assert.equal(_, Nothing) } fn unwrap(maybe, fallback) { diff --git a/src/Result.gleam b/src/Result.gleam index 83f5651..3650e61 100644 --- a/src/Result.gleam +++ b/src/Result.gleam @@ -46,20 +46,28 @@ fn map(result, fun) { } test map { - map(Ok(1), |x| x + 1) |> Assert.equal(_, Ok(2)) - map(Error(1), |x| x + 1) |> Assert.equal(Error(1)) + Ok(1) + |> map(_, fn(x) { x + 1 }) + |> Assert.equal(_, Ok(2)) + Error(1) + |> map(_, fn(x) { x + 1 }) + |> Assert.equal(Error(1)) } fn map_error(result, fun) { case result { | Ok(_) => result - | Error(error) => error |> fun |> Error + | Error(error) => Error(fun(error)) } } test map_error { - map_error(Ok(1), |x| x + 1) |> Assert.equal(_, Ok(1)) - map_error(Error(1), |x| x + 1) |> Assert.equal(Error(2)) + Ok(1) + |> map_error(_, fn(x) { x + 1 }) + |> Assert.equal(_, Ok(1)) + Error(1) + |> map_error(_, fn(x) { x + 1 }) + |> Assert.equal(_, Error(2)) } fn flatten(result) { @@ -70,9 +78,12 @@ fn flatten(result) { } test flatten { - flatten(Ok(Ok(1))) |> Assert.equal(_, Ok(1)) - flatten(Ok(Error(1))) |> Assert.equal(_, Error(1)) - flatten(Error(1)) |> Assert.equal(_, Error(1)) + flatten(Ok(Ok(1))) + |> Assert.equal(_, Ok(1)) + flatten(Ok(Error(1))) + |> Assert.equal(_, Error(1)) + flatten(Error(1)) + |> Assert.equal(_, Error(1)) } fn flat_map(result, fun) { @@ -82,9 +93,15 @@ fn flat_map(result, fun) { } test flat_map { - flat_map(Error(1), |x| Ok(x + 1)) |> Assert.equal(_, Error(1)) - flat_map(Ok(1), |x| Ok(x + 1)) |> Assert.equal(_, Ok(2)) - flat_map(Ok(1), |_| Error(1)) |> Assert.equal(_, Error(1)) + Error(1) + |> flat_map(_, fn(x) { Ok(x + 1) }) + |> Assert.equal(_, Error(1)) + Ok(1) + |> flat_map(_, fn(x) { Ok(x + 1) }) + |> Assert.equal(_, Ok(2)) + Ok(1) + |> flat_map(_, fn(_) { Error(1) }) + |> Assert.equal(_, Error(1)) } fn unwrap(result, default) { |