import { Stream } from '@motorcycle/types' import { curry2 } from '@typed/prelude' import { map } from './map' import { switchLatest } from './switchLatest' /** * Applies a function, which returns a higher-order stream, to each event value * of a stream and returns a new stream that adopts the behavior of (i.e., * emits the events of) the most recent inner stream. * * @name switchMap(f: (a: A) => Stream, s: Stream): Stream * * @example * import { now, scan, switchMap, observe, skip } from '@motorcycle/stream' * * const a$ = now(1) * const b$ = now(2) * const f = (a: number) => scan((x, y) => x + y, a, b$) * const s = skip(1, switchMap(f, a$)) * * observe(console.log, s) // 3 */ export const switchMap: SwitchMapArity2 = curry2(function switchMap( f: (a: A) => Stream, s: Stream ): Stream { return switchLatest(map(f, s)) }) export interface SwitchMapArity2 { (f: (a: A) => Stream, s: Stream): Stream (f: (a: A) => Stream): SwitchMapArity1 } export interface SwitchMapArity1 { (s: Stream): Stream }