effect 4.0.0-beta.17 → 4.0.0-beta.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Array.d.ts +127 -299
- package/dist/Array.d.ts.map +1 -1
- package/dist/Array.js +102 -62
- package/dist/Array.js.map +1 -1
- package/dist/Cache.d.ts.map +1 -1
- package/dist/Cache.js +5 -4
- package/dist/Cache.js.map +1 -1
- package/dist/Channel.d.ts +97 -11
- package/dist/Channel.d.ts.map +1 -1
- package/dist/Channel.js +72 -29
- package/dist/Channel.js.map +1 -1
- package/dist/Chunk.d.ts +54 -247
- package/dist/Chunk.d.ts.map +1 -1
- package/dist/Chunk.js +36 -67
- package/dist/Chunk.js.map +1 -1
- package/dist/Effect.d.ts +337 -437
- package/dist/Effect.d.ts.map +1 -1
- package/dist/Effect.js +118 -134
- package/dist/Effect.js.map +1 -1
- package/dist/Filter.d.ts +0 -33
- package/dist/Filter.d.ts.map +1 -1
- package/dist/Filter.js +0 -13
- package/dist/Filter.js.map +1 -1
- package/dist/HashMap.d.ts +15 -14
- package/dist/HashMap.d.ts.map +1 -1
- package/dist/HashMap.js +4 -4
- package/dist/HashMap.js.map +1 -1
- package/dist/Iterable.d.ts +40 -39
- package/dist/Iterable.d.ts.map +1 -1
- package/dist/Iterable.js +94 -22
- package/dist/Iterable.js.map +1 -1
- package/dist/Option.d.ts +22 -15
- package/dist/Option.d.ts.map +1 -1
- package/dist/Option.js +14 -7
- package/dist/Option.js.map +1 -1
- package/dist/Pull.d.ts.map +1 -1
- package/dist/Pull.js +1 -1
- package/dist/Pull.js.map +1 -1
- package/dist/Record.d.ts +24 -120
- package/dist/Record.d.ts.map +1 -1
- package/dist/Record.js +21 -41
- package/dist/Record.js.map +1 -1
- package/dist/Sink.d.ts +11 -11
- package/dist/Sink.d.ts.map +1 -1
- package/dist/Sink.js +53 -6
- package/dist/Sink.js.map +1 -1
- package/dist/Stream.d.ts +198 -386
- package/dist/Stream.d.ts.map +1 -1
- package/dist/Stream.js +103 -59
- package/dist/Stream.js.map +1 -1
- package/dist/Trie.d.ts +18 -17
- package/dist/Trie.d.ts.map +1 -1
- package/dist/Trie.js +5 -5
- package/dist/Trie.js.map +1 -1
- package/dist/TxChunk.d.ts +37 -37
- package/dist/TxChunk.d.ts.map +1 -1
- package/dist/TxChunk.js +3 -3
- package/dist/TxChunk.js.map +1 -1
- package/dist/TxDeferred.d.ts +328 -0
- package/dist/TxDeferred.d.ts.map +1 -0
- package/dist/TxDeferred.js +196 -0
- package/dist/TxDeferred.js.map +1 -0
- package/dist/TxHashMap.d.ts +84 -83
- package/dist/TxHashMap.d.ts.map +1 -1
- package/dist/TxHashMap.js +24 -24
- package/dist/TxHashMap.js.map +1 -1
- package/dist/TxHashSet.d.ts +35 -35
- package/dist/TxHashSet.d.ts.map +1 -1
- package/dist/TxHashSet.js +14 -14
- package/dist/TxHashSet.js.map +1 -1
- package/dist/TxPriorityQueue.d.ts +609 -0
- package/dist/TxPriorityQueue.d.ts.map +1 -0
- package/dist/TxPriorityQueue.js +415 -0
- package/dist/TxPriorityQueue.js.map +1 -0
- package/dist/TxPubSub.d.ts +585 -0
- package/dist/TxPubSub.d.ts.map +1 -0
- package/dist/TxPubSub.js +521 -0
- package/dist/TxPubSub.js.map +1 -0
- package/dist/TxQueue.d.ts +32 -32
- package/dist/TxQueue.d.ts.map +1 -1
- package/dist/TxQueue.js +26 -26
- package/dist/TxQueue.js.map +1 -1
- package/dist/TxReentrantLock.d.ts +523 -0
- package/dist/TxReentrantLock.d.ts.map +1 -0
- package/dist/TxReentrantLock.js +504 -0
- package/dist/TxReentrantLock.js.map +1 -0
- package/dist/TxRef.d.ts +34 -34
- package/dist/TxRef.d.ts.map +1 -1
- package/dist/TxRef.js +21 -14
- package/dist/TxRef.js.map +1 -1
- package/dist/TxSemaphore.d.ts +8 -8
- package/dist/TxSemaphore.d.ts.map +1 -1
- package/dist/TxSemaphore.js +7 -7
- package/dist/TxSemaphore.js.map +1 -1
- package/dist/TxSubscriptionRef.d.ts +508 -0
- package/dist/TxSubscriptionRef.d.ts.map +1 -0
- package/dist/TxSubscriptionRef.js +293 -0
- package/dist/TxSubscriptionRef.js.map +1 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/effect.js +99 -42
- package/dist/internal/effect.js.map +1 -1
- package/dist/internal/hashMap.js +3 -2
- package/dist/internal/hashMap.js.map +1 -1
- package/dist/internal/trie.js +5 -4
- package/dist/internal/trie.js.map +1 -1
- package/dist/unstable/ai/Tool.d.ts.map +1 -1
- package/dist/unstable/ai/Tool.js +0 -9
- package/dist/unstable/ai/Tool.js.map +1 -1
- package/dist/unstable/cli/Command.d.ts +1 -1
- package/dist/unstable/cli/Command.d.ts.map +1 -1
- package/dist/unstable/cli/Command.js +1 -1
- package/dist/unstable/cli/Command.js.map +1 -1
- package/dist/unstable/cluster/K8sHttpClient.js +4 -4
- package/dist/unstable/cluster/K8sHttpClient.js.map +1 -1
- package/dist/unstable/cluster/Sharding.js +1 -1
- package/dist/unstable/cluster/Sharding.js.map +1 -1
- package/dist/unstable/encoding/Sse.js +1 -1
- package/dist/unstable/encoding/Sse.js.map +1 -1
- package/dist/unstable/rpc/RpcServer.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcServer.js +1 -2
- package/dist/unstable/rpc/RpcServer.js.map +1 -1
- package/dist/unstable/socket/Socket.d.ts.map +1 -1
- package/dist/unstable/socket/Socket.js +3 -3
- package/dist/unstable/socket/Socket.js.map +1 -1
- package/package.json +1 -1
- package/src/Array.ts +190 -342
- package/src/Cache.ts +6 -5
- package/src/Channel.ts +506 -102
- package/src/Chunk.ts +81 -268
- package/src/Effect.ts +437 -518
- package/src/Filter.ts +0 -57
- package/src/HashMap.ts +15 -14
- package/src/Iterable.ts +105 -50
- package/src/Option.ts +30 -20
- package/src/Pull.ts +1 -1
- package/src/Record.ts +43 -152
- package/src/Sink.ts +75 -23
- package/src/Stream.ts +442 -502
- package/src/Trie.ts +18 -17
- package/src/TxChunk.ts +72 -53
- package/src/TxDeferred.ts +394 -0
- package/src/TxHashMap.ts +332 -285
- package/src/TxHashSet.ts +111 -116
- package/src/TxPriorityQueue.ts +767 -0
- package/src/TxPubSub.ts +789 -0
- package/src/TxQueue.ts +241 -251
- package/src/TxReentrantLock.ts +753 -0
- package/src/TxRef.ts +50 -38
- package/src/TxSemaphore.ts +29 -32
- package/src/TxSubscriptionRef.ts +639 -0
- package/src/index.ts +45 -0
- package/src/internal/effect.ts +368 -163
- package/src/internal/hashMap.ts +7 -5
- package/src/internal/trie.ts +16 -9
- package/src/unstable/ai/Tool.ts +0 -9
- package/src/unstable/cli/Command.ts +6 -4
- package/src/unstable/cluster/K8sHttpClient.ts +4 -4
- package/src/unstable/cluster/Sharding.ts +1 -1
- package/src/unstable/encoding/Sse.ts +1 -1
- package/src/unstable/rpc/RpcServer.ts +1 -7
- package/src/unstable/socket/Socket.ts +9 -11
package/src/Filter.ts
CHANGED
|
@@ -74,63 +74,6 @@ export interface FilterEffect<
|
|
|
74
74
|
(input: Input, ...args: Args): Effect<Result.Result<Pass, Fail>, E, R>
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
// -------------------------------------------------------------------------------------
|
|
78
|
-
// apply
|
|
79
|
-
// -------------------------------------------------------------------------------------
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* @since 4.0.0
|
|
83
|
-
* @category Apply
|
|
84
|
-
*/
|
|
85
|
-
export type OrPredicate<A, R extends boolean | Result.Result<any, any>, Args extends ReadonlyArray<any> = []> = (
|
|
86
|
-
input: A,
|
|
87
|
-
...args: Args
|
|
88
|
-
) => R
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* @since 4.0.0
|
|
92
|
-
* @category Apply
|
|
93
|
-
*/
|
|
94
|
-
export type ResultOrBool<Out = any> = boolean | Result.Result<any, Out>
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* @since 4.0.0
|
|
98
|
-
* @category Apply
|
|
99
|
-
*/
|
|
100
|
-
export type Pass<A, R extends boolean | Result.Result<any, any>> = [R] extends [Result.Result<infer _P, infer _F>] ? _P
|
|
101
|
-
: A
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* @since 4.0.0
|
|
105
|
-
* @category Apply
|
|
106
|
-
*/
|
|
107
|
-
export type Fail<A, R extends boolean | Result.Result<any, any>> = [R] extends [Result.Result<infer _P, infer _F>] ? _F
|
|
108
|
-
: A
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* @since 4.0.0
|
|
112
|
-
* @category Apply
|
|
113
|
-
*/
|
|
114
|
-
export type ApplyResult<A, R extends boolean | Result.Result<any, any>> = Result.Result<Pass<A, R>, Fail<A, R>>
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Applies a filter, predicate, or refinement to an input and returns a boxed
|
|
118
|
-
* result. Extra arguments are forwarded to the function.
|
|
119
|
-
*
|
|
120
|
-
* @since 4.0.0
|
|
121
|
-
* @category Apply
|
|
122
|
-
*/
|
|
123
|
-
export const apply = <In, R extends ResultOrBool>(
|
|
124
|
-
filter: (input: In, ...args: Array<any>) => R,
|
|
125
|
-
input: In,
|
|
126
|
-
...args: Array<any>
|
|
127
|
-
): ApplyResult<In, R> => {
|
|
128
|
-
const result = filter(input, ...args)
|
|
129
|
-
if (result === true) return Result.succeed(input) as any
|
|
130
|
-
if (result === false) return Result.fail(input) as any
|
|
131
|
-
return result as any
|
|
132
|
-
}
|
|
133
|
-
|
|
134
77
|
// -------------------------------------------------------------------------------------
|
|
135
78
|
// Constructors
|
|
136
79
|
// -------------------------------------------------------------------------------------
|
package/src/HashMap.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import type { Equal } from "./Equal.ts"
|
|
6
|
+
import type * as Filter from "./Filter.ts"
|
|
6
7
|
import type { Inspectable } from "./Inspectable.ts"
|
|
7
8
|
import * as internal from "./internal/hashMap.ts"
|
|
8
9
|
import type { Option } from "./Option.ts"
|
|
@@ -2011,18 +2012,18 @@ export const filter: {
|
|
|
2011
2012
|
export const compact: <K, A>(self: HashMap<K, Option<A>>) => HashMap<K, A> = internal.compact
|
|
2012
2013
|
|
|
2013
2014
|
/**
|
|
2014
|
-
* Maps over the entries of the `HashMap` using the specified
|
|
2015
|
-
*
|
|
2015
|
+
* Maps over the entries of the `HashMap` using the specified filter and keeps
|
|
2016
|
+
* only successful results.
|
|
2016
2017
|
*
|
|
2017
2018
|
* @example
|
|
2018
2019
|
* ```ts
|
|
2019
2020
|
* import * as HashMap from "effect/HashMap"
|
|
2020
|
-
* import * as
|
|
2021
|
+
* import * as Result from "effect/Result"
|
|
2021
2022
|
*
|
|
2022
2023
|
* const map1 = HashMap.make(["a", 1], ["b", 2], ["c", 3], ["d", 4])
|
|
2023
2024
|
* const map2 = HashMap.filterMap(
|
|
2024
2025
|
* map1,
|
|
2025
|
-
* (value) => value % 2 === 0 ?
|
|
2026
|
+
* (value) => value % 2 === 0 ? Result.succeed(value * 2) : Result.failVoid
|
|
2026
2027
|
* )
|
|
2027
2028
|
*
|
|
2028
2029
|
* console.log(HashMap.size(map2)) // 2
|
|
@@ -2035,18 +2036,18 @@ export const compact: <K, A>(self: HashMap<K, Option<A>>) => HashMap<K, A> = int
|
|
|
2035
2036
|
*/
|
|
2036
2037
|
export const filterMap: {
|
|
2037
2038
|
/**
|
|
2038
|
-
* Maps over the entries of the `HashMap` using the specified
|
|
2039
|
-
*
|
|
2039
|
+
* Maps over the entries of the `HashMap` using the specified filter and keeps
|
|
2040
|
+
* only successful results.
|
|
2040
2041
|
*
|
|
2041
2042
|
* @example
|
|
2042
2043
|
* ```ts
|
|
2043
2044
|
* import * as HashMap from "effect/HashMap"
|
|
2044
|
-
* import * as
|
|
2045
|
+
* import * as Result from "effect/Result"
|
|
2045
2046
|
*
|
|
2046
2047
|
* const map1 = HashMap.make(["a", 1], ["b", 2], ["c", 3], ["d", 4])
|
|
2047
2048
|
* const map2 = HashMap.filterMap(
|
|
2048
2049
|
* map1,
|
|
2049
|
-
* (value) => value % 2 === 0 ?
|
|
2050
|
+
* (value) => value % 2 === 0 ? Result.succeed(value * 2) : Result.failVoid
|
|
2050
2051
|
* )
|
|
2051
2052
|
*
|
|
2052
2053
|
* console.log(HashMap.size(map2)) // 2
|
|
@@ -2057,20 +2058,20 @@ export const filterMap: {
|
|
|
2057
2058
|
* @since 2.0.0
|
|
2058
2059
|
* @category filtering
|
|
2059
2060
|
*/
|
|
2060
|
-
<A, K, B>(f:
|
|
2061
|
+
<A, K, B, X>(f: Filter.Filter<A, B, X, [key: K]>): (self: HashMap<K, A>) => HashMap<K, B>
|
|
2061
2062
|
/**
|
|
2062
|
-
* Maps over the entries of the `HashMap` using the specified
|
|
2063
|
-
*
|
|
2063
|
+
* Maps over the entries of the `HashMap` using the specified filter and keeps
|
|
2064
|
+
* only successful results.
|
|
2064
2065
|
*
|
|
2065
2066
|
* @example
|
|
2066
2067
|
* ```ts
|
|
2067
2068
|
* import * as HashMap from "effect/HashMap"
|
|
2068
|
-
* import * as
|
|
2069
|
+
* import * as Result from "effect/Result"
|
|
2069
2070
|
*
|
|
2070
2071
|
* const map1 = HashMap.make(["a", 1], ["b", 2], ["c", 3], ["d", 4])
|
|
2071
2072
|
* const map2 = HashMap.filterMap(
|
|
2072
2073
|
* map1,
|
|
2073
|
-
* (value) => value % 2 === 0 ?
|
|
2074
|
+
* (value) => value % 2 === 0 ? Result.succeed(value * 2) : Result.failVoid
|
|
2074
2075
|
* )
|
|
2075
2076
|
*
|
|
2076
2077
|
* console.log(HashMap.size(map2)) // 2
|
|
@@ -2081,7 +2082,7 @@ export const filterMap: {
|
|
|
2081
2082
|
* @since 2.0.0
|
|
2082
2083
|
* @category filtering
|
|
2083
2084
|
*/
|
|
2084
|
-
<K, A, B>(self: HashMap<K, A>, f:
|
|
2085
|
+
<K, A, B, X>(self: HashMap<K, A>, f: Filter.Filter<A, B, X, [key: K]>): HashMap<K, B>
|
|
2085
2086
|
} = internal.filterMap
|
|
2086
2087
|
|
|
2087
2088
|
/**
|
package/src/Iterable.ts
CHANGED
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
|
|
33
33
|
import type { NonEmptyArray } from "./Array.ts"
|
|
34
34
|
import * as Equal from "./Equal.ts"
|
|
35
|
-
import
|
|
35
|
+
import type * as Filter from "./Filter.ts"
|
|
36
|
+
import { dual } from "./Function.ts"
|
|
36
37
|
import type { Option } from "./Option.ts"
|
|
37
38
|
import * as O from "./Option.ts"
|
|
38
39
|
import { isBoolean } from "./Predicate.ts"
|
|
@@ -3114,21 +3115,21 @@ export const flatten = <A>(self: Iterable<Iterable<A>>): Iterable<A> => ({
|
|
|
3114
3115
|
})
|
|
3115
3116
|
|
|
3116
3117
|
/**
|
|
3117
|
-
* Transforms elements of an iterable using a function that returns
|
|
3118
|
+
* Transforms elements of an iterable using a function that returns a `Result`, keeping only successful values.
|
|
3118
3119
|
*
|
|
3119
3120
|
* This combines mapping and filtering in a single operation - the function is applied to each element,
|
|
3120
|
-
* and only elements that result in
|
|
3121
|
+
* and only elements that result in `Result.succeed` are included in the result.
|
|
3121
3122
|
*
|
|
3122
3123
|
* @example
|
|
3123
3124
|
* ```ts
|
|
3124
3125
|
* import { Iterable } from "effect"
|
|
3125
|
-
* import * as
|
|
3126
|
+
* import * as Result from "effect/Result"
|
|
3126
3127
|
*
|
|
3127
3128
|
* // Parse strings to numbers, keeping only valid ones
|
|
3128
3129
|
* const strings = ["1", "2", "invalid", "4", "not-a-number"]
|
|
3129
3130
|
* const numbers = Iterable.filterMap(strings, (s) => {
|
|
3130
3131
|
* const num = parseInt(s)
|
|
3131
|
-
* return isNaN(num) ?
|
|
3132
|
+
* return isNaN(num) ? Result.failVoid : Result.succeed(num)
|
|
3132
3133
|
* })
|
|
3133
3134
|
* console.log(Array.from(numbers)) // [1, 2, 4]
|
|
3134
3135
|
*
|
|
@@ -3142,7 +3143,7 @@ export const flatten = <A>(self: Iterable<Iterable<A>>): Iterable<A> => ({
|
|
|
3142
3143
|
* const adultEmails = Iterable.filterMap(
|
|
3143
3144
|
* users,
|
|
3144
3145
|
* (user) =>
|
|
3145
|
-
* user.age >= 18 && user.email ?
|
|
3146
|
+
* user.age >= 18 && user.email ? Result.succeed(user.email) : Result.failVoid
|
|
3146
3147
|
* )
|
|
3147
3148
|
* console.log(Array.from(adultEmails)) // ["alice@example.com", "charlie@example.com"]
|
|
3148
3149
|
*
|
|
@@ -3150,7 +3151,7 @@ export const flatten = <A>(self: Iterable<Iterable<A>>): Iterable<A> => ({
|
|
|
3150
3151
|
* const items = ["a", "b", "c", "d", "e"]
|
|
3151
3152
|
* const evenIndexItems = Iterable.filterMap(
|
|
3152
3153
|
* items,
|
|
3153
|
-
* (item, i) => i % 2 === 0 ?
|
|
3154
|
+
* (item, i) => i % 2 === 0 ? Result.succeed(`${i}: ${item}`) : Result.failVoid
|
|
3154
3155
|
* )
|
|
3155
3156
|
* console.log(Array.from(evenIndexItems)) // ["0: a", "2: c", "4: e"]
|
|
3156
3157
|
* ```
|
|
@@ -3160,21 +3161,21 @@ export const flatten = <A>(self: Iterable<Iterable<A>>): Iterable<A> => ({
|
|
|
3160
3161
|
*/
|
|
3161
3162
|
export const filterMap: {
|
|
3162
3163
|
/**
|
|
3163
|
-
* Transforms elements of an iterable using a function that returns
|
|
3164
|
+
* Transforms elements of an iterable using a function that returns a `Result`, keeping only successful values.
|
|
3164
3165
|
*
|
|
3165
3166
|
* This combines mapping and filtering in a single operation - the function is applied to each element,
|
|
3166
|
-
* and only elements that result in
|
|
3167
|
+
* and only elements that result in `Result.succeed` are included in the result.
|
|
3167
3168
|
*
|
|
3168
3169
|
* @example
|
|
3169
3170
|
* ```ts
|
|
3170
3171
|
* import { Iterable } from "effect"
|
|
3171
|
-
* import * as
|
|
3172
|
+
* import * as Result from "effect/Result"
|
|
3172
3173
|
*
|
|
3173
3174
|
* // Parse strings to numbers, keeping only valid ones
|
|
3174
3175
|
* const strings = ["1", "2", "invalid", "4", "not-a-number"]
|
|
3175
3176
|
* const numbers = Iterable.filterMap(strings, (s) => {
|
|
3176
3177
|
* const num = parseInt(s)
|
|
3177
|
-
* return isNaN(num) ?
|
|
3178
|
+
* return isNaN(num) ? Result.failVoid : Result.succeed(num)
|
|
3178
3179
|
* })
|
|
3179
3180
|
* console.log(Array.from(numbers)) // [1, 2, 4]
|
|
3180
3181
|
*
|
|
@@ -3188,7 +3189,7 @@ export const filterMap: {
|
|
|
3188
3189
|
* const adultEmails = Iterable.filterMap(
|
|
3189
3190
|
* users,
|
|
3190
3191
|
* (user) =>
|
|
3191
|
-
* user.age >= 18 && user.email ?
|
|
3192
|
+
* user.age >= 18 && user.email ? Result.succeed(user.email) : Result.failVoid
|
|
3192
3193
|
* )
|
|
3193
3194
|
* console.log(Array.from(adultEmails)) // ["alice@example.com", "charlie@example.com"]
|
|
3194
3195
|
*
|
|
@@ -3196,7 +3197,7 @@ export const filterMap: {
|
|
|
3196
3197
|
* const items = ["a", "b", "c", "d", "e"]
|
|
3197
3198
|
* const evenIndexItems = Iterable.filterMap(
|
|
3198
3199
|
* items,
|
|
3199
|
-
* (item, i) => i % 2 === 0 ?
|
|
3200
|
+
* (item, i) => i % 2 === 0 ? Result.succeed(`${i}: ${item}`) : Result.failVoid
|
|
3200
3201
|
* )
|
|
3201
3202
|
* console.log(Array.from(evenIndexItems)) // ["0: a", "2: c", "4: e"]
|
|
3202
3203
|
* ```
|
|
@@ -3204,23 +3205,23 @@ export const filterMap: {
|
|
|
3204
3205
|
* @category filtering
|
|
3205
3206
|
* @since 2.0.0
|
|
3206
3207
|
*/
|
|
3207
|
-
<A, B>(f:
|
|
3208
|
+
<A, B, X>(f: Filter.Filter<A, B, X, [i: number]>): (self: Iterable<A>) => Iterable<B>
|
|
3208
3209
|
/**
|
|
3209
|
-
* Transforms elements of an iterable using a function that returns
|
|
3210
|
+
* Transforms elements of an iterable using a function that returns a `Result`, keeping only successful values.
|
|
3210
3211
|
*
|
|
3211
3212
|
* This combines mapping and filtering in a single operation - the function is applied to each element,
|
|
3212
|
-
* and only elements that result in
|
|
3213
|
+
* and only elements that result in `Result.succeed` are included in the result.
|
|
3213
3214
|
*
|
|
3214
3215
|
* @example
|
|
3215
3216
|
* ```ts
|
|
3216
3217
|
* import { Iterable } from "effect"
|
|
3217
|
-
* import * as
|
|
3218
|
+
* import * as Result from "effect/Result"
|
|
3218
3219
|
*
|
|
3219
3220
|
* // Parse strings to numbers, keeping only valid ones
|
|
3220
3221
|
* const strings = ["1", "2", "invalid", "4", "not-a-number"]
|
|
3221
3222
|
* const numbers = Iterable.filterMap(strings, (s) => {
|
|
3222
3223
|
* const num = parseInt(s)
|
|
3223
|
-
* return isNaN(num) ?
|
|
3224
|
+
* return isNaN(num) ? Result.failVoid : Result.succeed(num)
|
|
3224
3225
|
* })
|
|
3225
3226
|
* console.log(Array.from(numbers)) // [1, 2, 4]
|
|
3226
3227
|
*
|
|
@@ -3234,7 +3235,7 @@ export const filterMap: {
|
|
|
3234
3235
|
* const adultEmails = Iterable.filterMap(
|
|
3235
3236
|
* users,
|
|
3236
3237
|
* (user) =>
|
|
3237
|
-
* user.age >= 18 && user.email ?
|
|
3238
|
+
* user.age >= 18 && user.email ? Result.succeed(user.email) : Result.failVoid
|
|
3238
3239
|
* )
|
|
3239
3240
|
* console.log(Array.from(adultEmails)) // ["alice@example.com", "charlie@example.com"]
|
|
3240
3241
|
*
|
|
@@ -3242,7 +3243,7 @@ export const filterMap: {
|
|
|
3242
3243
|
* const items = ["a", "b", "c", "d", "e"]
|
|
3243
3244
|
* const evenIndexItems = Iterable.filterMap(
|
|
3244
3245
|
* items,
|
|
3245
|
-
* (item, i) => i % 2 === 0 ?
|
|
3246
|
+
* (item, i) => i % 2 === 0 ? Result.succeed(`${i}: ${item}`) : Result.failVoid
|
|
3246
3247
|
* )
|
|
3247
3248
|
* console.log(Array.from(evenIndexItems)) // ["0: a", "2: c", "4: e"]
|
|
3248
3249
|
* ```
|
|
@@ -3250,10 +3251,10 @@ export const filterMap: {
|
|
|
3250
3251
|
* @category filtering
|
|
3251
3252
|
* @since 2.0.0
|
|
3252
3253
|
*/
|
|
3253
|
-
<A, B>(self: Iterable<A>, f:
|
|
3254
|
+
<A, B, X>(self: Iterable<A>, f: Filter.Filter<A, B, X, [i: number]>): Iterable<B>
|
|
3254
3255
|
} = dual(
|
|
3255
3256
|
2,
|
|
3256
|
-
<A, B>(self: Iterable<A>, f:
|
|
3257
|
+
<A, B, X>(self: Iterable<A>, f: Filter.Filter<A, B, X, [i: number]>): Iterable<B> => ({
|
|
3257
3258
|
[Symbol.iterator]() {
|
|
3258
3259
|
const iterator = self[Symbol.iterator]()
|
|
3259
3260
|
let i = 0
|
|
@@ -3261,9 +3262,9 @@ export const filterMap: {
|
|
|
3261
3262
|
next() {
|
|
3262
3263
|
let result = iterator.next()
|
|
3263
3264
|
while (!result.done) {
|
|
3264
|
-
const
|
|
3265
|
-
if (
|
|
3266
|
-
return { done: false, value:
|
|
3265
|
+
const next = f(result.value, i++)
|
|
3266
|
+
if (R.isSuccess(next)) {
|
|
3267
|
+
return { done: false, value: next.success }
|
|
3267
3268
|
}
|
|
3268
3269
|
result = iterator.next()
|
|
3269
3270
|
}
|
|
@@ -3275,18 +3276,18 @@ export const filterMap: {
|
|
|
3275
3276
|
)
|
|
3276
3277
|
|
|
3277
3278
|
/**
|
|
3278
|
-
* Transforms all elements of the `Iterable` for as long as the specified function
|
|
3279
|
+
* Transforms all elements of the `Iterable` for as long as the specified function succeeds.
|
|
3279
3280
|
*
|
|
3280
3281
|
* @example
|
|
3281
3282
|
* ```ts
|
|
3282
3283
|
* import { Iterable } from "effect"
|
|
3283
|
-
* import * as
|
|
3284
|
+
* import * as Result from "effect/Result"
|
|
3284
3285
|
*
|
|
3285
3286
|
* // Parse numbers until we hit an invalid one
|
|
3286
3287
|
* const strings = ["1", "2", "3", "invalid", "4", "5"]
|
|
3287
3288
|
* const numbers = Iterable.filterMapWhile(strings, (s) => {
|
|
3288
3289
|
* const num = parseInt(s)
|
|
3289
|
-
* return isNaN(num) ?
|
|
3290
|
+
* return isNaN(num) ? Result.failVoid : Result.succeed(num)
|
|
3290
3291
|
* })
|
|
3291
3292
|
* console.log(Array.from(numbers)) // [1, 2, 3] (stops at "invalid")
|
|
3292
3293
|
*
|
|
@@ -3294,7 +3295,7 @@ export const filterMap: {
|
|
|
3294
3295
|
* const values = [2, 4, 6, 7, 8, 10]
|
|
3295
3296
|
* const doubledEvens = Iterable.filterMapWhile(
|
|
3296
3297
|
* values,
|
|
3297
|
-
* (n) => n % 2 === 0 ?
|
|
3298
|
+
* (n) => n % 2 === 0 ? Result.succeed(n * 2) : Result.failVoid
|
|
3298
3299
|
* )
|
|
3299
3300
|
* console.log(Array.from(doubledEvens)) // [4, 8, 12] (stops at 7)
|
|
3300
3301
|
*
|
|
@@ -3302,7 +3303,7 @@ export const filterMap: {
|
|
|
3302
3303
|
* const letters = ["a", "b", "c", "d", "e"]
|
|
3303
3304
|
* const indexedUntilC = Iterable.filterMapWhile(
|
|
3304
3305
|
* letters,
|
|
3305
|
-
* (letter, i) => letter !== "c" ?
|
|
3306
|
+
* (letter, i) => letter !== "c" ? Result.succeed(`${i}: ${letter}`) : Result.failVoid
|
|
3306
3307
|
* )
|
|
3307
3308
|
* console.log(Array.from(indexedUntilC)) // ["0: a", "1: b"] (stops at "c")
|
|
3308
3309
|
* ```
|
|
@@ -3312,18 +3313,18 @@ export const filterMap: {
|
|
|
3312
3313
|
*/
|
|
3313
3314
|
export const filterMapWhile: {
|
|
3314
3315
|
/**
|
|
3315
|
-
* Transforms all elements of the `Iterable` for as long as the specified function
|
|
3316
|
+
* Transforms all elements of the `Iterable` for as long as the specified function succeeds.
|
|
3316
3317
|
*
|
|
3317
3318
|
* @example
|
|
3318
3319
|
* ```ts
|
|
3319
3320
|
* import { Iterable } from "effect"
|
|
3320
|
-
* import * as
|
|
3321
|
+
* import * as Result from "effect/Result"
|
|
3321
3322
|
*
|
|
3322
3323
|
* // Parse numbers until we hit an invalid one
|
|
3323
3324
|
* const strings = ["1", "2", "3", "invalid", "4", "5"]
|
|
3324
3325
|
* const numbers = Iterable.filterMapWhile(strings, (s) => {
|
|
3325
3326
|
* const num = parseInt(s)
|
|
3326
|
-
* return isNaN(num) ?
|
|
3327
|
+
* return isNaN(num) ? Result.failVoid : Result.succeed(num)
|
|
3327
3328
|
* })
|
|
3328
3329
|
* console.log(Array.from(numbers)) // [1, 2, 3] (stops at "invalid")
|
|
3329
3330
|
*
|
|
@@ -3331,7 +3332,7 @@ export const filterMapWhile: {
|
|
|
3331
3332
|
* const values = [2, 4, 6, 7, 8, 10]
|
|
3332
3333
|
* const doubledEvens = Iterable.filterMapWhile(
|
|
3333
3334
|
* values,
|
|
3334
|
-
* (n) => n % 2 === 0 ?
|
|
3335
|
+
* (n) => n % 2 === 0 ? Result.succeed(n * 2) : Result.failVoid
|
|
3335
3336
|
* )
|
|
3336
3337
|
* console.log(Array.from(doubledEvens)) // [4, 8, 12] (stops at 7)
|
|
3337
3338
|
*
|
|
@@ -3339,7 +3340,7 @@ export const filterMapWhile: {
|
|
|
3339
3340
|
* const letters = ["a", "b", "c", "d", "e"]
|
|
3340
3341
|
* const indexedUntilC = Iterable.filterMapWhile(
|
|
3341
3342
|
* letters,
|
|
3342
|
-
* (letter, i) => letter !== "c" ?
|
|
3343
|
+
* (letter, i) => letter !== "c" ? Result.succeed(`${i}: ${letter}`) : Result.failVoid
|
|
3343
3344
|
* )
|
|
3344
3345
|
* console.log(Array.from(indexedUntilC)) // ["0: a", "1: b"] (stops at "c")
|
|
3345
3346
|
* ```
|
|
@@ -3347,20 +3348,20 @@ export const filterMapWhile: {
|
|
|
3347
3348
|
* @category filtering
|
|
3348
3349
|
* @since 2.0.0
|
|
3349
3350
|
*/
|
|
3350
|
-
<A, B>(f:
|
|
3351
|
+
<A, B, X>(f: Filter.Filter<A, B, X, [i: number]>): (self: Iterable<A>) => Iterable<B>
|
|
3351
3352
|
/**
|
|
3352
|
-
* Transforms all elements of the `Iterable` for as long as the specified function
|
|
3353
|
+
* Transforms all elements of the `Iterable` for as long as the specified function succeeds.
|
|
3353
3354
|
*
|
|
3354
3355
|
* @example
|
|
3355
3356
|
* ```ts
|
|
3356
3357
|
* import { Iterable } from "effect"
|
|
3357
|
-
* import * as
|
|
3358
|
+
* import * as Result from "effect/Result"
|
|
3358
3359
|
*
|
|
3359
3360
|
* // Parse numbers until we hit an invalid one
|
|
3360
3361
|
* const strings = ["1", "2", "3", "invalid", "4", "5"]
|
|
3361
3362
|
* const numbers = Iterable.filterMapWhile(strings, (s) => {
|
|
3362
3363
|
* const num = parseInt(s)
|
|
3363
|
-
* return isNaN(num) ?
|
|
3364
|
+
* return isNaN(num) ? Result.failVoid : Result.succeed(num)
|
|
3364
3365
|
* })
|
|
3365
3366
|
* console.log(Array.from(numbers)) // [1, 2, 3] (stops at "invalid")
|
|
3366
3367
|
*
|
|
@@ -3368,7 +3369,7 @@ export const filterMapWhile: {
|
|
|
3368
3369
|
* const values = [2, 4, 6, 7, 8, 10]
|
|
3369
3370
|
* const doubledEvens = Iterable.filterMapWhile(
|
|
3370
3371
|
* values,
|
|
3371
|
-
* (n) => n % 2 === 0 ?
|
|
3372
|
+
* (n) => n % 2 === 0 ? Result.succeed(n * 2) : Result.failVoid
|
|
3372
3373
|
* )
|
|
3373
3374
|
* console.log(Array.from(doubledEvens)) // [4, 8, 12] (stops at 7)
|
|
3374
3375
|
*
|
|
@@ -3376,7 +3377,7 @@ export const filterMapWhile: {
|
|
|
3376
3377
|
* const letters = ["a", "b", "c", "d", "e"]
|
|
3377
3378
|
* const indexedUntilC = Iterable.filterMapWhile(
|
|
3378
3379
|
* letters,
|
|
3379
|
-
* (letter, i) => letter !== "c" ?
|
|
3380
|
+
* (letter, i) => letter !== "c" ? Result.succeed(`${i}: ${letter}`) : Result.failVoid
|
|
3380
3381
|
* )
|
|
3381
3382
|
* console.log(Array.from(indexedUntilC)) // ["0: a", "1: b"] (stops at "c")
|
|
3382
3383
|
* ```
|
|
@@ -3384,8 +3385,8 @@ export const filterMapWhile: {
|
|
|
3384
3385
|
* @category filtering
|
|
3385
3386
|
* @since 2.0.0
|
|
3386
3387
|
*/
|
|
3387
|
-
<A, B>(self: Iterable<A>, f:
|
|
3388
|
-
} = dual(2, <A, B>(self: Iterable<A>, f:
|
|
3388
|
+
<A, B, X>(self: Iterable<A>, f: Filter.Filter<A, B, X, [i: number]>): Iterable<B>
|
|
3389
|
+
} = dual(2, <A, B, X>(self: Iterable<A>, f: Filter.Filter<A, B, X, [i: number]>) => ({
|
|
3389
3390
|
[Symbol.iterator]() {
|
|
3390
3391
|
const iterator = self[Symbol.iterator]()
|
|
3391
3392
|
let i = 0
|
|
@@ -3395,9 +3396,9 @@ export const filterMapWhile: {
|
|
|
3395
3396
|
if (result.done) {
|
|
3396
3397
|
return { done: true, value: undefined }
|
|
3397
3398
|
}
|
|
3398
|
-
const
|
|
3399
|
-
if (
|
|
3400
|
-
return { done: false, value:
|
|
3399
|
+
const next = f(result.value, i++)
|
|
3400
|
+
if (R.isSuccess(next)) {
|
|
3401
|
+
return { done: false, value: next.success }
|
|
3401
3402
|
}
|
|
3402
3403
|
return { done: true, value: undefined }
|
|
3403
3404
|
}
|
|
@@ -3425,7 +3426,25 @@ export const filterMapWhile: {
|
|
|
3425
3426
|
* @category filtering
|
|
3426
3427
|
* @since 2.0.0
|
|
3427
3428
|
*/
|
|
3428
|
-
export const getSomes
|
|
3429
|
+
export const getSomes = <A>(self: Iterable<Option<A>>): Iterable<A> => {
|
|
3430
|
+
return {
|
|
3431
|
+
[Symbol.iterator]() {
|
|
3432
|
+
const iterator = self[Symbol.iterator]()
|
|
3433
|
+
return {
|
|
3434
|
+
next() {
|
|
3435
|
+
let result = iterator.next()
|
|
3436
|
+
while (!result.done) {
|
|
3437
|
+
if (O.isSome(result.value)) {
|
|
3438
|
+
return { done: false, value: result.value.value }
|
|
3439
|
+
}
|
|
3440
|
+
result = iterator.next()
|
|
3441
|
+
}
|
|
3442
|
+
return { done: true, value: undefined }
|
|
3443
|
+
}
|
|
3444
|
+
}
|
|
3445
|
+
}
|
|
3446
|
+
}
|
|
3447
|
+
}
|
|
3429
3448
|
|
|
3430
3449
|
/**
|
|
3431
3450
|
* Retrieves the `Err` values from an `Iterable` of `Result`s.
|
|
@@ -3451,7 +3470,25 @@ export const getSomes: <A>(self: Iterable<Option<A>>) => Iterable<A> = filterMap
|
|
|
3451
3470
|
* @category filtering
|
|
3452
3471
|
* @since 2.0.0
|
|
3453
3472
|
*/
|
|
3454
|
-
export const getFailures = <
|
|
3473
|
+
export const getFailures = <R0, L>(self: Iterable<Result<R0, L>>): Iterable<L> => {
|
|
3474
|
+
return {
|
|
3475
|
+
[Symbol.iterator]() {
|
|
3476
|
+
const iterator = self[Symbol.iterator]()
|
|
3477
|
+
return {
|
|
3478
|
+
next() {
|
|
3479
|
+
let result = iterator.next()
|
|
3480
|
+
while (!result.done) {
|
|
3481
|
+
if (R.isFailure(result.value)) {
|
|
3482
|
+
return { done: false, value: result.value.failure }
|
|
3483
|
+
}
|
|
3484
|
+
result = iterator.next()
|
|
3485
|
+
}
|
|
3486
|
+
return { done: true, value: undefined }
|
|
3487
|
+
}
|
|
3488
|
+
}
|
|
3489
|
+
}
|
|
3490
|
+
}
|
|
3491
|
+
}
|
|
3455
3492
|
|
|
3456
3493
|
/**
|
|
3457
3494
|
* Retrieves the `Ok` values from an `Iterable` of `Result`s.
|
|
@@ -3477,7 +3514,25 @@ export const getFailures = <R, L>(self: Iterable<Result<R, L>>): Iterable<L> =>
|
|
|
3477
3514
|
* @category filtering
|
|
3478
3515
|
* @since 2.0.0
|
|
3479
3516
|
*/
|
|
3480
|
-
export const getSuccesses = <
|
|
3517
|
+
export const getSuccesses = <R0, L>(self: Iterable<Result<R0, L>>): Iterable<R0> => {
|
|
3518
|
+
return {
|
|
3519
|
+
[Symbol.iterator]() {
|
|
3520
|
+
const iterator = self[Symbol.iterator]()
|
|
3521
|
+
return {
|
|
3522
|
+
next() {
|
|
3523
|
+
let result = iterator.next()
|
|
3524
|
+
while (!result.done) {
|
|
3525
|
+
if (R.isSuccess(result.value)) {
|
|
3526
|
+
return { done: false, value: result.value.success }
|
|
3527
|
+
}
|
|
3528
|
+
result = iterator.next()
|
|
3529
|
+
}
|
|
3530
|
+
return { done: true, value: undefined }
|
|
3531
|
+
}
|
|
3532
|
+
}
|
|
3533
|
+
}
|
|
3534
|
+
}
|
|
3535
|
+
}
|
|
3481
3536
|
|
|
3482
3537
|
/**
|
|
3483
3538
|
* Filters an iterable to only include elements that match a predicate.
|
|
@@ -3855,7 +3910,7 @@ export const flatMapNullishOr: {
|
|
|
3855
3910
|
<A, B>(self: Iterable<A>, f: (a: A) => B): Iterable<NonNullable<B>> =>
|
|
3856
3911
|
filterMap(self, (a) => {
|
|
3857
3912
|
const b = f(a)
|
|
3858
|
-
return b == null ?
|
|
3913
|
+
return b == null ? R.failVoid : R.succeed(b)
|
|
3859
3914
|
})
|
|
3860
3915
|
)
|
|
3861
3916
|
|