<A, B>(source: Subscribable<A>, f: (a: A) => B): Subscribable<B>Derive a Subscribable from another by mapping both its current value and its changes — one source
of truth feeds every view (a queue's size/isEmpty from its status).
export const const mapSubscribable: <A, B>(
source: Subscribable<A>,
f: (a: A) => B
) => Subscribable<B>
Derive a
Subscribable
from another by mapping both its current value and its changes — one source
of truth feeds every view (a queue's size/isEmpty from its status).
mapSubscribable = <function (type parameter) A in <A, B>(source: Subscribable<A>, f: (a: A) => B): Subscribable<B>A, function (type parameter) B in <A, B>(source: Subscribable<A>, f: (a: A) => B): Subscribable<B>B>(
source: Subscribable<A>(parameter) source: {
get: Effect.Effect<A>;
changes: Stream.Stream<A>;
}
source: interface Subscribable<A>A read-only reactive value: its current value (
Subscribable.get
, an Effect) plus a stream
of every change (
Subscribable.changes
). This is what a
ref
field surfaces — uniform local
and remote — and it's exactly the read side of a SubscriptionRef (Effect ships no Subscribable type in
this beta, so we name it here).
Subscribable<function (type parameter) A in <A, B>(source: Subscribable<A>, f: (a: A) => B): Subscribable<B>A>,
f: (a: A) => Bf: (a: Aa: function (type parameter) A in <A, B>(source: Subscribable<A>, f: (a: A) => B): Subscribable<B>A) => function (type parameter) B in <A, B>(source: Subscribable<A>, f: (a: A) => B): Subscribable<B>B,
): interface Subscribable<A>A read-only reactive value: its current value (
Subscribable.get
, an Effect) plus a stream
of every change (
Subscribable.changes
). This is what a
ref
field surfaces — uniform local
and remote — and it's exactly the read side of a SubscriptionRef (Effect ships no Subscribable type in
this beta, so we name it here).
Subscribable<function (type parameter) B in <A, B>(source: Subscribable<A>, f: (a: A) => B): Subscribable<B>B> => ({
Subscribable<B>.get: Effect.Effect<B, never, never>(property) Subscribable<B>.get: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
get: import EffectEffect.const map: <A, never, never, B>(self: Effect.Effect<A, never, never>, f: (a: A) => B) => Effect.Effect<B, never, never> (+1 overload)Transforms the value inside an effect by applying a function to it.
When to use
Use to transform an effect's success value with a function that returns a
plain value, producing a new effect without changing the original effect's
typed error or context requirements.
Details
map takes a function and applies it to the value contained within an
effect, creating a new effect with the transformed value.
It's important to note that effects are immutable, meaning that the original
effect is not modified. Instead, a new effect is returned with the updated
value.
Example (Choosing map syntax variants)
import { Effect, pipe } from "effect"
const myEffect = Effect.succeed(1)
const transformation = (n: number) => n + 1
const mappedWithPipe = pipe(myEffect, Effect.map(transformation))
const mappedWithDataFirst = Effect.map(myEffect, transformation)
const mappedWithMethod = myEffect.pipe(Effect.map(transformation))
Example (Adding a service charge)
import { Effect, pipe } from "effect"
const addServiceCharge = (amount: number) => amount + 1
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const finalAmount = pipe(
fetchTransactionAmount,
Effect.map(addServiceCharge)
)
Effect.runPromise(finalAmount).then(console.log)
// Output: 101
map(source: Subscribable<A>(parameter) source: {
get: Effect.Effect<A>;
changes: Stream.Stream<A>;
}
source.Subscribable<A>.get: Effect.Effect<A, never, never>(property) Subscribable<A>.get: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
get, f: (a: A) => Bf),
Subscribable<B>.changes: Stream.Stream<B, never, never>(property) Subscribable<B>.changes: {
channel: Channel.Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, R>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
changes: import StreamStream.const map: <A, never, never, B>(self: Stream.Stream<A, never, never>, f: (a: A, i: number) => B) => Stream.Stream<B, never, never> (+1 overload)Transforms the elements of this stream using the supplied function.
Example (Mapping stream values)
import { Console, Effect, Stream } from "effect"
const stream = Stream.fromArray([1, 2, 3]).pipe(Stream.map((n, i) => n + i))
const program = Stream.runCollect(stream).pipe(
Effect.tap((values) => Console.log(values))
)
Effect.runPromise(program)
// [ 1, 3, 5 ]
map(source: Subscribable<A>(parameter) source: {
get: Effect.Effect<A>;
changes: Stream.Stream<A>;
}
source.Subscribable<A>.changes: Stream.Stream<A, never, never>(property) Subscribable<A>.changes: {
channel: Channel.Channel<Arr.NonEmptyReadonlyArray<A>, E, void, unknown, unknown, unknown, R>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
changes, f: (a: A) => Bf),
});