Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow record/map to transform type with optional properties #60

Merged
merged 11 commits into from
Aug 23, 2022
36 changes: 16 additions & 20 deletions affect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ declare module "./kind.ts" {
export function ap<A, I, B, C>(
tfai: Affect<C, B, (a: A) => I>,
): (ta: Affect<C, B, A>) => Affect<C, B, I> {
return (ta) =>
(r) =>
Promise.all([tfai(r), ta(r)]).then(([efai, ea]) =>
pipe(ea, eitherAp(efai))
);
return (ta) => (r) =>
Promise.all([tfai(r), ta(r)]).then(([efai, ea]) =>
pipe(ea, eitherAp(efai))
);
}

/**
Expand Down Expand Up @@ -233,11 +232,10 @@ export function bimap<A, B, I, J>(
export function chain<A, I, B, C>(
fati: (a: A) => Affect<C, B, I>,
): (ta: Affect<C, B, A>) => Affect<C, B, I> {
return (ta) =>
async (r) => {
const ea = await ta(r);
return eitherIsLeft(ea) ? ea : fati(ea.right)(r);
};
return (ta) => async (r) => {
const ea = await ta(r);
return eitherIsLeft(ea) ? ea : fati(ea.right)(r);
};
}

/**
Expand Down Expand Up @@ -268,11 +266,10 @@ export function chain<A, I, B, C>(
export function compose<I, B, A>(
tai: Affect<A, B, I>,
): <C>(tca: Affect<C, B, A>) => Affect<C, B, I> {
return (tca) =>
async (c) => {
const ea = await tca(c);
return eitherIsLeft(ea) ? ea : await tai(ea.right);
};
return (tca) => async (c) => {
const ea = await tca(c);
return eitherIsLeft(ea) ? ea : await tai(ea.right);
};
}

/**
Expand Down Expand Up @@ -369,11 +366,10 @@ export function fromIOEither<A, B, C = never>(
export function fromOption<B, C>(
onNone: (c: C) => B,
): <A>(ta: Option<A>) => Affect<C, B, A> {
return <A>(ta: Option<A>) =>
(c) =>
isNone(ta)
? resolve(eitherLeft(onNone(c)))
: resolve(eitherRight(ta.value));
return <A>(ta: Option<A>) => (c) =>
isNone(ta)
? resolve(eitherLeft(onNone(c)))
: resolve(eitherRight(ta.value));
}

/**
Expand Down
46 changes: 22 additions & 24 deletions array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,19 @@ export function prepend<A>(
return (ma) => [head, ...ma];
}

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

export const insertAt = <A>(i: number, a: A) =>
(as: ReadonlyArray<A>): O.Option<ReadonlyArray<A>> =>
export const insertAt =
<A>(i: number, a: A) => (as: ReadonlyArray<A>): O.Option<ReadonlyArray<A>> =>
i < 0 || i > as.length ? O.none : O.some(unsafeInsertAt(i, a, as));

export const updateAt = <A>(i: number, a: A) =>
(as: ReadonlyArray<A>): O.Option<ReadonlyArray<A>> =>
export const updateAt =
<A>(i: number, a: A) => (as: ReadonlyArray<A>): O.Option<ReadonlyArray<A>> =>
isOutOfBounds(i, as) ? O.none : O.some(unsafeUpdateAt(i, a, as));

export const deleteAt = (i: number) =>
<A>(as: ReadonlyArray<A>): O.Option<ReadonlyArray<A>> =>
export const deleteAt =
(i: number) => <A>(as: ReadonlyArray<A>): O.Option<ReadonlyArray<A>> =>
isOutOfBounds(i, as) ? O.none : O.some(unsafeDeleteAt(i, as));

/**
Expand Down Expand Up @@ -289,7 +288,8 @@ export const range = (
*
* @category combinators
*/
export const zipWith = <B, A, C>(fb: ReadonlyArray<B>, f: (a: A, b: B) => C) =>
export const zipWith =
<B, A, C>(fb: ReadonlyArray<B>, f: (a: A, b: B) => C) =>
(
fa: ReadonlyArray<A>,
): ReadonlyArray<C> => {
Expand Down Expand Up @@ -394,28 +394,26 @@ export const Traversable: T.Traversable<URI> = IndexedTraversable;

export function getSetoid<A>(S: T.Setoid<A>): T.Setoid<ReadonlyArray<A>> {
return ({
equals: (a) =>
(b) =>
a === b ||
(a.length === b.length && a.every((v, i) => S.equals(v)(b[i]))),
equals: (a) => (b) =>
a === b ||
(a.length === b.length && a.every((v, i) => S.equals(v)(b[i]))),
});
}

export function getOrd<A>(O: T.Ord<A>): T.Ord<ReadonlyArray<A>> {
const { equals } = getSetoid(O);
return ({
equals,
lte: (b) =>
(a) => {
const length = Math.min(a.length, b.length);
let index = -1;
while (++index < length) {
if (!O.equals(a[index])(b[index])) {
return O.lte(b[index])(a[index]);
}
lte: (b) => (a) => {
const length = Math.min(a.length, b.length);
let index = -1;
while (++index < length) {
if (!O.equals(a[index])(b[index])) {
return O.lte(b[index])(a[index]);
}
return a.length <= b.length;
},
}
return a.length <= b.length;
},
});
}

Expand Down
9 changes: 4 additions & 5 deletions at.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ export function atMap<A = never>(): At<Map<string, A>, string, Option<A>> {
tag: "Lens",
get: (m) => fromNullable(m.get(key)),
set: fold(
() =>
(m) => {
m.delete(key);
return m;
},
() => (m) => {
m.delete(key);
return m;
},
(a) => (m) => m.set(key, a),
),
}),
Expand Down
48 changes: 24 additions & 24 deletions decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ export function tuple<A extends any[]>(
) as Decoder<unknown, { [K in keyof A]: A[K] }>;
}

const traverseStruct = (items: Record<string, Decoder<unknown, unknown>>) =>
const traverseStruct =
(items: Record<string, Decoder<unknown, unknown>>) =>
(a: Record<string, unknown>) =>
pipe(
items,
Expand All @@ -231,7 +232,8 @@ const undefinedProperty: R.Result<E.Either<void, unknown>> = R.success(
E.right(undefined),
);

const traversePartial = (items: Record<string, Decoder<unknown, unknown>>) =>
const traversePartial =
(items: Record<string, Decoder<unknown, unknown>>) =>
(a: Record<string, unknown>) =>
pipe(
items,
Expand Down Expand Up @@ -259,33 +261,31 @@ export function partial<A>(
export function intersect<B, I>(
and: Decoder<B, I>,
): <A>(ta: Decoder<B, A>) => Decoder<B, A & I> {
return (ta) =>
(a) =>
pipe(
R.sequenceTuple(ta(a), and(a)),
E.bimap(
(e) => R.ofWrap("cannot decode intersection", e),
([left, right]) => _intersect(left, right),
),
);
return (ta) => (a) =>
pipe(
R.sequenceTuple(ta(a), and(a)),
E.bimap(
(e) => R.ofWrap("cannot decode intersection", e),
([left, right]) => _intersect(left, right),
),
);
}

export function union<B, I>(
or: Decoder<B, I>,
): <A>(ta: Decoder<B, A>) => Decoder<B, A | I> {
return <A>(ta: Decoder<B, A>) =>
(a) =>
pipe(
ta(a),
E.chainLeft((left) =>
pipe(
or(a),
E.mapLeft((right) =>
R.ofWrap("cannot decode union", R.concat(left)(right))
),
) as R.Result<A | I>
),
);
return <A>(ta: Decoder<B, A>) => (a) =>
pipe(
ta(a),
E.chainLeft((left) =>
pipe(
or(a),
E.mapLeft((right) =>
R.ofWrap("cannot decode union", R.concat(left)(right))
),
) as R.Result<A | I>
),
);
}

export function lazy<A, B>(id: string, fn: () => Decoder<B, A>): Decoder<B, A> {
Expand Down
64 changes: 31 additions & 33 deletions either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,19 @@ export function getSetoid<A, B>(
SA: T.Setoid<A>,
): T.Setoid<Either<B, A>> {
return ({
equals: (b) =>
(a) => {
if (isLeft(a)) {
if (isLeft(b)) {
return SB.equals(b.left)(a.left);
}
return false;
}

equals: (b) => (a) => {
if (isLeft(a)) {
if (isLeft(b)) {
return false;
return SB.equals(b.left)(a.left);
}
return SA.equals(b.right)(a.right);
},
return false;
}

if (isLeft(b)) {
return false;
}
return SA.equals(b.right)(a.right);
},
});
}

Expand All @@ -154,38 +153,37 @@ export function getOrd<A, B>(
): T.Ord<Either<B, A>> {
return ({
...getSetoid(OB, OA),
lte: (b) =>
(a) => {
if (isLeft(a)) {
if (isLeft(b)) {
return OB.lte(b.left)(a.left);
}
return true;
}

lte: (b) => (a) => {
if (isLeft(a)) {
if (isLeft(b)) {
return false;
return OB.lte(b.left)(a.left);
}
return OA.lte(b.right)(a.right);
},
return true;
}

if (isLeft(b)) {
return false;
}
return OA.lte(b.right)(a.right);
},
});
}

export function getLeftSemigroup<E = never, A = never>(
SE: T.Semigroup<E>,
): T.Semigroup<Either<E, A>> {
return ({
concat: (x) =>
(y) => isRight(x) ? x : isRight(y) ? y : left(SE.concat(x.left)(y.left)),
concat: (x) => (y) =>
isRight(x) ? x : isRight(y) ? y : left(SE.concat(x.left)(y.left)),
});
}

export function getRightSemigroup<E = never, A = never>(
SA: T.Semigroup<A>,
): T.Semigroup<Either<E, A>> {
return ({
concat: (x) =>
(y) => isLeft(x) ? x : isLeft(y) ? y : right(SA.concat(x.right)(y.right)),
concat: (x) => (y) =>
isLeft(x) ? x : isLeft(y) ? y : right(SA.concat(x.right)(y.right)),
});
}

Expand All @@ -204,11 +202,11 @@ export function getRightMonad<E>(
return ({
...Monad,
ap: (tfai) =>
// deno-lint-ignore no-explicit-any
(ta): Either<any, any> =>
isLeft(tfai)
? (isLeft(ta) ? left(concat(tfai.left)(ta.left)) : tfai)
: (isLeft(ta) ? ta : right(tfai.right(ta.right))),
// deno-lint-ignore no-explicit-any
(ta): Either<any, any> =>
isLeft(tfai)
? (isLeft(ta) ? left(concat(tfai.left)(ta.left)) : tfai)
: (isLeft(ta) ? ta : right(tfai.right(ta.right))),
});
}

Expand Down
16 changes: 8 additions & 8 deletions examples/fetch_archives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ const foldErrors = <O>(
onFetchError: (e: unknown) => O,
onDecodeError: (e: string) => O,
) =>
(e: MyErrors) => {
switch (e.type) {
case "FetchError":
return onFetchError(e.error);
case "DecodeError":
return onDecodeError(e.error);
}
};
(e: MyErrors) => {
switch (e.type) {
case "FetchError":
return onFetchError(e.error);
case "DecodeError":
return onDecodeError(e.error);
}
};

/**
* Let's make a helper function for fetch. This one takes the same
Expand Down
11 changes: 5 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ export function indexArray<A>(): Index<ReadonlyArray<A>, number, A> {
index: (i) => ({
tag: "Optional",
getOption: lookup(i),
set: (a) =>
(as) =>
pipe(
updateAt(i, a)(as),
getOrElse(() => as),
),
set: (a) => (as) =>
pipe(
updateAt(i, a)(as),
getOrElse(() => as),
),
}),
});
}
Expand Down
Loading