-
Notifications
You must be signed in to change notification settings - Fork 29
Option additions #57
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Option additions #57
Changes from all commits
e300c0f
2c1d16b
90483c8
f61e47e
79c8086
be9897e
de8a86f
5c06440
a056cd0
29b3d5d
8b198d5
9280051
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,73 +22,83 @@ | |
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | ||
|
||
let flat = opt => | ||
switch opt { | ||
| Some(v) => v | ||
| None => None | ||
} | ||
|
||
let filterU = (opt, p) => | ||
switch opt { | ||
| Some(x) as some if p(. x) => some | ||
| Some(value) as some if p(. value) => some | ||
| _ => None | ||
} | ||
|
||
let filter = (opt, p) => filterU(opt, (. x) => p(x)) | ||
let filter = (opt, p) => filterU(opt, (. value) => p(value)) | ||
|
||
let forEachU = (opt, f) => | ||
switch opt { | ||
| Some(x) => f(. x) | ||
| Some(value) => f(. value) | ||
| None => () | ||
} | ||
|
||
let forEach = (opt, f) => forEachU(opt, (. x) => f(x)) | ||
let forEach = (opt, f) => forEachU(opt, (. value) => f(value)) | ||
|
||
let getExn = x => | ||
switch x { | ||
| Some(x) => x | ||
let getExn = opt => | ||
switch opt { | ||
| Some(value) => value | ||
| None => raise(Not_found) | ||
} | ||
|
||
let expect = (opt, message) => | ||
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. Not sure if we shouldn't rather leave the implementation of such a function to e.g. testing frameworks (which might also want to raise a different exception). 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. This isn't for testing. You should consider using this every time you reach for 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. One thing that comes to mind here - do we need an additional alternative where you can pass your own exception? I recall people in the community talking about adding that themselves. 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. What's the use case for that? To have a distinct exception that they can catch? 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. Yeah, exactly. But let's ignore it for now, feels peripheral. 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. Ok, right, so there is definitely demand in the community for something like this. And what do you think about the naming? Usually we have 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. I think that would ensure that absolutely no-one would pick this over 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.
Is there not an annotation that can be put on functions that raise? I thought reanalyze utilized that, but I can't find it now. 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. Naming this one is hard for sure... I'm trying to come up with a good alternative as well. I like @glennsl correct, it's 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. I logged an issue for Still haven't figured out a better name than |
||
switch opt { | ||
| Some(value) => value | ||
| None => Core__Error.panic(message) | ||
} | ||
|
||
external getUnsafe: option<'a> => 'a = "%identity" | ||
|
||
let mapWithDefaultU = (opt, default, f) => | ||
switch opt { | ||
| Some(x) => f(. x) | ||
| Some(value) => f(. value) | ||
| None => default | ||
} | ||
|
||
let mapWithDefault = (opt, default, f) => mapWithDefaultU(opt, default, (. x) => f(x)) | ||
let mapWithDefault = (opt, default, f) => mapWithDefaultU(opt, default, (. value) => f(value)) | ||
|
||
let mapU = (opt, f) => | ||
switch opt { | ||
| Some(x) => Some(f(. x)) | ||
| Some(value) => Some(f(. value)) | ||
| None => None | ||
} | ||
|
||
let map = (opt, f) => mapU(opt, (. x) => f(x)) | ||
let map = (opt, f) => mapU(opt, (. value) => f(value)) | ||
|
||
let flatMapU = (opt, f) => | ||
switch opt { | ||
| Some(x) => f(. x) | ||
| Some(value) => f(. value) | ||
| None => None | ||
} | ||
|
||
let flatMap = (opt, f) => flatMapU(opt, (. x) => f(x)) | ||
let flatMap = (opt, f) => flatMapU(opt, (. value) => f(value)) | ||
|
||
let getWithDefault = (opt, default) => | ||
switch opt { | ||
| Some(x) => x | ||
| Some(value) => value | ||
| None => default | ||
} | ||
|
||
let orElse = (opt, other) => | ||
let or = (opt, other) => | ||
glennsl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
switch opt { | ||
| Some(_) as some => some | ||
| Some(_) => opt | ||
| None => other | ||
} | ||
|
||
let isSome = x => | ||
switch x { | ||
| Some(_) => true | ||
| None => false | ||
} | ||
let orElse = or | ||
|
||
let isSome = opt => opt !== None | ||
glennsl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let isNone = x => x == None | ||
let isNone = opt => opt == None | ||
|
||
let eqU = (a, b, f) => | ||
switch a { | ||
|
Uh oh!
There was an error while loading. Please reload this page.