- Notifications
You must be signed in to change notification settings - Fork471
Implement and use %option_for_each primitive for Option.each#7799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
mediremi wants to merge1 commit intorescript-lang:masterChoose a base branch frommediremi:optimise-option-for-each
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletionscompiler/core/js_of_lam_option.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -106,3 +106,12 @@ let null_to_opt e = E.econd (E.is_null e) none (some e) | ||
| let undef_to_opt e = E.econd (E.is_undef e) none (some e) | ||
| let null_undef_to_opt e = E.econd (E.is_null_undefined e) none (some e) | ||
| let option_for_each opt f = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Alternative implementation: E.econd (is_not_none opt) (E.call~info:(Js_call_info.na_full_callfalse) f [opt])E.unit | ||
| destruct_optional opt ~for_sure_none:E.unit | ||
| ~for_sure_some:(fun x -> | ||
| E.call ~info:(Js_call_info.na_full_call false) f [x]) | ||
| ~not_sure:(fun () -> | ||
| E.econd (is_not_none opt) | ||
| (E.call ~info:(Js_call_info.na_full_call false) f [opt]) | ||
| E.unit) | ||
2 changes: 2 additions & 0 deletionscompiler/core/js_of_lam_option.mli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletioncompiler/core/lam_analysis.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletionscompiler/core/lam_compile_primitive.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionscompiler/core/lam_convert.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
13 changes: 7 additions & 6 deletionscompiler/core/lam_primitive.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionscompiler/core/lam_primitive.mli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionscompiler/core/lam_print.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionscompiler/ml/lambda.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionscompiler/ml/lambda.mli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionscompiler/ml/printlambda.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionscompiler/ml/translcore.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletionslib/es6/Stdlib_Option.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletionslib/js/Stdlib_Option.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
6 changes: 1 addition & 5 deletionsruntime/Stdlib_Option.res
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletionruntime/Stdlib_Option.resi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletionstests/tests/src/OptionForEachTest.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Generated by ReScript, PLEASE EDIT WITH CARE | ||
| function testSome(opt) { | ||
| if (opt !== undefined) { | ||
| console.log(opt); | ||
| return; | ||
| } | ||
| } | ||
| function testNone() { | ||
| } | ||
| function testWithSideEffect() { | ||
| let counter = { | ||
| contents: 0 | ||
| }; | ||
| (param => { | ||
| counter.contents = counter.contents + 1 | 0; | ||
| })(42); | ||
| } | ||
| function testDirect(opt) { | ||
| if (opt !== undefined) { | ||
| console.log(opt); | ||
| return; | ||
| } | ||
| } | ||
| export { | ||
| testSome, | ||
| testNone, | ||
| testWithSideEffect, | ||
| testDirect, | ||
| } | ||
| /* No side effect */ |
20 changes: 20 additions & 0 deletionstests/tests/src/OptionForEachTest.res
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| let testSome = (opt: option<int>) => { | ||
| opt->Option.forEach(x => Js.log(x)) | ||
| } | ||
| let testNone = () => { | ||
| let opt: option<string> = None | ||
| opt->Option.forEach(x => Js.log(x)) | ||
| } | ||
| let testWithSideEffect = () => { | ||
| let counter = ref(0) | ||
| Some(42)->Option.forEach(_ => counter := counter.contents + 1) | ||
| None->Option.forEach(_ => counter := counter.contents + 1) | ||
| } | ||
| external directPrimitive: (option<'a>, 'a => unit) => unit = "%option_for_each" | ||
| let testDirect = (opt: option<float>) => { | ||
| directPrimitive(opt, x => Js.log(x)) | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.