@@ -76,7 +76,7 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
76
76
* @return [None] if both [oa] and [ob] are [None]. Otherwise [Some] wrapping
77
77
* an [Ior.Left], [Ior.Right], or [Ior.Both] if [oa], [ob], or both are defined (respectively).
78
78
*/
79
-
79
+ @Deprecated( " Deprecated, use `fromNullables` instead " , ReplaceWith ( " fromNullables(a, b) " ))
80
80
fun <A , B > fromOptions (oa : Option <A >, ob : Option <B >): Option <Ior <A , B >> = when (oa) {
81
81
is Some -> when (ob) {
82
82
is Some -> Some (Both (oa.t, ob.t))
@@ -88,6 +88,27 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
88
88
}
89
89
}
90
90
91
+ /* *
92
+ * Create an [Ior] from two nullables if at least one of them is defined.
93
+ *
94
+ * @param a an element (nullable) for the left side of the [Ior]
95
+ * @param b an element (nullable) for the right side of the [Ior]
96
+ *
97
+ * @return [null] if both [a] and [b] are [null]. Otherwise
98
+ * an [Ior.Left], [Ior.Right], or [Ior.Both] if [a], [b], or both are defined (respectively).
99
+ */
100
+ fun <A , B > fromNullables (a : A ? , b : B ? ): Ior <A , B >? =
101
+ when (a != null ) {
102
+ true -> when (b != null ) {
103
+ true -> Both (a, b)
104
+ false -> Left (a)
105
+ }
106
+ false -> when (b != null ) {
107
+ true -> Right (b)
108
+ false -> null
109
+ }
110
+ }
111
+
91
112
private tailrec fun <L , A , B > Semigroup<L>.loop (v : Ior <L , Either <A , B >>, f : (A ) -> IorOf <L , Either <A , B >>): Ior <L , B > = when (v) {
92
113
is Left -> Left (v.value)
93
114
is Right -> when (v.value) {
@@ -235,17 +256,44 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
235
256
*
236
257
* Example:
237
258
* ```
238
- * Right(12).pad() // Result: Pair(None, Some(12))
239
- * Left(12).pad() // Result: Pair(Some(12), None)
240
- * Both("power", 12).pad() // Result: Pair(Some("power"), Some(12))
259
+ * Ior. Right(12).pad() // Result: Pair(None, Some(12))
260
+ * Ior. Left(12).pad() // Result: Pair(Some(12), None)
261
+ * Ior. Both("power", 12).pad() // Result: Pair(Some("power"), Some(12))
241
262
* ```
242
263
*/
264
+ @Deprecated(" Deprecated, use `padNull` instead" , ReplaceWith (" padNull()" ))
243
265
fun pad (): Pair <Option <A >, Option<B>> = fold(
244
266
{ Pair (Some (it), None ) },
245
267
{ Pair (None , Some (it)) },
246
268
{ a, b -> Pair (Some (a), Some (b)) }
247
269
)
248
270
271
+ /* *
272
+ * Return this [Ior] as [Pair] of nullables]
273
+ *
274
+ * Example:
275
+ * ```kotlin:ank:playground
276
+ * import arrow.core.Ior
277
+ *
278
+ * //sampleStart
279
+ * val right = Ior.Right(12).padNull() // Result: Pair(null, 12)
280
+ * val left = Ior.Left(12).padNull() // Result: Pair(12, null)
281
+ * val both = Ior.Both("power", 12).padNull() // Result: Pair("power", 12)
282
+ * //sampleEnd
283
+ *
284
+ * fun main() {
285
+ * println("right = $right")
286
+ * println("left = $left")
287
+ * println("both = $both")
288
+ * }
289
+ * ```
290
+ */
291
+ fun padNull (): Pair <A ?, B ?> = fold(
292
+ { Pair (it, null ) },
293
+ { Pair (null , it) },
294
+ { a, b -> Pair (a, b) }
295
+ )
296
+
249
297
/* *
250
298
* Returns a [Either.Right] containing the [Right] value or `B` if this is [Right] or [Both]
251
299
* and [Either.Left] if this is a [Left].
@@ -271,9 +319,33 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
271
319
* Both(12, "power").toOption() // Result: Some("power")
272
320
* ```
273
321
*/
322
+ @Deprecated(" Deprecated, use `orNull` instead" , ReplaceWith (" orNull()" ))
274
323
fun toOption (): Option <B > =
275
324
fold({ None }, { Some (it) }, { _, b -> Some (b) })
276
325
326
+ /* *
327
+ * Returns the [Right] value or `B` if this is [Right] or [Both]
328
+ * and [null] if this is a [Left].
329
+ *
330
+ * Example:
331
+ * ```kotlin:ank:playground
332
+ * import arrow.core.Ior
333
+ *
334
+ * //sampleStart
335
+ * val right = Ior.Right(12).orNull() // Result: 12
336
+ * val left = Ior.Left(12).orNull() // Result: null
337
+ * val both = Ior.Both(12, "power").orNull() // Result: "power"
338
+ * //sampleEnd
339
+ * fun main() {
340
+ * println("right = $right")
341
+ * println("left = $left")
342
+ * println("both = $both")
343
+ * }
344
+ * ```
345
+ */
346
+ fun orNull (): B ? =
347
+ fold({ null }, { it }, { _, b -> b })
348
+
277
349
/* *
278
350
* Returns a [Some] containing the [Left] value or `A` if this is [Left] or [Both]
279
351
* and [None] if this is a [Right].
@@ -285,9 +357,34 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
285
357
* Both(12, "power").toLeftOption() // Result: Some(12)
286
358
* ```
287
359
*/
360
+ @Deprecated(" Deprecated, use `leftOrNull` instead" , ReplaceWith (" leftOrNull()" ))
288
361
fun toLeftOption (): Option <A > =
289
362
fold({ Option .just(it) }, { Option .empty() }, { a, _ -> Option .just(a) })
290
363
364
+ /* *
365
+ * Returns the [Left] value or `A` if this is [Left] or [Both]
366
+ * and [null] if this is a [Right].
367
+ *
368
+ * Example:
369
+ * ```kotlin:ank:playground
370
+ * import arrow.core.Ior
371
+ *
372
+ * //sampleStart
373
+ * val right = Ior.Right(12).leftOrNull() // Result: null
374
+ * val left = Ior.Left(12).leftOrNull() // Result: 12
375
+ * val both = Ior.Both(12, "power").leftOrNull() // Result: 12
376
+ * //sampleEnd
377
+ *
378
+ * fun main() {
379
+ * println("right = $right")
380
+ * println("left = $left")
381
+ * println("both = $both")
382
+ * }
383
+ * ```
384
+ */
385
+ fun leftOrNull (): A ? =
386
+ fold({ it }, { null }, { a, _ -> a })
387
+
291
388
/* *
292
389
* Returns a [Validated.Valid] containing the [Right] value or `B` if this is [Right] or [Both]
293
390
* and [Validated.Invalid] if this is a [Left].
0 commit comments