Skip to content

Commit

Permalink
use ts NoInfer in Iterable module
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Apr 9, 2024
1 parent a35b3a7 commit 1190983
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 59 deletions.
48 changes: 43 additions & 5 deletions packages/effect/src/Iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { isBoolean } from "./Predicate.js"
import type { NonEmptyArray } from "./ReadonlyArray.js"
import type * as ReadonlyRecord from "./ReadonlyRecord.js"
import * as Tuple from "./Tuple.js"
import type { NoInfer } from "./Types.js"

/**
* Return a `Iterable` with element `i` initialized with `f(i)`.
Expand Down Expand Up @@ -123,7 +122,7 @@ export const fromRecord = <K extends string, A>(self: Readonly<Record<K, A>>): I
export const prepend: {
<B>(head: B): <A>(self: Iterable<A>) => Iterable<A | B>
<A, B>(self: Iterable<A>, head: B): Iterable<A | B>
} = dual(2, <A, B>(self: Iterable<A>, head: B): Iterable<A | B> => flatten<A | B>([[head], self]))
} = dual(2, <A, B>(self: Iterable<A>, head: B): Iterable<A | B> => prependAll(self, [head]))

/**
* Prepends the specified prefix iterable to the beginning of the specified iterable.
Expand All @@ -144,7 +143,7 @@ export const prependAll: {
<A, B>(self: Iterable<A>, that: Iterable<B>): Iterable<A | B>
} = dual(
2,
<A, B>(self: Iterable<A>, that: Iterable<B>): Iterable<A | B> => flatten<A | B>([that, self])
<A, B>(self: Iterable<A>, that: Iterable<B>): Iterable<A | B> => appendAll(that, self)
)

/**
Expand All @@ -156,7 +155,7 @@ export const prependAll: {
export const append: {
<B>(last: B): <A>(self: Iterable<A>) => Iterable<A | B>
<A, B>(self: Iterable<A>, last: B): Iterable<A | B>
} = dual(2, <A, B>(self: Iterable<A>, last: B): Iterable<A | B> => flatten<A | B>([self, [last]]))
} = dual(2, <A, B>(self: Iterable<A>, last: B): Iterable<A | B> => appendAll(self, [last]))

/**
* Concatenates two iterables, combining their elements.
Expand All @@ -169,7 +168,27 @@ export const appendAll: {
<A, B>(self: Iterable<A>, that: Iterable<B>): Iterable<A | B>
} = dual(
2,
<A, B>(self: Iterable<A>, that: Iterable<B>): Iterable<A | B> => flatten<A | B>([self, that])
<A, B>(self: Iterable<A>, that: Iterable<B>): Iterable<A | B> => ({
[Symbol.iterator]() {
const iterA = self[Symbol.iterator]()
let doneA = false
let iterB: Iterator<B>
return {
next() {
if (!doneA) {
const r = iterA.next()
if (r.done) {
doneA = true
iterB = that[Symbol.iterator]()
return iterB.next()
}
return r
}
return iterB.next()
}
}
}
})
)

/**
Expand Down Expand Up @@ -938,6 +957,25 @@ export const forEach: {
}
})

/**
* @category folding
* @since 2.0.0
*/
export const reduce: {
<B, A>(b: B, f: (b: B, a: A, i: number) => B): (self: Iterable<A>) => B
<A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): B
} = dual(3, <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): B => {
if (Array.isArray(self)) {
return self.reduce(f, b)
}
let i = 0
let result = b
for (const n of self) {
result = f(result, n, i++)
}
return result
})

/**
* Deduplicates adjacent elements that are identical using the provided `isEquivalent` function.
*
Expand Down
54 changes: 0 additions & 54 deletions packages/effect/src/internal/Iterable.ts

This file was deleted.

0 comments on commit 1190983

Please sign in to comment.