Skip to content

Commit f4baa26

Browse files
author
Grant Wuerker
committed
monad
1 parent 680e3a9 commit f4baa26

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

Diff for: library/core/src/monad.fe

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use self::option::Option
2+
3+
trait Applicative
4+
where Self: * -> *
5+
{
6+
fn pure<T>(t: T) -> Self<T>
7+
}
8+
9+
impl Applicative for Option {
10+
fn pure<T>(t: T) -> Self<T> {
11+
Option::Some(t)
12+
}
13+
}
14+
15+
trait Monad: Applicative
16+
where Self: * -> *
17+
{
18+
fn bind<T, F, U>(self: Self<T>) -> Self<U> where F: Fn<T, Self<U>>
19+
}
20+
21+
impl Monad for Option {
22+
fn bind<T, F, U>(self: Self<T>) -> Self<U> where F: Fn<T, Self<U>> {
23+
match self {
24+
Self::Some(self) => F::exec(t: self)
25+
Self::None => Self::None
26+
}
27+
}
28+
}
29+
30+
trait Fn<T, U> {
31+
fn exec(t: T) -> U
32+
}
33+
34+
type MyFn = ()
35+
36+
impl Fn<bool, Option<u8>> for MyFn {
37+
fn exec(t: bool) -> Option<u8> {
38+
Option::Some(0)
39+
}
40+
}
41+
42+
#test
43+
fn my_test() {
44+
let a = Option::Some(true).bind<bool, MyFn, u8>()
45+
}

Diff for: library/core/src/monad/map.fe

Whitespace-only changes.

Diff for: library/core/src/monad/option.fe

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
enum Option<Inner> {
1+
pub enum Option<Inner> {
22
Some(Inner),
33
None
44
}

0 commit comments

Comments
 (0)