Skip to content

Commit

Permalink
Add infix operators for mapOk/flatMapOk (#27)
Browse files Browse the repository at this point in the history
* Add infix operators for mapOk/flatMapOk

* Fix infix operators and use in tests

* Add test for infix operations

* Pretty up >>= test

* Remove temp code used to fail tests
  • Loading branch information
johnhaley81 authored and gilbert committed Nov 29, 2018
1 parent c2b0899 commit 44ed243
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Future.re
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@ let tapError = (future, f) => future |. tap(r => switch(r) {
| Belt.Result.Error(v) => f(v) |. ignore
| Ok(_) => ()
});

let (>>=) = flatMapOk;
let (<$>) = mapOk;
39 changes: 39 additions & 0 deletions tests/TestFuture.re
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ describe("Future", () => {


describe("Future Belt.Result", () => {
let ((>>=), (<$>)) = Future.((>>=), (<$>));

test("mapOk", () => {
Belt.Result.Ok("two")
Expand Down Expand Up @@ -262,4 +263,42 @@ describe("Future Belt.Result", () => {
});
});

test("<$>", () => {
Future.value(Belt.Result.Ok("infix ops"))
<$> ((++)(" "))
<$> ((++)("rock!"))
|. Future.get(r =>
r
|. Belt.Result.getExn
|. equals("infix ops rock!")
);

Future.value(Belt.Result.Error("infix ops"))
<$> ((++)(" "))
<$> ((++)("suck!"))
|. Future.get(r => switch(r) {
| Ok(_) => raise(TestError("shouldn't be possible"))
| Error(e) => e |. equals("infix ops")
});
});

test(">>=", () => {
let appendToString = (appendedString, s) =>
(s ++ appendedString) |. Belt.Result.Ok |. Future.value;

Future.value(Belt.Result.Ok("infix ops"))
>>= appendToString(" still rock!")
|. Future.get(r =>
r
|. Belt.Result.getExn
|. equals("infix ops still rock!")
);

Future.value(Belt.Result.Error("infix ops"))
>>= appendToString(" still sucks!")
|. Future.get(r => switch (r) {
| Ok(_) => raise(TestError("shouldn't be possible"))
| Error(e) => e |. equals("infix ops");
});
});
});

0 comments on commit 44ed243

Please sign in to comment.