This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Chunk, Effect, Function, Pipeable, Predicate, Readable, Stream, Subscribable, SubscriptionRef } from "effect"
|
||||
import { Array, Chunk, Effect, Function, Pipeable, Predicate, Readable, Stream, Subscribable, SubscriptionRef } from "effect"
|
||||
import type { NoSuchElementException } from "effect/Cause"
|
||||
|
||||
|
||||
export const LensTypeId: unique symbol = Symbol.for("@effect-fc/Lens/Lens")
|
||||
@@ -136,6 +137,60 @@ export const mapEffect: {
|
||||
)),
|
||||
}))
|
||||
|
||||
export const mapArrayAt: {
|
||||
<A, ER, EW, RR, RW>(
|
||||
self: Lens<readonly A[], ER, EW, RR, RW>,
|
||||
index: number,
|
||||
): Lens<A, ER | NoSuchElementException, EW | NoSuchElementException, RR, RW>
|
||||
<A, ER, EW, RR, RW>(
|
||||
index: number
|
||||
): (self: Lens<readonly A[], ER, EW, RR, RW>) => Lens<A, ER | NoSuchElementException, EW | NoSuchElementException, RR, RW>
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW>(
|
||||
self: Lens<readonly A[], ER, EW, RR, RW>,
|
||||
index: number,
|
||||
): Lens<A, ER | NoSuchElementException, EW | NoSuchElementException, RR, RW> => mapEffect(
|
||||
self,
|
||||
Array.get(index),
|
||||
(a, b) => Array.replaceOption(a, index, b)),
|
||||
)
|
||||
|
||||
export const mapMutableArrayAt: {
|
||||
<A, ER, EW, RR, RW>(
|
||||
self: Lens<A[], ER, EW, RR, RW>,
|
||||
index: number,
|
||||
): Lens<A, ER | NoSuchElementException, EW | NoSuchElementException, RR, RW>
|
||||
<A, ER, EW, RR, RW>(
|
||||
index: number
|
||||
): (self: Lens<A[], ER, EW, RR, RW>) => Lens<A, ER | NoSuchElementException, EW | NoSuchElementException, RR, RW>
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW>(
|
||||
self: Lens<A[], ER, EW, RR, RW>,
|
||||
index: number,
|
||||
): Lens<A, ER | NoSuchElementException, EW | NoSuchElementException, RR, RW> => mapEffect(
|
||||
self,
|
||||
Array.get(index),
|
||||
(a, b) => Effect.flatMap(
|
||||
Array.get(a, index),
|
||||
() => Effect.as(Effect.sync(() => { a[index] = b }), a),
|
||||
),
|
||||
))
|
||||
|
||||
export const mapChunkAt: {
|
||||
<A, ER, EW, RR, RW>(
|
||||
self: Lens<Chunk.Chunk<A>, ER, EW, RR, RW>,
|
||||
index: number,
|
||||
): Lens<A, ER | NoSuchElementException, EW, RR, RW>
|
||||
<A, ER, EW, RR, RW>(
|
||||
index: number
|
||||
): (self: Lens<Chunk.Chunk<A>, ER, EW, RR, RW>) => Lens<A, ER | NoSuchElementException, EW, RR, RW>
|
||||
} = Function.dual(2, <A, ER, EW, RR, RW>(
|
||||
self: Lens<Chunk.Chunk<A>, ER, EW, RR, RW>,
|
||||
index: number,
|
||||
): Lens<A, ER | NoSuchElementException, EW, RR, RW> => mapEffect(
|
||||
self,
|
||||
Chunk.get(index),
|
||||
(a, b) => Effect.succeed(Chunk.replace(a, index, b))),
|
||||
)
|
||||
|
||||
|
||||
export const get = <A, ER, EW, RR, RW>(self: Lens<A, ER, EW, RR, RW>): Effect.Effect<A, ER, RR> => self.get
|
||||
|
||||
@@ -219,7 +274,7 @@ Effect.gen(function*() {
|
||||
const myChunkRef = yield* SubscriptionRef.make(Chunk.make(12, 38, 69) as Chunk.Chunk<number>)
|
||||
const chunkValueLens = myChunkRef.pipe(
|
||||
fromSubscriptionRef,
|
||||
mapEffect(Chunk.get(1), (a, b) => Effect.succeed(Chunk.replace(a, 1, b)))
|
||||
mapChunkAt(1),
|
||||
)
|
||||
|
||||
console.log(yield* myChunkRef.get, yield* chunkValueLens.get)
|
||||
|
||||
Reference in New Issue
Block a user