Skip to content

Commit

Permalink
Foldable / FlodableWithIndex: curry toReadonlyArrayWith and add toRea…
Browse files Browse the repository at this point in the history
…donlyArray
  • Loading branch information
gcanti authored and mikearnaldi committed Oct 17, 2022
1 parent 9683f82 commit ba899d7
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-goats-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fp-ts/core": patch
---

Foldable / FlodableWithIndex: curry toReadonlyArrayWith and add toReadonlyArray
22 changes: 16 additions & 6 deletions src/Foldable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import type { Kind, TypeClass, TypeLambda } from "@fp-ts/core/HKT"
import { identity } from "@fp-ts/core/internal/Function"
import type { Monoid } from "@fp-ts/core/Monoid"

/**
Expand All @@ -21,17 +22,26 @@ export interface Foldable<F extends TypeLambda> extends TypeClass<F> {
) => <S, R, O, E>(self: Kind<F, S, R, O, E, A>) => B
}

/**
* @since 1.0.0
*/
export const toReadonlyArray = <F extends TypeLambda>(
Foldable: Foldable<F>
): <S, R, O, E, A>(self: Kind<F, S, R, O, E, A>) => ReadonlyArray<A> =>
toReadonlyArrayWith(Foldable)(identity)

/**
* @since 1.0.0
*/
export const toReadonlyArrayWith = <F extends TypeLambda>(
Foldable: Foldable<F>
) =>
<S, R, O, E, A, B>(self: Kind<F, S, R, O, E, A>, f: (a: A) => B): ReadonlyArray<B> =>
Foldable.reduce<A, Array<B>>([], (out, a) => {
out.push(f(a))
return out
})(self)
<A, B>(f: (a: A) => B) =>
<S, R, O, E>(self: Kind<F, S, R, O, E, A>): ReadonlyArray<B> =>
Foldable.reduce<A, Array<B>>([], (out, a) => {
out.push(f(a))
return out
})(self)

/**
* @since 1.0.0
Expand All @@ -40,4 +50,4 @@ export const foldMap = <F extends TypeLambda>(Foldable: Foldable<F>) =>
<M>(Monoid: Monoid<M>) =>
<A>(f: (a: A) => M) =>
<S, R, O, E>(self: Kind<F, S, R, O, E, A>): M =>
Monoid.combineAll(toReadonlyArrayWith(Foldable)(self, f))
Monoid.combineAll(toReadonlyArrayWith(Foldable)(f)(self))
22 changes: 16 additions & 6 deletions src/FoldableWithIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import type { Kind, TypeClass, TypeLambda } from "@fp-ts/core/HKT"
import { identity } from "@fp-ts/core/internal/Function"
import type { Monoid } from "@fp-ts/core/Monoid"

/**
Expand All @@ -21,17 +22,26 @@ export interface FoldableWithIndex<F extends TypeLambda, I> extends TypeClass<F>
) => <S, R, O, E>(self: Kind<F, S, R, O, E, A>) => B
}

/**
* @since 1.0.0
*/
export const toReadonlyArray = <F extends TypeLambda, I>(
FoldableWithIndex: FoldableWithIndex<F, I>
): <S, R, O, E, A>(self: Kind<F, S, R, O, E, A>) => ReadonlyArray<A> =>
toReadonlyArrayWith(FoldableWithIndex)(identity)

/**
* @since 1.0.0
*/
export const toReadonlyArrayWith = <F extends TypeLambda, I>(
FoldableWithIndex: FoldableWithIndex<F, I>
) =>
<S, R, O, E, A, B>(self: Kind<F, S, R, O, E, A>, f: (a: A, i: I) => B): ReadonlyArray<B> =>
FoldableWithIndex.reduceWithIndex<A, Array<B>>([], (out, a, i) => {
out.push(f(a, i))
return out
})(self)
<A, B>(f: (a: A, i: I) => B) =>
<S, R, O, E>(self: Kind<F, S, R, O, E, A>): ReadonlyArray<B> =>
FoldableWithIndex.reduceWithIndex<A, Array<B>>([], (out, a, i) => {
out.push(f(a, i))
return out
})(self)

/**
* @since 1.0.0
Expand All @@ -42,4 +52,4 @@ export const foldMapWithIndex = <F extends TypeLambda, I>(
<M>(Monoid: Monoid<M>) =>
<A>(f: (a: A, i: I) => M) =>
<S, R, O, E>(self: Kind<F, S, R, O, E, A>): M =>
Monoid.combineAll(toReadonlyArrayWith(FoldableWithIndex)(self, f))
Monoid.combineAll(toReadonlyArrayWith(FoldableWithIndex)(f)(self))
10 changes: 8 additions & 2 deletions test/Foldable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ import * as RA from "./data/ReadonlyArray"
import * as U from "./util"

describe("Foldable", () => {
it("toReadonlyArray", () => {
const toReadonlyArray = foldable.toReadonlyArray(O.Foldable)
U.deepStrictEqual(toReadonlyArray(O.none), [])
U.deepStrictEqual(toReadonlyArray(O.some(2)), [2])
})

it("toReadonlyArrayWith", () => {
const toReadonlyArrayWith = foldable.toReadonlyArrayWith(O.Foldable)
U.deepStrictEqual(toReadonlyArrayWith(O.none, U.double), [])
U.deepStrictEqual(toReadonlyArrayWith(O.some(2), U.double), [4])
U.deepStrictEqual(pipe(O.none, toReadonlyArrayWith(U.double)), [])
U.deepStrictEqual(pipe(O.some(2), toReadonlyArrayWith(U.double)), [4])
})

it("foldMap", () => {
Expand Down
12 changes: 9 additions & 3 deletions test/FoldableWithIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import * as RA from "./data/ReadonlyArray"
import * as U from "./util"

describe("Foldable", () => {
it("toReadonlyArray", () => {
const toReadonlyArray = foldableWithIndex.toReadonlyArray(O.FoldableWithIndex)
U.deepStrictEqual(toReadonlyArray(O.none), [])
U.deepStrictEqual(toReadonlyArray(O.some(2)), [2])
})

it("toReadonlyArrayWith", () => {
const toReadonlyArrayWith = foldableWithIndex.toReadonlyArrayWith(O.FoldableWithIndex)
U.deepStrictEqual(toReadonlyArrayWith(O.none, U.double), [])
U.deepStrictEqual(toReadonlyArrayWith(O.some(2), U.double), [4])
U.deepStrictEqual(toReadonlyArrayWith(O.some(2), (a, i) => U.double(a) + i), [4])
U.deepStrictEqual(pipe(O.none, toReadonlyArrayWith(U.double)), [])
U.deepStrictEqual(pipe(O.some(2), toReadonlyArrayWith(U.double)), [4])
U.deepStrictEqual(pipe(O.some(2), toReadonlyArrayWith((a, i) => U.double(a) * i)), [0])
})

it("foldMapWithIndex", () => {
Expand Down
33 changes: 33 additions & 0 deletions test/Function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as _ from "@fp-ts/core/internal/Function"
import * as U from "./util"

describe("FlatMap", () => {
it("pipe", () => {
const f = (n: number): number => n + 1
const g = U.double
U.deepStrictEqual(_.pipe(2), 2)
U.deepStrictEqual(_.pipe(2, f), 3)
U.deepStrictEqual(_.pipe(2, f, g), 6)
U.deepStrictEqual(_.pipe(2, f, g, f), 7)
U.deepStrictEqual(_.pipe(2, f, g, f, g), 14)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f), 15)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g), 30)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g, f), 31)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g, f, g), 62)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g, f, g, f), 63)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g, f, g, f, g), 126)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g, f, g, f, g, f), 127)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g, f, g, f, g, f, g), 254)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g, f, g, f, g, f, g, f), 255)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g, f, g, f, g, f, g, f, g), 510)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g, f, g, f, g, f, g, f, g, f), 511)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g, f, g, f, g, f, g, f, g, f, g), 1022)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g, f, g, f, g, f, g, f, g, f, g, f), 1023)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g, f, g, f, g, f, g, f, g, f, g, f, g), 2046)
U.deepStrictEqual(_.pipe(2, f, g, f, g, f, g, f, g, f, g, f, g, f, g, f, g, f, g, f), 2047)
U.deepStrictEqual(
(_.pipe as any)(...[2, f, g, f, g, f, g, f, g, f, g, f, g, f, g, f, g, f, g, f, g]),
4094
)
})
})

0 comments on commit ba899d7

Please sign in to comment.