Skip to content

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/applicative.rs
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;
Copy link
Collaborator

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.

Copy link
Owner

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.

Copy link
Collaborator

@Centril Centril Apr 9, 2018

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 or wrap.

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()
}
}
27 changes: 27 additions & 0 deletions src/functor.rs
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()
}
}
15 changes: 15 additions & 0 deletions src/kind.rs
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>;
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ extern crate frunk_derives;
pub mod semigroup;
pub mod monoid;
pub mod validated;
pub mod kind;
pub mod functor;
pub mod applicative;

pub use frunk_core::*;
pub use frunk_derives::*;
Expand Down