Skip to content

Commit

Permalink
refactor(fp-core): adding functor and HKT into fp-core
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonShin committed Aug 5, 2019
1 parent 783f5ed commit be7cc6a
Show file tree
Hide file tree
Showing 41 changed files with 72 additions and 46 deletions.
15 changes: 10 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
[package]
name = "functional-programming-jargon"
version = "0.1.0"
authors = ["Jason Shin <visualbbasic@gmail.com>"]
edition = "2018"
[workspace]

[dependencies]
partial_application = "0.1.0"
itertools = "0.8.0"
rats = "0.0.1"
members = [
"fp-examples",
"fp-core"
]
9 changes: 9 additions & 0 deletions fp-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "fp-core"
version = "0.1.0"
authors = ["Jason Shin <visualbbasic@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
16 changes: 16 additions & 0 deletions fp-core/src/functor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::hkt::HKT;

pub trait Functor<A, B>: HKT<A, B> {
fn fmap<F>(self, f: F) -> <Self as HKT<A, B>>::Target
where
F: FnOnce(A) -> B;
}

impl<A, B> Functor<A, B> for Option<A> {
fn fmap<F>(self, f: F) -> Self::Target
where
F: FnOnce(A) -> B,
{
self.map(f)
}
}
9 changes: 9 additions & 0 deletions fp-core/src/hkt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub trait HKT<A, B> {
type URI;
type Target;
}

impl<A, B> HKT<A, B> for Option<A> {
type URI = Self;
type Target = Option<B>;
}
2 changes: 2 additions & 0 deletions fp-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod functor;
pub mod hkt;
13 changes: 13 additions & 0 deletions fp-examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "fp-examples"
version = "0.1.0"
authors = ["Jason Shin <visualbbasic@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
partial_application = "0.1.0"
itertools = "0.8.0"
rats = "0.0.1"
fp-core = { path = "../fp-core" }
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::functor_example::{Functor, HKT};
use fp_core::functor::Functor;
use fp_core::hkt::HKT;

// HKT 3
pub trait HKT3<A, B, C> {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::functor_example::{Functor, HKT};
use fp_core::functor::Functor;
use fp_core::hkt::HKT;

trait Extend<A, B>: Functor<A, B> + Sized {
fn extend<W>(self, f: W) -> <Self as HKT<A, B>>::Target
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 1 addition & 24 deletions src/functor_example.rs → fp-examples/src/functor_example.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,4 @@
pub trait HKT<A, B> {
type URI;
type Target;
}

pub trait Functor<A, B>: HKT<A, B> {
fn fmap<F>(self, f: F) -> <Self as HKT<A, B>>::Target
where
F: FnOnce(A) -> B;
}

impl<A, B> HKT<A, B> for Option<A> {
type URI = Self;
type Target = Option<B>;
}

impl<A, B> Functor<A, B> for Option<A> {
fn fmap<F>(self, f: F) -> Self::Target
where
F: FnOnce(A) -> B,
{
self.map(f)
}
}
use fp_core::functor::Functor;

// impl<A, B, T> HKT<A, B> for T
// where
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 2 additions & 5 deletions src/main.rs → fp-examples/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#[macro_use]
extern crate partial_application;
extern crate fp_core;
extern crate rats;

mod adt_example;
pub mod adt_example;
mod anamorphism_example;
mod applicative_example;
mod arity_example;
Expand Down Expand Up @@ -33,7 +34,3 @@ mod semigroup_example;
mod setoid_example;
mod side_effects_example;
mod type_signature_example;

fn main() {
println!("Hello, PureRust!");
}
2 changes: 1 addition & 1 deletion src/monad_example.rs → fp-examples/src/monad_example.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::applicative_example::{Applicative, Pure};
use crate::functor_example::HKT;
use fp_core::hkt::HKT;

pub trait Chain<A, B>: HKT<A, B> {
fn chain<F>(self, f: F) -> <Self as HKT<A, B>>::Target
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit be7cc6a

Please sign in to comment.