Skip to content

Commit

Permalink
Reduce code size of allOk* functions
Browse files Browse the repository at this point in the history
Resolves #57.
  • Loading branch information
aantron committed Jun 21, 2020
1 parent 94e76e0 commit 7b7656c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ In addition:
implementation][native], which passes all the same tests. It can be used for
native code or in JS.

[bundle-size]: https://travis-ci.org/aantron/promise/jobs/638748670#L241
[bundle-size]: https://travis-ci.org/github/aantron/promise/jobs/700562910#L210
[native]: https://github.com/aantron/promise/tree/master/src/native

<br>
Expand Down
69 changes: 13 additions & 56 deletions src/js/promise.re
Original file line number Diff line number Diff line change
Expand Up @@ -311,66 +311,23 @@ let allOkArray = promises => {
let allOk = promises =>
mapOk(allOkArray(Belt.List.toArray(promises)), Belt.List.fromArray);

let allOk2 = (p1, p2) => {
let promises = [|Obj.magic(p1), Obj.magic(p2)|];
mapOk(allOkArray(promises), fun
| [|v1, v2|] => (Obj.magic(v1), Obj.magic(v2))
| _ => assert(false))
};
let unsafeAllOkArray =
Obj.magic(allOkArray);

let allOk3 = (p1, p2, p3) => {
let promises = [|Obj.magic(p1), Obj.magic(p2), Obj.magic(p3)|];
mapOk(allOkArray(promises), fun
| [|v1, v2, v3|] => (Obj.magic(v1), Obj.magic(v2), Obj.magic(v3))
| _ => assert(false))
};
let allOk2 = (p1, p2) =>
unsafeAllOkArray((p1, p2));

let allOk4 = (p1, p2, p3, p4) => {
let promises = [|Obj.magic(p1), Obj.magic(p2), Obj.magic(p3), Obj.magic(p4)|];
mapOk(allOkArray(promises), fun
| [|v1, v2, v3, v4|] =>
(Obj.magic(v1), Obj.magic(v2), Obj.magic(v3), Obj.magic(v4))
| _ =>
assert(false))
};
let allOk3 = (p1, p2, p3) =>
unsafeAllOkArray((p1, p2, p3));

let allOk5 = (p1, p2, p3, p4, p5) => {
let promises = [|
Obj.magic(p1),
Obj.magic(p2),
Obj.magic(p3),
Obj.magic(p4),
Obj.magic(p5)
|];
mapOk(allOkArray(promises), fun
| [|v1, v2, v3, v4, v5|] =>
(Obj.magic(v1), Obj.magic(v2), Obj.magic(v3), Obj.magic(v4), Obj.magic(v5))
| _ =>
assert(false))
};
let allOk4 = (p1, p2, p3, p4) =>
unsafeAllOkArray((p1, p2, p3, p4));

let allOk6 = (p1, p2, p3, p4, p5, p6) => {
let promises = [|
Obj.magic(p1),
Obj.magic(p2),
Obj.magic(p3),
Obj.magic(p4),
Obj.magic(p5),
Obj.magic(p6)
|];
mapOk(allOkArray(promises), fun
| [|v1, v2, v3, v4, v5, v6|] =>
(
Obj.magic(v1),
Obj.magic(v2),
Obj.magic(v3),
Obj.magic(v4),
Obj.magic(v5),
Obj.magic(v6)
)
| _ =>
assert(false))
};
let allOk5 = (p1, p2, p3, p4, p5) =>
unsafeAllOkArray((p1, p2, p3, p4, p5));

let allOk6 = (p1, p2, p3, p4, p5, p6) =>
unsafeAllOkArray((p1, p2, p3, p4, p5, p6));

module Operators = {
let (>|=) = mapOk;
Expand Down
2 changes: 1 addition & 1 deletion test/bundle/size.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ gzip -9f test/bundle/uses_promise.js
CONTROL=`stat -c '%s' test/bundle/control.js.gz`
PROMISE=`stat -c '%s' test/bundle/uses_promise.js.gz`
DIFFERENCE=`expr $PROMISE - $CONTROL`
LIMIT=1024
LIMIT=1152

if [ $DIFFERENCE -gt $LIMIT ]
then
Expand Down

0 comments on commit 7b7656c

Please sign in to comment.