Skip to content

Commit

Permalink
refactor: move createMonadicSequence into the sequencePrototype modul…
Browse files Browse the repository at this point in the history
…e to resolve some cyclic/bidirectional dependency issues. This way we also make sure that whoever creates a sequence has all the functions on the prototype.
  • Loading branch information
Dierk Koenig committed Sep 8, 2023
1 parent 11d855c commit 49087b7
Show file tree
Hide file tree
Showing 23 changed files with 65 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Just, Nothing } from "../../../../kolibri/stdlib.js";
import { createMonadicSequence } from "../../../../kolibri/sequence/util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../../../kolibri/sequence/util/sequenceUtil/sequencePrototype.js";
import { choiceMaybe } from "../../../../kolibri/stdlib/stdlib.js";
import { uncurry } from "../../../../kolibri/lambda/church.js";
import { iteratorOf } from "../../../../kolibri/sequence/util/sequenceUtil/iteratorOf.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Sequence } from "../../../../kolibri/sequence/sequence.js"
import { iteratorOf } from "../../../../kolibri/sequence/util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../../../kolibri/sequence/util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../../../kolibri/sequence/util/sequenceUtil/sequencePrototype.js";

export { SquareNumberSequence }

Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/json/jsonMonad.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Just, Nothing } from "../stdlib/maybe.js";
import { iteratorOf } from "../sequence/util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../sequence/util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../sequence/util/sequenceUtil/sequencePrototype.js";
import {
PureSequence,
nil,
Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/constructors/range/rangeTest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Range, Walk} from "./range.js";
import { TestSuite } from "../../../util/test.js";
import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";
import { addToTestingTable, TESTS } from "../../util/testingTable.js";
import { createTestConfig } from "../../util/testUtil.js";

Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/constructors/seq/seq.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";

export { Seq }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pop, emptyStack, stackEquals } from "../../../../../../contrib/p6_brodwolf_andermatt/src/stack/stack.js";
import { fst, snd } from "../../../stdlib.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";
import { toJsBool } from "../../../lambda/church.js";

export { StackSequence }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { map } from "../../operators/map/map.js";
import { Sequence } from "../sequence/Sequence.js";
import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";

export { TupleSequence }

Expand Down Expand Up @@ -34,4 +34,4 @@ const TupleSequence = tuple => {
};

return createMonadicSequence(tupleIterator);
};
};
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/constructors/unfold/unfold.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @module kolibri.sequence.constructors.unfold
* The idea was thankfully provided by Daniel Kröni.
*/
import {createMonadicSequence} from "../../util/sequenceUtil/createMonadicSequence.js";
import {createMonadicSequence} from "../../util/sequenceUtil/sequencePrototype.js";

export { unfold }
/**
Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/operators/catMaybes/catMaybes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence} from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence} from "../../util/sequenceUtil/sequencePrototype.js";
import { catMaybes as arrCatMaybes } from "../../../stdlib/stdlib.js";

export { catMaybes }
Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/operators/cycle/cycle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";

/**
* {@link cycle} ties a finite {@link Iterable} into a circular one, or equivalently,
Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/operators/drop/drop.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dropWhile } from "../dropWhile/dropWhile.js";
import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";

export { drop }

Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/operators/dropWhile/dropWhile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";

export { dropWhile }

Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/operators/map/map.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";

export { map }

Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/operators/mconcat/mconcat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";

export { mconcat }

Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/operators/reverse/reverse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";

export { reverse$ }

Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/operators/take/take.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";

export { take }

Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/operators/takeWhere/takeWhere.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";

export { takeWhere }

Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/operators/takeWhile/takeWhile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";

export { takeWhile }

Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/operators/zipWith/zipWith.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";

export { zipWith }

Expand Down
2 changes: 1 addition & 1 deletion docs/src/kolibri/sequence/sequenceBuilder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { nil } from "./constructors/nil/nil.js";
import { createMonadicSequence } from "./util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "./util/sequenceUtil/sequencePrototype.js";
import { isIterable } from "./util/sequenceUtil/isIterable.js";
import { iteratorOf } from "./util/sequenceUtil/iteratorOf.js";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// noinspection GrazieInspection

import { iteratorOf } from "../../util/sequenceUtil/iteratorOf.js";
import { createMonadicSequence } from "../../util/sequenceUtil/createMonadicSequence.js";
import { createMonadicSequence } from "../../util/sequenceUtil/sequencePrototype.js";
import { Pair } from "../../../stdlib/pair.js";

export { uncons }
Expand Down

This file was deleted.

72 changes: 43 additions & 29 deletions docs/src/kolibri/sequence/util/sequenceUtil/sequencePrototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { PureSequence } from "../../constructors/pureSequence/pureSequence.js";
import { isIterable } from "./isIterable.js";
import { LoggerFactory } from "../../../logger/loggerFactory.js";
import { nil } from "../../constructors/nil/nil.js";
import {
append,
bind,
Expand All @@ -23,21 +24,45 @@ import {
takeWhile,
zip,
zipWith
} from "../../operators/operators.js";
} from "../../operators/operators.js";
import {
eq$,
show
} from "../../terminalOperations/terminalOperations.js";
} from "../../terminalOperations/terminalOperations.js";

const log = LoggerFactory("kolibri.sequence");

export { SequencePrototype }
export { SequencePrototype, createMonadicSequence }

/**
* This function object serves as prototype for the {@link SequenceType}.
* Singleton object.
*/
function SequencePrototype () { } // does nothing on purpose

/**
* This function serves as prototype for the {@link SequenceType}.
*
* @template _T_
* @param { Iterable<_T_> } iterable
* @returns { SequenceType<_T_> }
*/
const SequencePrototype = () => null;
function setPrototype (iterable) {
Object.setPrototypeOf(iterable, SequencePrototype);
return /**@type SequenceType*/ iterable;
}

/**
* Builds an {@link SequenceType} by decorating a given {@link Iterator}.
* @template _T_
* @param { () => Iterator<_T_> } iteratorConstructor - a function that returns an {@link Iterator}
* @returns { SequenceType<_T_> }
*/
function createMonadicSequence (iteratorConstructor) {
const iterable = { [Symbol.iterator]: iteratorConstructor }; // make a new iterable object
return setPrototype(iterable);
}

// monadic sequence operations ----------------------------------

SequencePrototype.and = function (bindFn) {
return bind(bindFn)(this);
Expand All @@ -49,21 +74,9 @@ SequencePrototype.fmap = function (mapper) {

SequencePrototype.pure = val => PureSequence(val);

SequencePrototype.empty = () => {
const emptySequence = () => {
const iterator = () => {
const next = () => ({ done: true, value: undefined });
return { next };
};

return {[Symbol.iterator]: iterator};
};
SequencePrototype.empty = () => nil;

const nil = emptySequence();
Object.setPrototypeOf(nil, SequencePrototype);

return /** @type SequenceType */ nil;
};
// terminal sequence operations ----------------------------------

SequencePrototype.show = function (maxValues = 50) {
return show(this, maxValues);
Expand All @@ -75,9 +88,6 @@ SequencePrototype.toString = function (maxValues = 50) {
}
return show(this, maxValues);
};
SequencePrototype.pipe = function(...transformers) {
return pipe(...transformers)(this);
};

SequencePrototype.eq$ = function(that) {
if (!isIterable(that)) return false;
Expand All @@ -86,7 +96,7 @@ SequencePrototype.eq$ = function(that) {

SequencePrototype["=="] = SequencePrototype.eq$;

// all the SequenceOperations are added to the prototype
// "semigroup-like" sequence operations -------------------------------------

SequencePrototype.append = function (sequence) {
return append(this)(sequence);
Expand Down Expand Up @@ -121,6 +131,16 @@ SequencePrototype.forEach = function (callback) {
return forEach(callback)(this);
};

SequencePrototype.map = SequencePrototype.fmap;

SequencePrototype.mconcat = function () {
return mconcat(this);
};

SequencePrototype.pipe = function(...transformers) {
return pipe(...transformers)(this);
};

SequencePrototype.reverse$ = function () {
return reverse$(this);
};
Expand All @@ -141,12 +161,6 @@ SequencePrototype.takeWhile = function (predicate) {
return takeWhile(predicate)(this);
};

SequencePrototype.map = SequencePrototype.fmap;

SequencePrototype.mconcat = function () {
return mconcat(this);
};

SequencePrototype.zip = function (iterable) {
return zip(this)(iterable);
};
Expand Down

0 comments on commit 49087b7

Please sign in to comment.