Skip to content

Commit

Permalink
drop monadic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario T. Lanza committed May 26, 2024
1 parent def743f commit 99fdb34
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
20 changes: 0 additions & 20 deletions src/core/monadic.js

This file was deleted.

15 changes: 12 additions & 3 deletions src/core/types/just/behave.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {implement} from "../protocol.js";
import {identity, does} from "../../core.js";
import {IOtherwise} from "../../protocols.js";
import {IOtherwise, IFunctor, IFlatMappable, IDeref} from "../../protocols.js";
import {maybe, Just} from "./construct.js";
import monadic from "../../monadic.js";
import {keying} from "../../protocols/imapentry/concrete.js";

function otherwise(self){
Expand All @@ -13,7 +12,17 @@ function flat(self){
return self.value instanceof Just ? self.value : self;
}

function fmap(self, f){
return maybe(f(self.value));
}

function deref(self){
return self.value;
}

export default does(
keying("Just"),
monadic(maybe, flat),
implement(IFunctor, {fmap}),
implement(IFlatMappable, {flat}),
implement(IDeref, {deref}),
implement(IOtherwise, {otherwise}));
14 changes: 8 additions & 6 deletions src/core/types/left/behave.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {implement, satisfies} from "../protocol.js";
import {identity, does} from "../../core.js";
import {IFunctor, IForkable, IDeref} from "../../protocols.js";
import {IFunctor, IFlatMappable, IForkable, IDeref} from "../../protocols.js";
import {keying} from "../../protocols/imapentry/concrete.js";
import {left} from "./construct.js";
import monadic from "../../monadic.js";

const fmap = identity;
const flat = identity;

function flat(self){
return self.value instanceof Right || self.value instanceof Left ? self.value : self;
}

function fork(self, reject, resolve){
reject(self.value);
Expand All @@ -18,7 +20,7 @@ function deref(self){

export default does(
keying("Left"),
monadic(left, flat),
implement(IDeref, {deref}),
implement(IForkable, {fork}),
implement(IFunctor, {fmap}));
implement(IFunctor, {fmap}),
implement(IFlatMappable, {flat}),
implement(IForkable, {fork}));
23 changes: 18 additions & 5 deletions src/core/types/right/behave.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {implement} from "../protocol.js";
import {identity, does} from "../../core.js";
import {IFunctor, IOtherwise, IForkable, IDeref} from "../../protocols.js";
import {IFunctor, IFlatMappable, IOtherwise, IForkable, IDeref} from "../../protocols.js";
import * as p from "../../protocols/concrete.js";
import {right, Right} from "./construct.js";
import monadic from "../../monadic.js";
import {result, right, Right} from "./construct.js";
import {keying} from "../../protocols/imapentry/concrete.js";
import {Left} from "../left/construct.js";
import {left, Left} from "../left/construct.js";

function flat(self){
return self.value instanceof Right || self.value instanceof Left ? self.value : self;
Expand All @@ -19,8 +18,22 @@ function fork(self, reject, resolve){
resolve(self.value);
}

function fmap(self, f){
try {
return result(f(self.value));
} catch (error) {
return left(error);
}
}

function deref(self){
return self.value;
}

export default does(
keying("Right"),
monadic(right, flat),
implement(IDeref, {deref}),
implement(IFunctor, {fmap}),
implement(IFlatMappable, {flat}),
implement(IForkable, {fork}),
implement(IOtherwise, {otherwise}));
5 changes: 5 additions & 0 deletions src/core/types/right/construct.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {constructs} from "../../core.js";
import {thrush} from "../../protocols/ifunctor/concrete.js";
import {left} from "../left/construct.js";

export function Right(value){
this.value = value;
Expand All @@ -8,3 +9,7 @@ export function Right(value){
Right.prototype[Symbol.toStringTag] = "Right";

export const right = thrush(constructs(Right));

export function result(value){
return value instanceof Error ? left(value) : right(value);
}

0 comments on commit 99fdb34

Please sign in to comment.