-
Notifications
You must be signed in to change notification settings - Fork 5
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
basic result type and functions #3
Open
tsloughter
wants to merge
1
commit into
alpaca-lang:master
Choose a base branch
from
tsloughter:result-type
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.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
_build | ||
_checkouts | ||
.rebar3 | ||
rebar3.crashdump | ||
rebar.lock |
This file contains 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,54 @@ | ||
{- Basic result type. | ||
-} | ||
module result | ||
|
||
import_type option.option | ||
|
||
export isOk/1, map/1, fromOption/2, andThen/2, withDefault/2 | ||
|
||
export_type result | ||
|
||
type result 'a 'b = Ok 'a | Err 'b | ||
|
||
let isOk r = | ||
match r with | ||
Ok _ -> true | ||
| Err _ -> false | ||
|
||
test "Applying `isOk` to Ok x should return true" = | ||
assert.equal (true) (isOk (Ok 1)) | ||
|
||
let map f (Ok x) = Ok (f x) | ||
|
||
let map _ (Err x) = Err x | ||
|
||
test "mapping the identity function to Ok x should return Ok x" = | ||
let f x = x in | ||
assert.equal (Ok 1) (map f (Ok 1)) | ||
|
||
let fromOption opt e = | ||
match opt with | ||
Some x -> Ok x | ||
| None -> Err e | ||
|
||
let andThen callback (Ok value) = callback value | ||
let andThen _ (Err msg) = Err msg | ||
|
||
test "andThen test with Ok x" = | ||
let f x = (Ok x) in | ||
assert.equal (Ok 1) (andThen f (Ok 1)) | ||
|
||
test "andThen with Err x and identity function is Err x" = | ||
let f x = (Ok x) in | ||
assert.equal (Err "msg") (andThen f (Err "msg")) | ||
|
||
let withDefault _ (Ok x) = x | ||
let withDefault default (Err _) = default | ||
|
||
test "withdefault test with Ok result" = | ||
let f x = (Ok x) in | ||
assert.equal (1) (withDefault 0 (f 1)) | ||
|
||
test "withdefault test with Err returning default" = | ||
let f _ = (Err "error") in | ||
assert.equal (0) (withDefault 0 (f 1)) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we are using camelCase while everywhere else we are using snake_case. Could we be consistent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have we decided which case we prefer? I'm leaning towards camelCase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iirc @lepoetemaudit and I discussed this mid-way through last year and settled on moving to camelCase. I'm easy either way tbh. Other opinions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fab, I'll update the other modules/PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For option this is called
flatMap
. Which name do we prefer? I tookflatMap
from Scala as we hadoption
which felt Scala-y to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer
andThen
personally, but would also change it inoption
, hehe. So if it is staying that way in option then to be consistent I'll change it here.Maybe in scala the option and result types are actually related to sequences and so a
flatMap
makes sense? To me it doesn't fit in the alpaca case and would be confusing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the result of composing
flatten : m m a -> m a
andmap
, so I think it makes sense.I find the name
andThen
a little odd, doesn't seem to fit withmap
,fold
etc