-
Notifications
You must be signed in to change notification settings - Fork 59
Add Functor and Applicative #113
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
Closed
Closed
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 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,37 @@ | ||
use kind::Kind; | ||
use functor::Functor; | ||
|
||
pub trait Applicative<B, F>: Functor<B> + Kind<F> + Sized | ||
where F: Fn(Self::Item) -> B { | ||
fn pure_(Self::Item) -> Self; | ||
fn apply(f: <Self as Kind<F>>::Make, Self) -> <Self as Kind<B>>::Make; | ||
} | ||
|
||
impl<A, B, F> Applicative<B, F> for Option<A> | ||
where F: Fn(A) -> B { | ||
fn pure_(a: A) -> Option<A> { | ||
Some(a) | ||
} | ||
|
||
fn apply(f: Option<F>, to: Option<A>) -> Option<B> { | ||
match f { | ||
Some(f) => to.map(f), | ||
None => None, | ||
} | ||
} | ||
} | ||
|
||
impl<A: Clone, B, F> Applicative<B, F> for Vec<A> | ||
where F: Fn(A) -> B { | ||
fn pure_(a: A) -> Vec<A> { | ||
vec![a] | ||
} | ||
|
||
fn apply(f: Vec<F>, to: Vec<A>) -> Vec<B> { | ||
f.into_iter().flat_map(|f| { | ||
to.clone().into_iter().map(|to| { | ||
f(to) | ||
}).collect::<Vec<B>>() | ||
}).collect() | ||
} | ||
} |
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,27 @@ | ||
use kind::Kind; | ||
|
||
pub trait Functor<B>: Kind<B> { | ||
type Item; | ||
fn fmap<F>(self, f: F) -> <Self as Kind<B>>::Make where F: Fn(Self::Item) -> B; | ||
} | ||
|
||
impl<A, B> Functor<B> for Option<A> { | ||
type Item = A; | ||
fn fmap<F>(self, f: F) -> Option<B> where F: Fn(A) -> B { | ||
self.map(f) | ||
} | ||
} | ||
|
||
impl<A, B, E> Functor<B> for Result<A, E> { | ||
type Item = A; | ||
fn fmap<F>(self, f: F) -> Result<B, E> where F: Fn(A) -> B { | ||
self.map(f) | ||
} | ||
} | ||
|
||
impl<A, B> Functor<B> for Vec<A> { | ||
type Item = A; | ||
fn fmap<F>(self, f: F) -> Vec<B> where F: Fn(A) -> B{ | ||
self.into_iter().map(f).collect() | ||
} | ||
} |
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,15 @@ | ||
pub trait Kind<T> { | ||
type Make; | ||
} | ||
|
||
impl<A, B> Kind<B> for Option<A> { | ||
type Make = Option<B>; | ||
} | ||
|
||
impl<A, B, E> Kind<B> for Result<A, E> { | ||
type Make = Result<B, E>; | ||
} | ||
|
||
impl<A, B> Kind<B> for Vec<A> { | ||
type Make = Vec<B>; | ||
} |
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
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.
yuck on the hack for avoiding the keyword. I might suggest looking to other langauges for naming precedent. (I know a lot of JS libs call this
of
; I don't know about other languages)ISTM you are also going to immediately run into problems with type inference when you try to use this method, because its signature does not constrain
F
.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've also seen
just
being used in the wild.Uh oh!
There was an error while loading. Please reload this page.
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.
Some "insider info":
pure
will likely be removed as a keyword soon-ish.For now, I'm partial to
pointed
orwrap
.