Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.

Commit

Permalink
fix: update types on Do derivation
Browse files Browse the repository at this point in the history
  • Loading branch information
baetheus committed Apr 8, 2021
1 parent 75ae1e1 commit 0c7e022
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 22 deletions.
8 changes: 4 additions & 4 deletions array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,16 @@ export const { filter } = Filterable;

export const lookup = (i: number) =>
<A>(as: readonly A[]): O.Option<A> =>
_isOutOfBounds(i, as) ? O.constNone : O.some(as[i]);
_isOutOfBounds(i, as) ? O.none : O.some(as[i]);

export const insertAt = <A>(i: number, a: A) =>
(as: readonly A[]): O.Option<readonly A[]> =>
i < 0 || i > as.length ? O.constNone : O.some(_unsafeInsertAt(i, a, as));
i < 0 || i > as.length ? O.none : O.some(_unsafeInsertAt(i, a, as));

export const updateAt = <A>(i: number, a: A) =>
(as: readonly A[]): O.Option<readonly A[]> =>
_isOutOfBounds(i, as) ? O.constNone : O.some(_unsafeUpdateAt(i, a, as));
_isOutOfBounds(i, as) ? O.none : O.some(_unsafeUpdateAt(i, a, as));

export const deleteAt = (i: number) =>
<A>(as: readonly A[]): O.Option<readonly A[]> =>
_isOutOfBounds(i, as) ? O.constNone : O.some(_unsafeDeleteAt(i, as));
_isOutOfBounds(i, as) ? O.none : O.some(_unsafeDeleteAt(i, as));
11 changes: 6 additions & 5 deletions datum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,12 @@ export const Traversable: TC.Traversable<URI> = {
reduce: Foldable.reduce,
traverse: (A) =>
(faub) =>
(ta) =>
isNone(ta) ? A.of(ta) : pipe(
faub(ta.value),
A.map((b) => isRefresh(ta) ? refresh(b) : replete(b)),
),
fold(
() => A.of(constInitial()),
() => A.of(constPending()),
flow(faub, A.map(replete)),
flow(faub, A.map(refresh)),
),
};

/*******************************************************************************
Expand Down
23 changes: 19 additions & 4 deletions derivations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,24 @@ export const createDo = <URI extends URIS>(
) => ({
// deno-lint-ignore ban-types
Do: <B = never, C = never, D = never>() => M.of<{}, B, C, D>({}),
bindTo: (name: string) => M.map(<A = never>(a: A) => ({ [name]: a })),
bind: <A = never, B = never, C = never, D = never, I = never>(
name: string,
bindTo: <N extends string>(
name: N,
): (<A, B, C, D>(
ta: Kind<URI, [A, B, C, D]>,
// deno-lint-ignore no-explicit-any
) => Kind<URI, [{ [K in N]: A }, B, C, D]>) =>
M.map((a: any): any => ({ [name]: a })),
bind: <N extends string, A, I, B, C, D>(
name: Exclude<N, keyof A>,
fati: (a: A) => Kind<URI, [I, B, C, D]>,
) => M.chain((a: A) => pipe(fati(a), M.map((b) => ({ ...a, [name]: b })))),
): ((
ma: Kind<URI, [A, B, C, D]>,
) => Kind<
URI,
[{ readonly [K in keyof A | N]: K extends keyof A ? A[K] : I }, B, C, D]
> // deno-lint-ignore no-explicit-any
) =>
M.chain((a: any): any =>
pipe(a, fati, M.map((b: any): any => ({ ...a, [name]: b })))
),
});
6 changes: 3 additions & 3 deletions either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type * as TC from "./type_classes.ts";
import type { Fn, Lazy, Predicate, Refinement } from "./types.ts";

import * as O from "./option.ts";
import { constant, flow, identity, isNotNil, pipe } from "./fns.ts";
import { constant, identity, isNotNil, pipe } from "./fns.ts";
import { createSequenceStruct, createSequenceTuple } from "./sequence.ts";
import { createDo } from "./derivations.ts";

Expand Down Expand Up @@ -93,10 +93,10 @@ export const getOrElse = <E, A>(onLeft: (e: E) => A) =>
(ma: Either<E, A>): A => isLeft(ma) ? onLeft(ma.left) : ma.right;

export const getRight = <E, A>(ma: Either<E, A>): O.Option<A> =>
pipe(ma, fold(O.none, O.some));
pipe(ma, fold(O.constNone, O.some));

export const getLeft = <E, A>(ma: Either<E, A>): O.Option<E> =>
pipe(ma, fold(O.some, O.none));
pipe(ma, fold(O.some, O.constNone));

/*******************************************************************************
* Combinators
Expand Down
10 changes: 10 additions & 0 deletions examples/do.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as O from "../option.ts";
import { pipe } from "../fns.ts";

const t1 = pipe(
O.some("Hello"),
O.bindTo("one"),
O.bind("two", ({ one }) => O.some(`${one} World`)),
);

console.log(t1);
10 changes: 5 additions & 5 deletions testing/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Deno.test("Array IndexedTraversable", () => {
const add = traverseOption((a: number, i: number) => O.some(a + i));

assertEquals(sequence([O.some(1), O.some(2), O.some(3)]), O.some([1, 2, 3]));
assertEquals(sequence([O.some(1), O.some(2), O.none()]), O.none());
assertEquals(sequence([O.some(1), O.some(2), O.none]), O.none);

assertEquals(add([1, 2, 3]), O.some([1, 3, 5]));
});
Expand Down Expand Up @@ -229,23 +229,23 @@ Deno.test("Array filter", () => {

Deno.test("Array lookup", () => {
assertEquals(pipe([1, 2, 3], A.lookup(1)), O.some(2));
assertEquals(pipe([], A.lookup(1)), O.none());
assertEquals(pipe([], A.lookup(1)), O.none);
});

Deno.test("Array insertAt", () => {
assertEquals(pipe([1, 2, 3], A.insertAt(0, 10)), O.some([10, 1, 2, 3]));
assertEquals(pipe([1, 2, 3], A.insertAt(3, 10)), O.some([1, 2, 3, 10]));
assertEquals(pipe([1, 2, 3], A.insertAt(5, 10)), O.none());
assertEquals(pipe([1, 2, 3], A.insertAt(5, 10)), O.none);
});

Deno.test("Array updateAt", () => {
assertEquals(pipe([1, 2, 3], A.updateAt(0, 10)), O.some([10, 2, 3]));
assertEquals(pipe([1, 2, 3], A.updateAt(2, 10)), O.some([1, 2, 10]));
assertEquals(pipe([1, 2, 3], A.updateAt(5, 10)), O.none());
assertEquals(pipe([1, 2, 3], A.updateAt(5, 10)), O.none);
});

Deno.test("Array deleteAt", () => {
assertEquals(pipe([1, 2, 3], A.deleteAt(0)), O.some([2, 3]));
assertEquals(pipe([1, 2, 3], A.deleteAt(2)), O.some([1, 2]));
assertEquals(pipe([1, 2, 3], A.deleteAt(5)), O.none());
assertEquals(pipe([1, 2, 3], A.deleteAt(5)), O.none);
});
2 changes: 1 addition & 1 deletion testing/datum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { setoidNumber } from "../setoid.ts";
import { monoidSum } from "../monoid.ts";
import { ordNumber } from "../ord.ts";
import { semigroupSum } from "../semigroup.ts";
import { absurd, pipe } from "../fns.ts";
import { pipe } from "../fns.ts";

import * as AS from "./assert.ts";

Expand Down

0 comments on commit 0c7e022

Please sign in to comment.