From ac1c7c512d22ad62dd6a04ce6c2b4fd5608ebee6 Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Tue, 16 Aug 2022 20:56:12 +0200 Subject: [PATCH 01/20] Shift without suspend, inline all the rest --- .../arrow/core/continuations/Builders.kt | 143 +++ .../arrow/core/continuations/EagerEffect.kt | 204 ----- .../core/continuations/EagerEffectScope.kt | 269 ------ .../kotlin/arrow/core/continuations/Effect.kt | 831 ------------------ .../arrow/core/continuations/EffectScope.kt | 294 ------- .../arrow/core/continuations/ErrorHandlers.kt | 48 + .../kotlin/arrow/core/continuations/Fold.kt | 67 ++ .../arrow/core/continuations/Mappers.kt | 37 + .../kotlin/arrow/core/continuations/Shift.kt | 89 ++ .../kotlin/arrow/core/continuations/either.kt | 12 - .../kotlin/arrow/core/continuations/ior.kt | 83 -- .../arrow/core/continuations/nullable.kt | 67 -- .../kotlin/arrow/core/continuations/option.kt | 64 -- .../kotlin/arrow/core/continuations/result.kt | 41 - .../core/continuations/EagerEffectSpec.kt | 18 +- .../arrow/core/continuations/EffectSpec.kt | 24 +- .../arrow/core/continuations/IorSpec.kt | 4 +- .../arrow/core/continuations/OptionSpec.kt | 2 +- .../StructuredConcurrencySpec.kt | 6 +- .../kotlin/arrow/core/continuations/predef.kt | 4 +- .../arrow/core/continuations/EffectUsage.kt | 10 +- .../example-arrow-core-either-contains-01.kt | 12 - .../examples/example-eager-effect-01.kt | 17 - .../examples/example-eager-effect-02.kt | 26 - .../examples/example-eager-effect-scope-01.kt | 12 - .../examples/example-eager-effect-scope-02.kt | 20 - .../examples/example-eager-effect-scope-03.kt | 14 - .../examples/example-eager-effect-scope-04.kt | 14 - .../examples/example-eager-effect-scope-05.kt | 15 - .../examples/example-eager-effect-scope-06.kt | 18 - .../examples/example-eager-effect-scope-07.kt | 16 - .../examples/example-eager-effect-scope-08.kt | 20 - .../examples/example-eager-effect-scope-09.kt | 16 - .../kotlin/examples/example-effect-01.kt | 17 - .../kotlin/examples/example-effect-02.kt | 26 - .../examples/example-effect-guide-01.kt | 17 - .../examples/example-effect-guide-02.kt | 49 -- .../examples/example-effect-guide-03.kt | 15 - .../examples/example-effect-guide-04.kt | 33 - .../examples/example-effect-guide-05.kt | 24 - .../examples/example-effect-guide-06.kt | 31 - .../examples/example-effect-guide-07.kt | 30 - .../examples/example-effect-guide-08.kt | 28 - .../examples/example-effect-guide-09.kt | 30 - .../examples/example-effect-guide-10.kt | 57 -- .../examples/example-effect-guide-11.kt | 20 - .../examples/example-effect-guide-12.kt | 21 - .../examples/example-effect-guide-13.kt | 24 - .../examples/example-effect-scope-01.kt | 12 - .../examples/example-effect-scope-02.kt | 20 - .../examples/example-effect-scope-03.kt | 21 - .../examples/example-effect-scope-04.kt | 14 - .../examples/example-effect-scope-05.kt | 14 - .../examples/example-effect-scope-06.kt | 15 - .../examples/example-effect-scope-07.kt | 18 - .../examples/example-effect-scope-08.kt | 16 - .../examples/example-effect-scope-09.kt | 20 - .../examples/example-effect-scope-10.kt | 16 - .../kotlin/examples/example-either-01.kt | 12 - .../kotlin/examples/example-either-02.kt | 10 - .../kotlin/examples/example-either-03.kt | 10 - .../kotlin/examples/example-either-04.kt | 11 - .../kotlin/examples/example-either-05.kt | 11 - .../kotlin/examples/example-either-06.kt | 15 - .../kotlin/examples/example-either-07.kt | 21 - .../kotlin/examples/example-either-08.kt | 15 - .../kotlin/examples/example-either-09.kt | 27 - .../kotlin/examples/example-either-10.kt | 31 - .../kotlin/examples/example-either-11.kt | 24 - .../kotlin/examples/example-either-12.kt | 35 - .../kotlin/examples/example-either-13.kt | 16 - .../kotlin/examples/example-either-14.kt | 79 -- .../kotlin/examples/example-either-15.kt | 13 - .../kotlin/examples/example-either-16.kt | 11 - .../kotlin/examples/example-either-17.kt | 10 - .../kotlin/examples/example-either-18.kt | 10 - .../kotlin/examples/example-either-19.kt | 11 - .../kotlin/examples/example-either-20.kt | 11 - .../kotlin/examples/example-either-21.kt | 11 - .../kotlin/examples/example-either-22.kt | 10 - .../kotlin/examples/example-either-23.kt | 10 - .../kotlin/examples/example-either-24.kt | 11 - .../kotlin/examples/example-either-25.kt | 11 - .../kotlin/examples/example-either-26.kt | 16 - .../kotlin/examples/example-either-27.kt | 13 - .../kotlin/examples/example-either-28.kt | 11 - .../kotlin/examples/example-either-29.kt | 11 - .../kotlin/examples/example-either-30.kt | 10 - .../kotlin/examples/example-either-31.kt | 10 - .../kotlin/examples/example-either-32.kt | 10 - .../kotlin/examples/example-either-33.kt | 10 - .../kotlin/examples/example-either-34.kt | 16 - .../kotlin/examples/example-either-35.kt | 9 - .../kotlin/examples/example-either-36.kt | 9 - .../kotlin/examples/example-either-37.kt | 9 - .../kotlin/examples/example-either-38.kt | 9 - .../kotlin/examples/example-either-39.kt | 9 - .../kotlin/examples/example-either-40.kt | 13 - .../kotlin/examples/example-either-41.kt | 12 - .../kotlin/examples/example-either-42.kt | 10 - .../kotlin/examples/example-either-43.kt | 11 - .../kotlin/examples/example-either-44.kt | 3 - .../kotlin/examples/example-either-45.kt | 11 - .../kotlin/examples/example-either-46.kt | 10 - .../kotlin/examples/example-either-47.kt | 11 - .../kotlin/examples/example-either-48.kt | 14 - .../kotlin/examples/example-either-49.kt | 17 - .../kotlin/examples/example-either-50.kt | 11 - .../kotlin/examples/example-either-51.kt | 12 - .../kotlin/examples/example-either-52.kt | 9 - .../kotlin/examples/example-either-53.kt | 13 - .../example-either-computations-01.kt | 16 - .../example-either-computations-02.kt | 17 - .../kotlin/examples/example-eval-01.kt | 21 - .../kotlin/examples/example-eval-02.kt | 9 - .../kotlin/examples/example-eval-03.kt | 9 - .../kotlin/examples/example-eval-04.kt | 9 - .../jvmTest/kotlin/examples/example-ior-01.kt | 10 - .../jvmTest/kotlin/examples/example-ior-02.kt | 10 - .../jvmTest/kotlin/examples/example-ior-03.kt | 10 - .../jvmTest/kotlin/examples/example-ior-04.kt | 10 - .../jvmTest/kotlin/examples/example-ior-05.kt | 10 - .../jvmTest/kotlin/examples/example-ior-06.kt | 10 - .../jvmTest/kotlin/examples/example-ior-07.kt | 10 - .../jvmTest/kotlin/examples/example-ior-08.kt | 14 - .../jvmTest/kotlin/examples/example-ior-09.kt | 10 - .../jvmTest/kotlin/examples/example-ior-10.kt | 14 - .../jvmTest/kotlin/examples/example-ior-11.kt | 13 - .../jvmTest/kotlin/examples/example-ior-12.kt | 10 - .../jvmTest/kotlin/examples/example-ior-13.kt | 12 - .../jvmTest/kotlin/examples/example-ior-14.kt | 13 - .../kotlin/examples/example-iterable-01.kt | 14 - .../kotlin/examples/example-iterable-02.kt | 14 - .../kotlin/examples/example-iterable-03.kt | 14 - .../kotlin/examples/example-iterable-04.kt | 14 - .../kotlin/examples/example-iterable-05.kt | 14 - .../kotlin/examples/example-iterable-06.kt | 14 - .../kotlin/examples/example-iterable-07.kt | 14 - .../kotlin/examples/example-iterable-08.kt | 12 - .../kotlin/examples/example-iterable-09.kt | 12 - .../kotlin/examples/example-iterable-10.kt | 16 - .../kotlin/examples/example-iterable-11.kt | 13 - .../kotlin/examples/example-iterable-12.kt | 14 - .../kotlin/examples/example-iterable-13.kt | 12 - .../kotlin/examples/example-iterable-14.kt | 13 - .../kotlin/examples/example-iterable-15.kt | 12 - .../kotlin/examples/example-iterable-16.kt | 14 - .../jvmTest/kotlin/examples/example-map-01.kt | 12 - .../jvmTest/kotlin/examples/example-map-02.kt | 14 - .../jvmTest/kotlin/examples/example-map-03.kt | 12 - .../jvmTest/kotlin/examples/example-map-04.kt | 14 - .../jvmTest/kotlin/examples/example-map-05.kt | 16 - .../jvmTest/kotlin/examples/example-map-06.kt | 13 - .../jvmTest/kotlin/examples/example-map-07.kt | 12 - .../jvmTest/kotlin/examples/example-map-08.kt | 16 - .../kotlin/examples/example-memoization-01.kt | 19 - .../examples/example-nonemptylist-01.kt | 11 - .../examples/example-nonemptylist-02.kt | 10 - .../examples/example-nonemptylist-03.kt | 12 - .../examples/example-nonemptylist-04.kt | 10 - .../examples/example-nonemptylist-05.kt | 17 - .../examples/example-nonemptylist-06.kt | 21 - .../kotlin/examples/example-nonfatal-01.kt | 28 - .../examples/example-nonfatalorthrow-01.kt | 24 - .../example-nullable-computations-01.kt | 16 - .../example-nullable-computations-02.kt | 17 - .../kotlin/examples/example-option-01.kt | 13 - .../kotlin/examples/example-option-02.kt | 9 - .../kotlin/examples/example-option-03.kt | 17 - .../kotlin/examples/example-option-04.kt | 17 - .../kotlin/examples/example-option-05.kt | 17 - .../kotlin/examples/example-option-06.kt | 10 - .../kotlin/examples/example-option-07.kt | 15 - .../kotlin/examples/example-option-08.kt | 15 - .../kotlin/examples/example-option-09.kt | 17 - .../kotlin/examples/example-option-10.kt | 11 - .../kotlin/examples/example-option-11.kt | 11 - .../kotlin/examples/example-option-12.kt | 12 - .../kotlin/examples/example-option-13.kt | 14 - .../kotlin/examples/example-option-14.kt | 16 - .../kotlin/examples/example-option-15.kt | 12 - .../kotlin/examples/example-option-16.kt | 10 - .../kotlin/examples/example-option-17.kt | 17 - .../kotlin/examples/example-option-18.kt | 18 - .../kotlin/examples/example-option-19.kt | 10 - .../kotlin/examples/example-option-20.kt | 10 - .../kotlin/examples/example-option-21.kt | 14 - .../kotlin/examples/example-option-22.kt | 14 - .../kotlin/examples/example-option-23.kt | 3 - .../example-option-computations-01.kt | 16 - .../example-option-computations-02.kt | 17 - .../example-result-computations-01.kt | 23 - .../kotlin/examples/example-semiring-01.kt | 12 - .../kotlin/examples/example-semiring-02.kt | 12 - .../kotlin/examples/example-semiring-03.kt | 14 - .../kotlin/examples/example-semiring-04.kt | 14 - .../kotlin/examples/example-sequence-01.kt | 14 - .../kotlin/examples/example-sequence-02.kt | 12 - .../kotlin/examples/example-sequence-03.kt | 14 - .../kotlin/examples/example-sequence-04.kt | 13 - .../kotlin/examples/example-sequence-05.kt | 14 - .../kotlin/examples/example-sequence-06.kt | 14 - .../kotlin/examples/example-sequence-07.kt | 14 - .../kotlin/examples/example-sequence-08.kt | 14 - .../kotlin/examples/example-sequence-09.kt | 14 - .../kotlin/examples/example-sequence-10.kt | 14 - .../kotlin/examples/example-sequence-11.kt | 11 - .../kotlin/examples/example-sequence-12.kt | 13 - .../kotlin/examples/example-sequence-13.kt | 12 - .../kotlin/examples/example-sequence-14.kt | 11 - .../kotlin/examples/example-sequence-15.kt | 11 - .../kotlin/examples/example-sequence-16.kt | 16 - .../kotlin/examples/example-validated-01.kt | 14 - .../kotlin/examples/example-validated-02.kt | 12 - .../kotlin/examples/example-validated-03.kt | 9 - .../kotlin/examples/example-validated-04.kt | 9 - .../kotlin/examples/example-validated-05.kt | 13 - .../kotlin/examples/example-validated-06.kt | 11 - 218 files changed, 414 insertions(+), 4872 deletions(-) create mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt delete mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EagerEffect.kt delete mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EagerEffectScope.kt delete mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt delete mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EffectScope.kt create mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt create mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt create mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt create mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt delete mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/either.kt delete mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ior.kt delete mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/nullable.kt delete mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/option.kt delete mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/result.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-arrow-core-either-contains-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-03.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-04.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-05.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-06.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-07.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-08.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-09.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-03.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-04.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-05.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-06.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-07.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-08.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-09.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-10.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-11.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-12.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-13.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-03.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-04.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-05.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-06.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-07.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-08.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-09.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-10.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-03.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-04.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-05.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-06.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-07.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-08.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-09.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-10.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-11.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-12.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-13.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-14.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-15.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-16.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-17.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-18.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-19.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-20.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-21.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-22.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-23.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-24.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-25.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-26.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-27.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-28.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-29.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-30.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-31.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-32.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-33.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-34.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-35.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-36.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-37.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-38.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-39.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-40.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-41.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-42.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-43.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-44.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-45.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-46.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-47.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-48.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-49.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-50.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-51.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-52.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-53.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-03.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-04.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-03.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-04.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-05.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-06.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-07.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-08.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-09.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-10.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-11.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-12.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-13.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-14.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-03.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-04.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-05.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-06.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-07.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-08.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-09.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-10.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-11.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-12.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-13.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-14.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-15.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-16.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-03.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-04.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-05.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-06.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-07.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-08.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-memoization-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-03.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-04.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-05.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-06.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatal-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatalorthrow-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-03.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-04.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-05.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-06.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-07.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-08.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-09.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-10.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-11.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-12.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-13.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-14.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-15.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-16.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-17.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-18.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-19.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-20.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-21.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-22.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-23.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-result-computations-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-03.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-04.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-03.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-04.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-05.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-06.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-07.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-08.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-09.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-10.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-11.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-12.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-13.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-14.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-15.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-16.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-01.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-02.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-03.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-04.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-05.kt delete mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-06.kt diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt new file mode 100644 index 00000000000..5dd670462dc --- /dev/null +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt @@ -0,0 +1,143 @@ +@file:JvmMultifileClass +@file:JvmName("Effect") + +package arrow.core.continuations + +import arrow.core.Either +import arrow.core.EmptyValue +import arrow.core.Ior +import arrow.core.None +import arrow.core.Option +import arrow.core.Some +import arrow.core.identity +import arrow.typeclasses.Semigroup +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.contract +import kotlin.experimental.ExperimentalTypeInference +import kotlin.jvm.JvmInline +import kotlin.jvm.JvmMultifileClass +import kotlin.jvm.JvmName + +public typealias EagerEffect = Shift.() -> A + +public typealias Effect = suspend Shift.() -> A + +@OptIn(ExperimentalTypeInference::class) +public inline fun eagerEffect(@BuilderInference noinline f: Shift.() -> A): EagerEffect = f + +@OptIn(ExperimentalTypeInference::class) +public inline fun effect(@BuilderInference noinline f: suspend Shift.() -> A): Effect = f + +public inline fun either(action: Shift.() -> A): Either = + fold>( + { action() }, + { throw it }, + { Either.Left(it) }, + { Either.Right(it) } + ) + +public inline fun nullable(f: NullableShift.() -> A): A? = + fold( + { f(NullableShift(this)) }, + { throw it }, + { null }, + { it } + ) + +@JvmInline +public value class NullableShift(private val cont: Shift) : Shift { + @EffectDSL + public fun ensure(value: Boolean): Unit = ensure(value) { null } + override fun shift(r: Nothing?): B = cont.shift(r) + + public fun Option.bind(): B = bind { shift(null) } + + @OptIn(ExperimentalContracts::class) + public fun B?.bind(): B { + contract { returns() implies (this@bind != null) } + return this ?: shift(null) + } + + @OptIn(ExperimentalContracts::class) + public fun ensureNotNull(value: B?): B { + contract { returns() implies (value != null) } + return ensureNotNull(value) { null } + } +} + +public inline fun result(action: ResultShift.() -> A): Result = + fold>( + { action(ResultShift(this)) }, + { throw it }, + { Result.failure(it) }, + { Result.success(it) } + ) + +@JvmInline +public value class ResultShift(private val cont: Shift) : Shift { + override fun shift(r: Throwable): B = cont.shift(r) + public fun Result.bind(): B = fold(::identity) { shift(it) } +} + +public inline fun option(action: OptionShift.() -> A): Option = + fold>( + { action(OptionShift(this)) }, + { throw it }, + { None }, + { Some(it) } + ) + +@JvmInline +public value class OptionShift(private val cont: Shift) : Shift { + override fun shift(r: None): B = + cont.shift(r) + + // public fun Option.bind(): B = bind { None } + + public fun ensure(value: Boolean): Unit = + ensure(value) { None } + + @OptIn(ExperimentalContracts::class) + public fun ensureNotNull(value: B?): B { + contract { returns() implies (value != null) } + return ensureNotNull(value) { None } + } +} + +public inline fun ior( + semigroup: Semigroup, + action: IorShift.() -> A, +): Ior = fold, Ior>( + { + val effect = IorShift(semigroup, this) + val res = action(effect) + val leftState = effect.leftState.get() + if (leftState === EmptyValue) Ior.Right(res) else Ior.Both(EmptyValue.unbox(leftState), res) + }, + { throw it }, + { Ior.Left(it) }, + { it } +) + +public class IorShift(semigroup: Semigroup, private val effect: Shift) : Shift, Semigroup by semigroup { + + @PublishedApi + internal var leftState: AtomicRef = AtomicRef(EmptyValue) + + private fun combine(other: E): E = + leftState.updateAndGet { state -> + if (state === EmptyValue) other else EmptyValue.unbox(state).combine(other) + } as E + + public fun Ior.bind(): B = + when (this) { + is Ior.Left -> shift(value) + is Ior.Right -> value + is Ior.Both -> { + combine(leftValue) + rightValue + } + } + + override fun shift(r: E): B = effect.shift(combine(r)) +} diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EagerEffect.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EagerEffect.kt deleted file mode 100644 index e5a7269cb54..00000000000 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EagerEffect.kt +++ /dev/null @@ -1,204 +0,0 @@ -package arrow.core.continuations - -import arrow.core.Either -import arrow.core.Ior -import arrow.core.Option -import arrow.core.Some -import arrow.core.Validated -import arrow.core.identity -import arrow.core.nonFatalOrThrow -import kotlin.coroutines.Continuation -import kotlin.coroutines.EmptyCoroutineContext -import kotlin.coroutines.intrinsics.startCoroutineUninterceptedOrReturn -import kotlin.coroutines.RestrictsSuspension - -/** - * [RestrictsSuspension] version of [Effect]. This version runs eagerly, and can be used in - * non-suspending code. - * An [effect] computation interoperates with an [EagerEffect] via `bind`. - * @see Effect - */ -public interface EagerEffect { - - /** - * Runs the non-suspending computation by creating a [Continuation] with an [EmptyCoroutineContext], - * and running the `fold` function over the computation. - * - * When the [EagerEffect] has shifted with [R] it will [recover] the shifted value to [B], and when it - * ran the computation to completion it will [transform] the value [A] to [B]. - * - * ```kotlin - * import arrow.core.continuations.eagerEffect - * import io.kotest.matchers.shouldBe - * - * fun main() { - * val shift = eagerEffect { - * shift("Hello, World!") - * }.fold({ str: String -> str }, { int -> int.toString() }) - * shift shouldBe "Hello, World!" - * - * val res = eagerEffect { - * 1000 - * }.fold({ str: String -> str.length }, { int -> int }) - * res shouldBe 1000 - * } - * ``` - * - */ - public fun fold(recover: (R) -> B, transform: (A) -> B): B - - /** - * Like `fold` but also allows folding over any unexpected [Throwable] that might have occurred. - * @see fold - */ - public fun fold( - error: (error: Throwable) -> B, - recover: (shifted: R) -> B, - transform: (value: A) -> B - ): B = - try { - fold(recover, transform) - } catch (e: Throwable) { - error(e.nonFatalOrThrow()) - } - - /** - * [fold] the [EagerEffect] into an [Ior]. Where the shifted value [R] is mapped to [Ior.Left], and - * result value [A] is mapped to [Ior.Right]. - */ - public fun toIor(): Ior = fold({ Ior.Left(it) }) { Ior.Right(it) } - - /** - * [fold] the [EagerEffect] into an [Either]. Where the shifted value [R] is mapped to [Either.Left], and - * result value [A] is mapped to [Either.Right]. - */ - public fun toEither(): Either = fold({ Either.Left(it) }) { Either.Right(it) } - - /** - * [fold] the [EagerEffect] into an [Validated]. Where the shifted value [R] is mapped to - * [Validated.Invalid], and result value [A] is mapped to [Validated.Valid]. - */ - public fun toValidated(): Validated = - fold({ Validated.Invalid(it) }) { Validated.Valid(it) } - - /** - * [fold] the [EagerEffect] into an [A?]. Where the shifted value [R] is mapped to - * [null], and result value [A]. - */ - public fun orNull(): A? = fold({ null }, ::identity) - - /** - * [fold] the [EagerEffect] into an [Option]. Where the shifted value [R] is mapped to [Option] by the - * provided function [orElse], and result value [A] is mapped to [Some]. - */ - public fun toOption(orElse: (R) -> Option<@UnsafeVariance A>): Option = - fold(orElse, ::Some) - - @Deprecated(deprecateMonadAppFunctorOperators, ReplaceWith("flatMap { eagerEffect { f(it) } }")) - public fun map(f: (A) -> B): EagerEffect = flatMap { a -> eagerEffect { f(a) } } - - @Deprecated(deprecateMonadAppFunctorOperators) - public fun flatMap(f: (A) -> EagerEffect<@UnsafeVariance R, B>): EagerEffect = eagerEffect { - f(bind()).bind() - } - - public fun attempt(): EagerEffect> = eagerEffect { - kotlin.runCatching { bind() } - } - - public fun handleError(f: (R) -> @UnsafeVariance A): EagerEffect = eagerEffect { - fold(f, ::identity) - } - - public fun handleErrorWith(f: (R) -> EagerEffect): EagerEffect = - eagerEffect { - toEither().fold({ r -> f(r).bind() }, ::identity) - } - - public fun redeem(f: (R) -> B, g: (A) -> B): EagerEffect = eagerEffect { - fold(f, g) - } - - public fun redeemWith( - f: (R) -> EagerEffect, - g: (A) -> EagerEffect - ): EagerEffect = eagerEffect { fold(f, g).bind() } -} - -@PublishedApi -internal class Eager(val token: Token, val shifted: Any?, val recover: (Any?) -> Any?) : - ShiftCancellationException() { - override fun toString(): String = "ShiftCancellationException($message)" -} - -/** - * DSL for constructing `EagerEffect` values - * - * ```kotlin - * import arrow.core.Either - * import arrow.core.None - * import arrow.core.Option - * import arrow.core.Validated - * import arrow.core.continuations.eagerEffect - * import io.kotest.assertions.fail - * import io.kotest.matchers.shouldBe - * - * fun main() { - * eagerEffect { - * val x = Either.Right(1).bind() - * val y = Validated.Valid(2).bind() - * val z = Option(3).bind { "Option was empty" } - * x + y + z - * }.fold({ fail("Shift can never be the result") }, { it shouldBe 6 }) - * - * eagerEffect { - * val x = Either.Right(1).bind() - * val y = Validated.Valid(2).bind() - * val z: Int = None.bind { "Option was empty" } - * x + y + z - * }.fold({ it shouldBe "Option was empty" }, { fail("Int can never be the result") }) - * } - * ``` - * - */ -public fun eagerEffect(f: suspend EagerEffectScope.() -> A): EagerEffect = DefaultEagerEffect(f) - -private class DefaultEagerEffect(private val f: suspend EagerEffectScope.() -> A) : EagerEffect { - override fun fold(recover: (R) -> B, transform: (A) -> B): B { - val token = Token() - val eagerEffectScope = - object : EagerEffectScope { - // Shift away from this Continuation by intercepting it, and completing it with - // ShiftCancellationException - // This is needed because this function will never yield a result, - // so it needs to be cancelled to properly support coroutine cancellation - override suspend fun shift(r: R): B = - // Some interesting consequences of how Continuation Cancellation works in Kotlin. - // We have to throw CancellationException to signal the Continuation was cancelled, and we - // shifted away. - // This however also means that the user can try/catch shift and recover from the - // CancellationException and thus effectively recovering from the cancellation/shift. - // This means try/catch is also capable of recovering from monadic errors. - // See: EagerEffectSpec - try/catch tests - throw Eager(token, r, recover as (Any?) -> Any?) - } - - return try { - suspend { transform(f(eagerEffectScope)) } - .startCoroutineUninterceptedOrReturn(Continuation(EmptyCoroutineContext) { result -> - result.getOrElse { throwable -> - if (throwable is Eager && token == throwable.token) { - throwable.recover(throwable.shifted) as B - } else throw throwable - } - }) as B - } catch (e: Eager) { - if (token == e.token) e.recover(e.shifted) as B - else throw e - } - } -} - -private const val deprecateMonadAppFunctorOperators: String = "Operators related to Functor, Applicative or Monad hierarchies are being deprecated in favor of bind" - -public fun EagerEffect.merge(): A = fold(::identity, ::identity) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EagerEffectScope.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EagerEffectScope.kt deleted file mode 100644 index 2eedc8bf645..00000000000 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EagerEffectScope.kt +++ /dev/null @@ -1,269 +0,0 @@ -package arrow.core.continuations - -import arrow.core.Either -import arrow.core.EmptyValue -import arrow.core.None -import arrow.core.Option -import arrow.core.Some -import arrow.core.Validated -import arrow.core.identity -import kotlin.contracts.ExperimentalContracts -import kotlin.contracts.contract -import kotlin.coroutines.RestrictsSuspension -import kotlin.experimental.ExperimentalTypeInference -import kotlin.jvm.JvmInline - -/** Context of the [EagerEffect] DSL. */ -@RestrictsSuspension -public interface EagerEffectScope { - - /** Short-circuit the [EagerEffect] computation with value [R]. - * - * ```kotlin - * import arrow.core.continuations.eagerEffect - * import io.kotest.assertions.fail - * import io.kotest.matchers.shouldBe - * - * fun main() { - * eagerEffect { - * shift("SHIFT ME") - * }.fold({ it shouldBe "SHIFT ME" }, { fail("Computation never finishes") }) - * } - * ``` - * - */ - public suspend fun shift(r: R): B - - /** - * Runs the [EagerEffect] to finish, returning [B] or [shift] in case of [R]. - * - * ```kotlin - * import arrow.core.Either - * import arrow.core.continuations.EagerEffect - * import arrow.core.continuations.eagerEffect - * import arrow.core.identity - * import io.kotest.matchers.shouldBe - * - * fun Either.toEagerEffect(): EagerEffect = eagerEffect { - * fold({ e -> shift(e) }, ::identity) - * } - * - * fun main() { - * val either = Either.Left("failed") - * eagerEffect { - * val x: Int = either.toEagerEffect().bind() - * x - * }.toEither() shouldBe either - * } - * ``` - * - */ - public suspend fun EagerEffect.bind(): B { - var left: Any? = EmptyValue - var right: Any? = EmptyValue - fold({ r -> left = r }, { a -> right = a }) - return if (left === EmptyValue) EmptyValue.unbox(right) else shift(EmptyValue.unbox(left)) - } - - /** - * Folds [Either] into [EagerEffect], by returning [B] or a shift with [R]. - * - * ```kotlin - * import arrow.core.Either - * import arrow.core.continuations.eagerEffect - * import io.kotest.matchers.shouldBe - * - * fun main() { - * val either = Either.Right(9) - * eagerEffect { - * val x: Int = either.bind() - * x - * }.toEither() shouldBe either - * } - * ``` - * - */ - public suspend fun Either.bind(): B = - when (this) { - is Either.Left -> shift(value) - is Either.Right -> value - } - - /** - * Folds [Validated] into [EagerEffect], by returning [B] or a shift with [R]. - * - * ```kotlin - * import arrow.core.Validated - * import arrow.core.continuations.eagerEffect - * import io.kotest.matchers.shouldBe - * - * fun main() { - * val validated = Validated.Valid(40) - * eagerEffect { - * val x: Int = validated.bind() - * x - * }.toValidated() shouldBe validated - * } - * ``` - * - */ - public suspend fun Validated.bind(): B = - when (this) { - is Validated.Valid -> value - is Validated.Invalid -> shift(value) - } - - /** - * Folds [Result] into [EagerEffect], by returning [B] or a transforming [Throwable] into [R] and - * shifting the result. - * - * ```kotlin - * import arrow.core.continuations.eagerEffect - * import arrow.core.identity - * import io.kotest.matchers.shouldBe - * - * private val default = "failed" - * fun main() { - * val result = Result.success(1) - * eagerEffect { - * val x: Int = result.bind { _: Throwable -> default } - * x - * }.fold({ default }, ::identity) shouldBe result.getOrElse { default } - * } - * ``` - * - */ - public suspend fun Result.bind(transform: (Throwable) -> R): B = - fold(::identity) { throwable -> shift(transform(throwable)) } - - /** - * Folds [Option] into [EagerEffect], by returning [B] or a transforming [None] into [R] and shifting the - * result. - * - * ```kotlin - * import arrow.core.None - * import arrow.core.Option - * import arrow.core.continuations.eagerEffect - * import arrow.core.getOrElse - * import arrow.core.identity - * import io.kotest.matchers.shouldBe - * - * private val default = "failed" - * fun main() { - * val option: Option = None - * eagerEffect { - * val x: Int = option.bind { default } - * x - * }.fold({ default }, ::identity) shouldBe option.getOrElse { default } - * } - * ``` - * - */ - public suspend fun Option.bind(shift: () -> R): B = - when (this) { - None -> shift(shift()) - is Some -> value - } - - /** - * ensure that condition is `true`, if it's `false` it will `shift` with the provided value [R]. - * Monadic version of [kotlin.require]. - * - * ```kotlin - * import arrow.core.Either - * import arrow.core.continuations.eagerEffect - * import io.kotest.matchers.shouldBe - * - * fun main() { - * val condition = true - * val failure = "failed" - * val int = 4 - * eagerEffect { - * ensure(condition) { failure } - * int - * }.toEither() shouldBe if(condition) Either.Right(int) else Either.Left(failure) - * } - * ``` - * - */ - public suspend fun ensure(condition: Boolean, shift: () -> R): Unit = - if (condition) Unit else shift(shift()) - - /** - * Encloses an action for which you want to catch any `shift`. - * [attempt] is used in combination with [catch]. - * - * ``` - * attempt { ... } catch { ... } - * ``` - * - * The [f] may `shift` into a different `EagerEffectScope`, giving - * the chance for a later [catch] to change the shifted value. - * This is useful to simulate re-throwing of exceptions. - */ - @OptIn(ExperimentalTypeInference::class) - public suspend fun attempt( - @BuilderInference - f: suspend EagerEffectScope.() -> A, - ): suspend EagerEffectScope.() -> A = f - - - /** - * When the [EagerEffect] has shifted with [R] it will [recover] - * the shifted value to [A], and when it ran the computation to - * completion it will return the value [A]. - * [catch] is used in combination with [attempt]. - * - * ```kotlin - * import arrow.core.Either - * import arrow.core.None - * import arrow.core.Option - * import arrow.core.Validated - * import arrow.core.continuations.eagerEffect - * import io.kotest.assertions.fail - * import io.kotest.matchers.shouldBe - * - * fun main() { - * eagerEffect { - * val x = Either.Right(1).bind() - * val y = Validated.Valid(2).bind() - * val z = - * attempt { None.bind { "Option was empty" } } catch { 0 } - * x + y + z - * }.fold({ fail("Shift can never be the result") }, { it shouldBe 3 }) - * } - * ``` - * - */ - public infix fun (suspend EagerEffectScope.() -> A).catch( - recover: EagerEffectScope.(E) -> A, - ): A = eagerEffect(this).fold({ recover(it) }, ::identity) -} - -/** - * Ensure that [value] is not `null`. if it's non-null it will be smart-casted and returned if it's - * `false` it will `shift` with the provided value [R]. Monadic version of [kotlin.requireNotNull]. - * - * ```kotlin - * import arrow.core.continuations.eagerEffect - * import arrow.core.continuations.ensureNotNull - * import arrow.core.left - * import arrow.core.right - * import io.kotest.matchers.shouldBe - * - * fun main() { - * val failure = "failed" - * val int: Int? = null - * eagerEffect { - * ensureNotNull(int) { failure } - * }.toEither() shouldBe (int?.right() ?: failure.left()) - * } - * ``` - * - */ -@OptIn(ExperimentalContracts::class) -public suspend fun EagerEffectScope.ensureNotNull(value: B?, shift: () -> R): B { - contract { returns() implies (value != null) } - return value ?: shift(shift()) -} - diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt deleted file mode 100644 index 94f76c710c5..00000000000 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt +++ /dev/null @@ -1,831 +0,0 @@ -package arrow.core.continuations - -import arrow.core.Either -import arrow.core.Ior -import arrow.core.NonFatal -import arrow.core.Option -import arrow.core.Some -import arrow.core.Validated -import arrow.core.identity -import arrow.core.nonFatalOrThrow -import kotlin.coroutines.Continuation -import kotlin.coroutines.CoroutineContext -import kotlin.coroutines.cancellation.CancellationException -import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED -import kotlin.coroutines.intrinsics.startCoroutineUninterceptedOrReturn -import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn -import kotlin.coroutines.resume -import kotlin.coroutines.resumeWithException - -/** - * [Effect] represents a function of `suspend () -> A`, that short-circuit with a value of [R] (and [Throwable]), - * or completes with a value of [A]. - * - * So [Effect] is defined by `suspend fun fold(f: suspend (R) -> B, g: suspend (A) -> B): B`, - * to map both values of [R] and [A] to a value of `B`. - * - * - - * [Writing a program with Effect](#writing-a-program-with-effect) - * [Handling errors](#handling-errors) - * [Structured Concurrency](#structured-concurrency) - * [Arrow Fx Coroutines](#arrow-fx-coroutines) - * [parZip](#parzip) - * [parTraverse](#partraverse) - * [raceN](#racen) - * [bracketCase / Resource](#bracketcase--resource) - * [KotlinX](#kotlinx) - * [withContext](#withcontext) - * [async](#async) - * [launch](#launch) - * [Strange edge cases](#strange-edge-cases) - - * - * - * - * ## Writing a program with Effect - * - * Let's write a small program to read a file from disk, and instead of having the program work exception based we want to - * turn it into a polymorphic type-safe program. - * - * We'll start by defining a small function that accepts a [String], and does some simply validation to check that the path - * is not empty. If the path is empty, we want to program to result in `EmptyPath`. So we're immediately going to see how - * we can raise an error of any arbitrary type [R] by using the function `shift`. The name `shift` comes shifting (or - * changing, especially unexpectedly), away from the computation and finishing the `Continuation` with [R]. - * - * - * ```kotlin - * object EmptyPath - * - * fun readFile(path: String): Effect = effect { - * if (path.isEmpty()) shift(EmptyPath) else Unit - * } - * ``` - * - * Here we see how we can define an `Effect` which has `EmptyPath` for the shift type [R], and `Unit` for the success - * type [A]. - * - * Patterns like validating a [Boolean] is very common, and the [Effect] DSL offers utility functions like [kotlin.require] - * and [kotlin.requireNotNull]. They're named [EffectScope.ensure] and [ensureNotNull] to avoid conflicts with the `kotlin` namespace. - * So let's rewrite the function from above to use the DSL instead. - * - * ```kotlin - * fun readFile2(path: String?): Effect = effect { - * ensureNotNull(path) { EmptyPath } - * ensure(path.isNotEmpty()) { EmptyPath } - * } - * ``` - * - * - * Now that we have the path, we can read from the `File` and return it as a domain model `Content`. - * We also want to take a look at what exceptions reading from a file might occur `FileNotFoundException` & `SecurityError`, - * so lets make some domain errors for those too. Grouping them as a sealed interface is useful since that way we can resolve *all* errors in a type safe manner. - * - * - * ```kotlin - * @JvmInline - * value class Content(val body: List) - * - * sealed interface FileError - * @JvmInline value class SecurityError(val msg: String?) : FileError - * @JvmInline value class FileNotFound(val path: String) : FileError - * object EmptyPath : FileError { - * override fun toString() = "EmptyPath" - * } - * ``` - * - * We can finish our function, but we need to refactor the return type from `Unit` to `Content` and the error type from `EmptyPath` to `FileError`. - * - * ```kotlin - * fun readFile(path: String?): Effect = effect { - * ensureNotNull(path) { EmptyPath } - * ensure(path.isNotEmpty()) { EmptyPath } - * try { - * val lines = File(path).readLines() - * Content(lines) - * } catch (e: FileNotFoundException) { - * shift(FileNotFound(path)) - * } catch (e: SecurityException) { - * shift(SecurityError(e.message)) - * } - * } - * ``` - * - * The `readFile` function defines a `suspend fun` that will return: - * - * - the `Content` of a given `path` - * - a `FileError` - * - An unexpected fatal error (`OutOfMemoryException`) - * - * Since these are the properties of our `Effect` function, we can turn it into a value. - * - * ```kotlin - * suspend fun main() { - * readFile("").toEither() shouldBe Either.Left(EmptyPath) - * readFile("knit.properties").toValidated() shouldBe Validated.Invalid(FileNotFound("knit.properties")) - * readFile("gradle.properties").toIor() shouldBe Ior.Left(FileNotFound("gradle.properties")) - * readFile("README.MD").toOption { None } shouldBe None - * - * readFile("build.gradle.kts").fold({ _: FileError -> null }, { it }) - * .shouldBeInstanceOf() - * .body.shouldNotBeEmpty() - * } - * ``` - * - * - * The functions above are available out of the box, but it's easy to define your own extension functions in terms - * of `fold`. Implementing the `toEither()` operator is as simple as: - * - * - * ```kotlin - * suspend fun Effect.toEither(): Either = - * fold({ Either.Left(it) }) { Either.Right(it) } - * - * suspend fun Effect.toOption(): Option = - * fold(::identity) { Some(it) } - * ``` - * - * - * Adding your own syntax to `EffectScope` is not advised, yet, but will be easy once "Multiple Receivers" become available. - * - * ``` - * context(EffectScope) - * suspend fun Either.bind(): A = - * when (this) { - * is Either.Left -> shift(value) - * is Either.Right -> value - * } - * - * context(EffectScope) - * fun Option.bind(): A = - * fold({ shift(it) }, ::identity) - * ``` - * - * ## Handling errors - * - * Handling errors of type [R] is the same as handling errors for any other data type in Arrow. - * `Effect` offers `handleError`, `handleErrorWith`, `redeem`, `redeemWith` and `attempt`. - * - * As you can see in the examples below it is possible to resolve errors of [R] or `Throwable` in `Effect` in a generic manner. - * There is no need to run `Effect` into `Either` before you can access [R], - * you can simply call the same functions on `Effect` as you would on `Either` directly. - * - * - * ```kotlin - * val failed: Effect = - * effect { shift("failed") } - * - * val resolved: Effect = - * failed.handleError { it.length } - * - * val newError: Effect, Int> = - * failed.handleErrorWith { str -> - * effect { shift(str.reversed().toList()) } - * } - * - * val redeemed: Effect = - * failed.redeem({ str -> str.length }, ::identity) - * - * val captured: Effect> = - * effect { 1 }.attempt() - * - * suspend fun main() { - * failed.toEither() shouldBe Either.Left("failed") - * resolved.toEither() shouldBe Either.Right(6) - * newError.toEither() shouldBe Either.Left(listOf('d', 'e', 'l', 'i', 'a', 'f')) - * redeemed.toEither() shouldBe Either.Right(6) - * captured.toEither() shouldBe Either.Right(Result.success(1)) - * } - * ``` - * - * - * Note: - * Handling errors can also be done with `try/catch` but this is **not recommended**, it uses `CancellationException` which is used to cancel `Coroutine`s and is advised not to capture in Kotlin. - * The `CancellationException` from `Effect` is `ShiftCancellationException`, this a public type, thus can be distinguished from any other `CancellationException` if necessary. - * - * ## Structured Concurrency - * - * `Effect` relies on `kotlin.cancellation.CancellationException` to `shift` error values of type [R] inside the `Continuation` since it effectively cancels/short-circuits it. - * For this reason `shift` adheres to the same rules as [`Structured Concurrency`](https://kotlinlang.org/docs/coroutines-basics.html#structured-concurrency) - * - * Let's overview below how `shift` behaves with the different concurrency builders from Arrow Fx & KotlinX Coroutines. - * In the examples below we're going to be using a utility to show how _sibling tasks_ get cancelled. - * The utility function show below called `awaitExitCase` will `never` finish suspending, and completes a `Deferred` with the `ExitCase`. - * `ExitCase` is a sealed class that can be a value of `Failure(Throwable)`, `Cancelled(CancellationException)`, or `Completed`. - * Since `awaitExitCase` suspends forever, it can only result in `Cancelled(CancellationException)`. - * - * - * ```kotlin - * suspend fun awaitExitCase(exit: CompletableDeferred): A = - * guaranteeCase(::awaitCancellation) { exitCase -> exit.complete(exitCase) } - * - * ``` - * - * ### Arrow Fx Coroutines - * All operators in Arrow Fx Coroutines run in place, so they have no way of leaking `shift`. - * It's there always safe to compose `effect` with any Arrow Fx combinator. Let's see some small examples below. - * - * #### parZip - * - * ```kotlin - * suspend fun main() { - * val error = "Error" - * val exit = CompletableDeferred() - * effect { - * parZip({ awaitExitCase(exit) }, { shift(error) }) { a, b -> a + b } - * }.fold({ it shouldBe error }, { fail("Int can never be the result") }) - * exit.await().shouldBeTypeOf() - * } - * ``` - * - * - * #### parTraverse - * - * ```kotlin - * suspend fun main() { - * val error = "Error" - * val exits = (0..3).map { CompletableDeferred() } - * effect> { - * (0..4).parTraverse { index -> - * if (index == 4) shift(error) - * else awaitExitCase(exits[index]) - * } - * }.fold({ msg -> msg shouldBe error }, { fail("Int can never be the result") }) - * // It's possible not all parallel task got launched, and in those cases awaitCancellation never ran - * exits.forEach { exit -> exit.getOrNull()?.shouldBeTypeOf() } - * } - * ``` - * - * - * `parTraverse` will launch 5 tasks, for every element in `1..5`. - * The last task to get scheduled will `shift` with "error", and it will cancel the other launched tasks before returning. - * - * #### raceN - * - * ```kotlin - * suspend fun main() { - * val error = "Error" - * val exit = CompletableDeferred() - * effect { - * raceN({ awaitExitCase(exit) }) { shift(error) } - * .merge() // Flatten Either result from race into Int - * }.fold({ msg -> msg shouldBe error }, { fail("Int can never be the result") }) - * // It's possible not all parallel task got launched, and in those cases awaitCancellation never ran - * exit.getOrNull()?.shouldBeTypeOf() - * } - * ``` - * - * - * `raceN` races `n` suspend functions in parallel, and cancels all participating functions when a winner is found. - * We can consider the function that `shift`s the winner of the race, except with a shifted value instead of a successful one. - * So when a function in the race `shift`s, and thus short-circuiting the race, it will cancel all the participating functions. - * - * #### bracketCase / Resource - * - * ```kotlin - * suspend fun main() { - * val error = "Error" - * val exit = CompletableDeferred() - * effect { - * bracketCase( - * acquire = { File("build.gradle.kts").bufferedReader() }, - * use = { reader: BufferedReader -> shift(error) }, - * release = { reader, exitCase -> - * reader.close() - * exit.complete(exitCase) - * } - * ) - * }.fold({ it shouldBe error }, { fail("Int can never be the result") }) - * exit.await().shouldBeTypeOf() - * } - * ``` - * - * - * - * ```kotlin - * suspend fun main() { - * val error = "Error" - * val exit = CompletableDeferred() - * - * fun bufferedReader(path: String): Resource = - * Resource.fromAutoCloseable { File(path).bufferedReader() } - * .releaseCase { _, exitCase -> exit.complete(exitCase) } - * - * effect { - * val lineCount = bufferedReader("build.gradle.kts") - * .use { reader -> shift(error) } - * lineCount - * }.fold({ it shouldBe error }, { fail("Int can never be the result") }) - * exit.await().shouldBeTypeOf() - * } - * ``` - * - * - * ### KotlinX - * #### withContext - * It's always safe to call `shift` from `withContext` since it runs in place, so it has no way of leaking `shift`. - * When `shift` is called from within `withContext` it will cancel all `Job`s running inside the `CoroutineScope` of `withContext`. - * - * - * ```kotlin - * suspend fun main() { - * val exit = CompletableDeferred() - * effect { - * withContext(Dispatchers.IO) { - * val job = launch { awaitExitCase(exit) } - * val content = readFile("failure").bind() - * job.join() - * content.body.size - * } - * }.fold({ e -> e shouldBe FileNotFound("failure") }, { fail("Int can never be the result") }) - * exit.await().shouldBeInstanceOf() - * } - * ``` - * - * - * #### async - * - * When calling `shift` from `async` you should **always** call `await`, otherwise `shift` can leak out of its scope. - * - * - * ```kotlin - * suspend fun main() { - * val errorA = "ErrorA" - * val errorB = "ErrorB" - * coroutineScope { - * effect { - * val fa = async { shift(errorA) } - * val fb = async { shift(errorB) } - * fa.await() + fb.await() - * }.fold({ error -> error shouldBeIn listOf(errorA, errorB) }, { fail("Int can never be the result") }) - * } - * } - * ``` - * - * - * #### launch - * - * - * ```kotlin - * suspend fun main() { - * val errorA = "ErrorA" - * val errorB = "ErrorB" - * val int = 45 - * effect { - * coroutineScope { - * launch { shift(errorA) } - * launch { shift(errorB) } - * int - * } - * }.fold({ fail("Shift can never finish") }, { it shouldBe int }) - * } - * ``` - * - * - * #### Strange edge cases - * - * **NOTE** - * Capturing `shift` into a lambda, and leaking it outside of `Effect` to be invoked outside will yield unexpected results. - * Below we capture `shift` from inside the DSL, and then invoke it outside its context `EffectScope`. - * - * - * - * ```kotlin - * effect Unit> { - * suspend { shift("error") } - * }.fold({ }, { leakedShift -> leakedShift.invoke() }) - * ``` - * - * The same violation is possible in all DSLs in Kotlin, including Structured Concurrency. - * - * ```kotlin - * val leakedAsync = coroutineScope Deferred> { - * suspend { - * async { - * println("I am never going to run, until I get called invoked from outside") - * } - * } - * } - * - * leakedAsync.invoke().await() - * ``` - * - */ -public interface Effect { - /** - * Runs the suspending computation by creating a [Continuation], and running the `fold` function - * over the computation. - * - * When the [Effect] has shifted with [R] it will [recover] the shifted value to [B], and when it - * ran the computation to completion it will [transform] the value [A] to [B]. - * - * ```kotlin - * import arrow.core.continuations.effect - * import io.kotest.matchers.shouldBe - * - * suspend fun main() { - * val shift = effect { - * shift("Hello, World!") - * }.fold({ str: String -> str }, { int -> int.toString() }) - * shift shouldBe "Hello, World!" - * - * val res = effect { - * 1000 - * }.fold({ str: String -> str.length }, { int -> int }) - * res shouldBe 1000 - * } - * ``` - * - */ - public suspend fun fold( - recover: suspend (shifted: R) -> B, - transform: suspend (value: A) -> B - ): B - - /** - * Like [fold] but also allows folding over any unexpected [Throwable] that might have occurred. - * @see fold - */ - public suspend fun fold( - error: suspend (error: Throwable) -> B, - recover: suspend (shifted: R) -> B, - transform: suspend (value: A) -> B - ): B = - try { - fold(recover, transform) - } catch (e: Throwable) { - error(e.nonFatalOrThrow()) - } - - /** - * [fold] the [Effect] into an [Either]. Where the shifted value [R] is mapped to [Either.Left], and - * result value [A] is mapped to [Either.Right]. - */ - public suspend fun toEither(): Either = fold({ Either.Left(it) }) { Either.Right(it) } - - /** - * [fold] the [Effect] into an [Ior]. Where the shifted value [R] is mapped to [Ior.Left], and - * result value [A] is mapped to [Ior.Right]. - */ - public suspend fun toIor(): Ior = fold({ Ior.Left(it) }) { Ior.Right(it) } - - /** - * [fold] the [Effect] into an [Validated]. Where the shifted value [R] is mapped to - * [Validated.Invalid], and result value [A] is mapped to [Validated.Valid]. - */ - public suspend fun toValidated(): Validated = - fold({ Validated.Invalid(it) }) { Validated.Valid(it) } - - /** - * [fold] the [Effect] into an [Option]. Where the shifted value [R] is mapped to [Option] by the - * provided function [orElse], and result value [A] is mapped to [Some]. - */ - public suspend fun toOption(orElse: suspend (R) -> Option<@UnsafeVariance A>): Option = fold(orElse, ::Some) - - /** - * [fold] the [Effect] into an [A?]. Where the shifted value [R] is mapped to - * [null], and result value [A]. - */ - public suspend fun orNull(): A? = fold({ null }, ::identity) - - /** Runs the [Effect] and captures any [NonFatal] exception into [Result]. */ - public fun attempt(): Effect> = effect { - try { - Result.success(bind()) - } catch (e: Throwable) { - Result.failure(e.nonFatalOrThrow()) - } - } - - public fun handleError(recover: suspend (R) -> @UnsafeVariance A): Effect = effect { - fold(recover, ::identity) - } - - public fun handleErrorWith(recover: suspend (R) -> Effect): Effect = effect { - fold({ recover(it).bind() }, ::identity) - } - - public fun redeem(recover: suspend (R) -> B, transform: suspend (A) -> B): Effect = - effect { - fold(recover, transform) - } - - public fun redeemWith( - recover: suspend (R) -> Effect, - transform: suspend (A) -> Effect - ): Effect = effect { fold(recover, transform).bind() } -} - -/** - * **AVOID USING THIS TYPE, it's meant for low-level cancellation code** When in need in low-level - * code, you can use this type to differentiate between a foreign [CancellationException] and the - * one from [Effect]. - */ -public sealed class ShiftCancellationException : CancellationException("Shifted Continuation") - -/** - * Holds `R` and `suspend (R) -> B`, the exception that wins the race, will get to execute - * `recover`. - */ -@PublishedApi -internal class Suspend(val token: Token, val shifted: Any?, val recover: suspend (Any?) -> Any?) : - ShiftCancellationException() { - override fun toString(): String = "ShiftCancellationException($message)" -} - -/** Class that represents a unique token by hash comparison **/ -@PublishedApi -internal open class Token { - override fun toString(): String = "Token(${hashCode().toString(16)})" -} - -/** - * Continuation that runs the `recover` function, after attempting to calculate [B]. In case we - * encounter a `shift` after suspension, we will receive [Result.failure] with - * [ShiftCancellationException]. In that case we still need to run `suspend (R) -> B`, which is what - * we do inside the body of this `Continuation`, and we complete the [parent] [Continuation] with - * the result. - */ -@PublishedApi -@Deprecated( - "This will become private in Arrow 2.0, and is not going to be visible from binary anymore", - level = DeprecationLevel.WARNING -) -internal class FoldContinuation( - override val context: CoroutineContext, - private val error: suspend (Throwable) -> B, - private val parent: Continuation, -) : Continuation, Token(), EffectScope { - - constructor(ignored: Token, context: CoroutineContext, parent: Continuation) : this(context, { throw it }, parent) - constructor( - ignored: Token, - context: CoroutineContext, - error: suspend (Throwable) -> B, - parent: Continuation, - ) : this(context, error, parent) - - lateinit var recover: suspend (R) -> Any? - - // Shift away from this Continuation by intercepting it, and completing it with - // ShiftCancellationException - // This is needed because this function will never yield a result, - // so it needs to be cancelled to properly support coroutine cancellation - override suspend fun shift(r: R): B = - // Some interesting consequences of how Continuation Cancellation works in Kotlin. - // We have to throw CancellationException to signal the Continuation was cancelled, and we - // shifted away. - // This however also means that the user can try/catch shift and recover from the - // CancellationException and thus effectively recovering from the cancellation/shift. - // This means try/catch is also capable of recovering from monadic errors. - // See: EffectSpec - try/catch tests - throw Suspend(this, r, recover as suspend (Any?) -> Any?) - - // In contrast to `createCoroutineUnintercepted this doesn't create a new ContinuationImpl - private fun (suspend () -> B).startCoroutineUnintercepted() { - try { - when (val res = startCoroutineUninterceptedOrReturn(parent)) { - COROUTINE_SUSPENDED -> Unit - else -> parent.resume(res as B) - } - // We need to wire all immediately throw exceptions to the parent Continuation - } catch (e: Throwable) { - parent.resumeWithException(e) - } - } - - override fun resumeWith(result: Result) { - result.fold(parent::resume) { throwable -> - when { - throwable is Suspend && this === throwable.token -> - suspend { throwable.recover(throwable.shifted) as B }.startCoroutineUnintercepted() - - throwable is Suspend -> parent.resumeWith(result) - else -> suspend { error(throwable.nonFatalOrThrow()) }.startCoroutineUnintercepted() - } - } - } -} - -/** - * DSL for constructing Effect values - * - * ```kotlin - * import arrow.core.Either - * import arrow.core.None - * import arrow.core.Option - * import arrow.core.Validated - * import arrow.core.continuations.effect - * import io.kotest.assertions.fail - * import io.kotest.matchers.shouldBe - * - * suspend fun main() { - * effect { - * val x = Either.Right(1).bind() - * val y = Validated.Valid(2).bind() - * val z = Option(3).bind { "Option was empty" } - * x + y + z - * }.fold({ fail("Shift can never be the result") }, { it shouldBe 6 }) - * - * effect { - * val x = Either.Right(1).bind() - * val y = Validated.Valid(2).bind() - * val z: Int = None.bind { "Option was empty" } - * x + y + z - * }.fold({ it shouldBe "Option was empty" }, { fail("Int can never be the result") }) - * } - * ``` - * - */ -public fun effect(f: suspend EffectScope.() -> A): Effect = DefaultEffect(f) - -private class DefaultEffect(val f: suspend EffectScope.() -> A) : Effect { - - override suspend fun fold( - recover: suspend (shifted: R) -> B, - transform: suspend (value: A) -> B, - ): B = fold({ throw it }, recover, transform) - - // We create a `Token` for fold Continuation, so we can properly differentiate between nested folds - override suspend fun fold( - error: suspend (error: Throwable) -> B, - recover: suspend (shifted: R) -> B, - transform: suspend (value: A) -> B, - ): B = - suspendCoroutineUninterceptedOrReturn { cont -> - val shift = FoldContinuation(cont.context, error, cont) - shift.recover = recover - try { - val fold: suspend EffectScope.() -> B = { transform(f(this)) } - fold.startCoroutineUninterceptedOrReturn(shift, shift) - } catch (e: Suspend) { - if (shift === e.token) { - val f: suspend () -> B = { e.recover(e.shifted) as B } - f.startCoroutineUninterceptedOrReturn(cont) - } else throw e - } catch (e: Throwable) { - val f: suspend () -> B = { error(e.nonFatalOrThrow()) } - f.startCoroutineUninterceptedOrReturn(cont) - } - } -} - -public suspend fun Effect.merge(): A = fold(::identity, ::identity) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EffectScope.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EffectScope.kt deleted file mode 100644 index 8a6a5b46466..00000000000 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/EffectScope.kt +++ /dev/null @@ -1,294 +0,0 @@ -package arrow.core.continuations - -import arrow.core.Either -import arrow.core.EmptyValue -import arrow.core.None -import arrow.core.Option -import arrow.core.Some -import arrow.core.Validated -import arrow.core.identity -import kotlin.contracts.ExperimentalContracts -import kotlin.contracts.contract -import kotlin.experimental.ExperimentalTypeInference -import kotlin.jvm.JvmInline - -/** Context of the [Effect] DSL. */ -public interface EffectScope { - /** - * Short-circuit the [Effect] computation with value [R]. - * - * ```kotlin - * import arrow.core.continuations.effect - * import io.kotest.assertions.fail - * import io.kotest.matchers.shouldBe - * - * suspend fun main() { - * effect { - * shift("SHIFT ME") - * }.fold({ it shouldBe "SHIFT ME" }, { fail("Computation never finishes") }) - * } - * ``` - * - */ - public suspend fun shift(r: R): B - - /** - * Runs the [Effect] to finish, returning [B] or [shift] in case of [R]. - * - * ```kotlin - * import arrow.core.Either - * import arrow.core.continuations.Effect - * import arrow.core.continuations.effect - * import arrow.core.identity - * import io.kotest.matchers.shouldBe - * - * suspend fun Either.toEffect(): Effect = effect { - * fold({ e -> shift(e) }, ::identity) - * } - * - * suspend fun main() { - * val either = Either.Left("failed") - * effect { - * val x: Int = either.toEffect().bind() - * x - * }.toEither() shouldBe either - * } - * ``` - * - */ - public suspend fun Effect.bind(): B = fold(this@EffectScope::shift, ::identity) - - /** - * Runs the [EagerEffect] to finish, returning [B] or [shift] in case of [R], - * bridging eager computations into suspending. - * - * ```kotlin - * import arrow.core.Either - * import arrow.core.continuations.EagerEffect - * import arrow.core.continuations.eagerEffect - * import arrow.core.continuations.effect - * import arrow.core.identity - * import io.kotest.matchers.shouldBe - * - * suspend fun Either.toEagerEffect(): EagerEffect = eagerEffect { - * fold({ e -> shift(e) }, ::identity) - * } - * - * suspend fun main() { - * val either = Either.Left("failed") - * effect { - * val x: Int = either.toEagerEffect().bind() - * x - * }.toEither() shouldBe either - * } - * ``` - * - */ - public suspend fun EagerEffect.bind(): B { - var left: Any? = EmptyValue - var right: Any? = EmptyValue - fold({ r -> left = r }, { a -> right = a }) - return if (left === EmptyValue) EmptyValue.unbox(right) else shift(EmptyValue.unbox(left)) - } - - /** - * Folds [Either] into [Effect], by returning [B] or a shift with [R]. - * - * ```kotlin - * import arrow.core.Either - * import arrow.core.continuations.effect - * import io.kotest.matchers.shouldBe - * - * suspend fun main() { - * val either = Either.Right(9) - * effect { - * val x: Int = either.bind() - * x - * }.toEither() shouldBe either - * } - * ``` - * - */ - public suspend fun Either.bind(): B = - when (this) { - is Either.Left -> shift(value) - is Either.Right -> value - } - - /** - * Folds [Validated] into [Effect], by returning [B] or a shift with [R]. - * - * ```kotlin - * import arrow.core.Validated - * import arrow.core.continuations.effect - * import io.kotest.matchers.shouldBe - * - * suspend fun main() { - * val validated = Validated.Valid(40) - * effect { - * val x: Int = validated.bind() - * x - * }.toValidated() shouldBe validated - * } - * ``` - * - */ - public suspend fun Validated.bind(): B = - when (this) { - is Validated.Valid -> value - is Validated.Invalid -> shift(value) - } - - /** - * Folds [Result] into [Effect], by returning [B] or a transforming [Throwable] into [R] and - * shifting the result. - * - * ```kotlin - * import arrow.core.continuations.effect - * import arrow.core.identity - * import io.kotest.matchers.shouldBe - * - * private val default = "failed" - * suspend fun main() { - * val result = Result.success(1) - * effect { - * val x: Int = result.bind { _: Throwable -> default } - * x - * }.fold({ default }, ::identity) shouldBe result.getOrElse { default } - * } - * ``` - * - */ - public suspend fun Result.bind(transform: (Throwable) -> R): B = - fold(::identity) { throwable -> shift(transform(throwable)) } - - /** - * Folds [Option] into [Effect], by returning [B] or a transforming [None] into [R] and shifting the - * result. - * - * ```kotlin - * import arrow.core.None - * import arrow.core.Option - * import arrow.core.continuations.effect - * import arrow.core.getOrElse - * import arrow.core.identity - * import io.kotest.matchers.shouldBe - * - * private val default = "failed" - * suspend fun main() { - * val option: Option = None - * effect { - * val x: Int = option.bind { default } - * x - * }.fold({ default }, ::identity) shouldBe option.getOrElse { default } - * } - * ``` - * - */ - public suspend fun Option.bind(shift: () -> R): B = - when (this) { - None -> shift(shift()) - is Some -> value - } - - /** - * ensure that condition is `true`, if it's `false` it will `shift` with the provided value [R]. - * Monadic version of [kotlin.require]. - * - * ```kotlin - * import arrow.core.Either - * import arrow.core.continuations.effect - * import io.kotest.matchers.shouldBe - * - * suspend fun main() { - * val condition = true - * val failure = "failed" - * val int = 4 - * effect { - * ensure(condition) { failure } - * int - * }.toEither() shouldBe if(condition) Either.Right(int) else Either.Left(failure) - * } - * ``` - * - */ - public suspend fun ensure(condition: Boolean, shift: () -> R): Unit = - if (condition) Unit else shift(shift()) - - /** - * Encloses an action for which you want to catch any `shift`. - * [attempt] is used in combination with [catch]. - * - * ``` - * attempt { ... } catch { ... } - * ``` - * - * The [f] may `shift` into a different `EffectScope`, giving - * the chance for a later [catch] to change the shifted value. - * This is useful to simulate re-throwing of exceptions. - */ - @OptIn(ExperimentalTypeInference::class) - public suspend fun attempt( - @BuilderInference - f: suspend EffectScope.() -> A, - ): suspend EffectScope.() -> A = f - - /** - * When the [Effect] has shifted with [R] it will [recover] - * the shifted value to [A], and when it ran the computation to - * completion it will return the value [A]. - * [catch] is used in combination with [attempt]. - * - * ```kotlin - * import arrow.core.Either - * import arrow.core.None - * import arrow.core.Option - * import arrow.core.Validated - * import arrow.core.continuations.effect - * import io.kotest.assertions.fail - * import io.kotest.matchers.shouldBe - * - * suspend fun main() { - * effect { - * val x = Either.Right(1).bind() - * val y = Validated.Valid(2).bind() - * val z = - * attempt { None.bind { "Option was empty" } } catch { 0 } - * x + y + z - * }.fold({ fail("Shift can never be the result") }, { it shouldBe 3 }) - * } - * ``` - * - */ - public suspend infix fun (suspend EffectScope.() -> A).catch( - recover: suspend EffectScope.(E) -> A, - ): A = effect(this).fold({ recover(it) }, ::identity) -} - -/** - * Ensure that [value] is not `null`. if it's non-null it will be smart-casted and returned if it's - * `false` it will `shift` with the provided value [R]. Monadic version of [kotlin.requireNotNull]. - * - * ```kotlin - * import arrow.core.continuations.effect - * import arrow.core.continuations.ensureNotNull - * import arrow.core.left - * import arrow.core.right - * import io.kotest.matchers.shouldBe - * - * suspend fun main() { - * val failure = "failed" - * val int: Int? = null - * effect { - * ensureNotNull(int) { failure } - * }.toEither() shouldBe (int?.right() ?: failure.left()) - * } - * ``` - * - */ -@OptIn(ExperimentalContracts::class) -public suspend fun EffectScope.ensureNotNull(value: B?, shift: () -> R): B { - contract { returns() implies (value != null) } - return value ?: shift(shift()) -} - diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt new file mode 100644 index 00000000000..539c7c198dd --- /dev/null +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt @@ -0,0 +1,48 @@ +@file:JvmMultifileClass +@file:JvmName("Effect") + +package arrow.core.continuations + +import kotlin.experimental.ExperimentalTypeInference +import kotlin.jvm.JvmMultifileClass +import kotlin.jvm.JvmName + +@OptIn(ExperimentalTypeInference::class) +public infix fun Effect.catch(@BuilderInference resolve: suspend Shift.(E) -> A): Effect = + effect { + catch({ bind() }) { e -> resolve(e) } + } + +@OptIn(ExperimentalTypeInference::class) +public infix fun Effect.attempt(@BuilderInference recover: suspend Shift.(Throwable) -> A): Effect = + effect { + attempt({ bind() }) { t -> recover(t) } + } + +@OptIn(ExperimentalTypeInference::class) +@JvmName("attemptOrThrow") +public inline infix fun Effect.attempt( + @BuilderInference crossinline recover: suspend Shift.(T) -> A, +): Effect = effect { + attempt({ bind() }) { t: T -> recover(t) } +} + +@OptIn(ExperimentalTypeInference::class) +public infix fun EagerEffect.catch(@BuilderInference resolve: Shift.(E) -> A): EagerEffect = + eagerEffect { + catch({ bind() }) { e -> resolve(e) } + } + +@OptIn(ExperimentalTypeInference::class) +public infix fun EagerEffect.attempt(@BuilderInference recover: Shift.(Throwable) -> A): EagerEffect = + eagerEffect { + attempt({ bind() }) { t -> recover(t) } + } + +@OptIn(ExperimentalTypeInference::class) +@JvmName("attemptOrThrow") +public inline infix fun EagerEffect.attempt( + @BuilderInference crossinline recover: Shift.(T) -> A, +): EagerEffect = eagerEffect { + attempt({ bind() }) { t: T -> recover(t) } +} diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt new file mode 100644 index 00000000000..eaf936eb55e --- /dev/null +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt @@ -0,0 +1,67 @@ +@file:JvmMultifileClass +@file:JvmName("Effect") + +package arrow.core.continuations + +import arrow.core.nonFatalOrThrow +import kotlin.coroutines.cancellation.CancellationException +import kotlin.experimental.ExperimentalTypeInference +import kotlin.jvm.JvmMultifileClass +import kotlin.jvm.JvmName + +public suspend fun Effect.fold( + recover: suspend (shifted: R) -> B, + transform: suspend (value: A) -> B, +): B = fold({ throw it }, recover, transform) + +public suspend fun Effect.fold( + error: suspend (error: Throwable) -> B, + recover: suspend (shifted: R) -> B, + transform: suspend (value: A) -> B, +): B = fold( + { bind() }, + { error(it) }, + { recover(it) }, + { transform(it) } +) + +public fun EagerEffect.fold(recover: (R) -> B, transform: (A) -> B): B = + fold({ throw it }, recover, transform) + +public inline fun EagerEffect.fold( + error: (error: Throwable) -> B, + recover: (shifted: R) -> B, + transform: (value: A) -> B, +): B = fold({ invoke(this) }, error, recover, transform) + +@OptIn(ExperimentalTypeInference::class) +@JvmName("_fold") +public inline fun fold( + @BuilderInference program: Shift.() -> A, + error: (error: Throwable) -> B, + recover: (shifted: R) -> B, + transform: (value: A) -> B, +): B { + val shift = DefaultShift() + return try { + transform(program(shift)) + } catch (e: ShiftCancellationException) { + if (shift === e.shift) recover(e.shifted as R) else throw e + } catch (e: Throwable) { + error(e.nonFatalOrThrow()) + } +} + +@PublishedApi +internal class ShiftCancellationException( + @PublishedApi + internal val shifted: Any?, + @PublishedApi + internal val shift: Shift, +) : CancellationException("Shifted Continuation") + +@PublishedApi +internal class DefaultShift : Shift { + override fun shift(r: R): B = + throw ShiftCancellationException(r, this as DefaultShift) +} diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt new file mode 100644 index 00000000000..dd3cee5738c --- /dev/null +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt @@ -0,0 +1,37 @@ +@file:JvmMultifileClass +@file:JvmName("Effect") + +package arrow.core.continuations + +import arrow.core.Either +import arrow.core.None +import arrow.core.Option +import arrow.core.Some +import arrow.core.identity +import kotlin.jvm.JvmMultifileClass +import kotlin.jvm.JvmName + +public suspend fun Effect.toEither(): Either = + fold({ Either.Left(it) }, { Either.Right(it) }) + +public fun EagerEffect.toEither(): Either = + fold({ Either.Left(it) }, { Either.Right(it) }) + + +public suspend fun Effect.orNull(): A? = + fold({ null }, { it }) + +public fun EagerEffect.orNull(): A? = + fold({ null }, { it }) + +public suspend fun Effect.toOption(): Option = + fold(::identity) { Some(it) } + +public fun EagerEffect.toOption(): Option = + fold(::identity) { Some(it) } + +public suspend fun Effect.toResult(): Result = + fold({ Result.failure(it) }) { Result.success(it) } + +public fun EagerEffect.toResult(): Result = + fold({ Result.failure(it) }) { Result.success(it) } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt new file mode 100644 index 00000000000..7dfacaa4203 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt @@ -0,0 +1,89 @@ +package arrow.core.continuations + +import arrow.core.Either +import arrow.core.None +import arrow.core.Option +import arrow.core.Some +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.contract +import kotlin.experimental.ExperimentalTypeInference +import kotlin.jvm.JvmName + +@DslMarker +public annotation class EffectDSL + +public interface Shift { + public fun shift(r: R): B + public fun EagerEffect.bind(): B = invoke(this@Shift) + public suspend fun Effect.bind(): B = invoke(this@Shift) + public fun Either.bind(): B = when(this) { + is Either.Left -> shift(value) + is Either.Right -> value + } + public fun Option.bind(transform: Shift.(None) -> B): B = + when(this) { + None -> transform(None) + is Some -> value + } +} + +@OptIn(ExperimentalTypeInference::class) +@EffectDSL +public inline fun Shift.catch( + @BuilderInference action: Shift.() -> A, + @BuilderInference resolve: Shift.(E) -> A, +): A = fold( + { action(this) }, + { throw it }, + { resolve(it) }, + { it } +) + +@OptIn(ExperimentalTypeInference::class) +@EffectDSL +public inline fun Shift.catch( + @BuilderInference action: Shift.() -> A, + @BuilderInference resolve: Shift.(E) -> A, + @BuilderInference recover: Shift.(Throwable) -> A, +): A = fold( + { action(this) }, + { recover(it) }, + { resolve(it) }, + { it } +) + +@OptIn(ExperimentalTypeInference::class) +@EffectDSL +public inline fun Shift.attempt( + @BuilderInference action: Shift.() -> A, + @BuilderInference recover: Shift.(Throwable) -> A, +): A = fold( + { action(this) }, + { recover(it) }, + { shift(it) }, + { it } +) + +@OptIn(ExperimentalTypeInference::class) +@EffectDSL +@JvmName("attemptOrThrow") +public inline fun Shift.attempt( + @BuilderInference action: Shift.() -> A, + @BuilderInference recover: Shift.(T) -> A, +): A = fold( + { action(this) }, + { if (it is T) recover(it) else throw it }, + { shift(it) }, + { it } +) + +@EffectDSL +public inline fun Shift.ensure(condition: Boolean, shift: () -> R): Unit = + if (condition) Unit else shift(shift()) + +@OptIn(ExperimentalContracts::class) +@EffectDSL +public inline fun Shift.ensureNotNull(value: B?, shift: () -> R): B { + contract { returns() implies (value != null) } + return value ?: shift(shift()) +} diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/either.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/either.kt deleted file mode 100644 index a03b738ef37..00000000000 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/either.kt +++ /dev/null @@ -1,12 +0,0 @@ -package arrow.core.continuations - -import arrow.core.Either - -@Suppress("ClassName") -public object either { - public inline fun eager(noinline f: suspend EagerEffectScope.() -> A): Either = - eagerEffect(f).toEither() - - public suspend operator fun invoke(f: suspend EffectScope.() -> A): Either = - effect(f).toEither() -} diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ior.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ior.kt deleted file mode 100644 index 99ff373005a..00000000000 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ior.kt +++ /dev/null @@ -1,83 +0,0 @@ -package arrow.core.continuations - -import arrow.core.EmptyValue -import arrow.core.Ior -import arrow.core.identity -import arrow.typeclasses.Semigroup - -@Suppress("ClassName") -public object ior { - public inline fun eager( - semigroup: Semigroup, - crossinline f: suspend IorEagerEffectScope.() -> A - ): Ior = - eagerEffect> { - val effect = IorEagerEffectScope(semigroup, this) - @Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL") - val res = f(effect) - val leftState = effect.leftState.get() - if (leftState === EmptyValue) Ior.Right(res) else Ior.Both(EmptyValue.unbox(leftState), res) - }.fold({ Ior.Left(it) }, ::identity) - - public suspend inline operator fun invoke( - semigroup: Semigroup, - crossinline f: suspend IorEffectScope.() -> A - ): Ior = - effect> { - val effect = IorEffectScope(semigroup, this) - val res = f(effect) - val leftState = effect.leftState.get() - if (leftState === EmptyValue) Ior.Right(res) else Ior.Both(EmptyValue.unbox(leftState), res) - }.fold({ Ior.Left(it) }, ::identity) -} - -public class IorEffectScope(semigroup: Semigroup, private val effect: EffectScope) : - EffectScope, Semigroup by semigroup { - - @PublishedApi - internal var leftState: AtomicRef = AtomicRef(EmptyValue) - - private fun combine(other: E): E = - leftState.updateAndGet { state -> - if (state === EmptyValue) other else EmptyValue.unbox(state).combine(other) - } as - E - - public suspend fun Ior.bind(): B = - when (this) { - is Ior.Left -> shift(value) - is Ior.Right -> value - is Ior.Both -> { - combine(leftValue) - rightValue - } - } - - override suspend fun shift(r: E): B = effect.shift(combine(r)) -} - -public class IorEagerEffectScope(semigroup: Semigroup, private val effect: EagerEffectScope) : - EagerEffectScope, Semigroup by semigroup { - - @PublishedApi - internal var leftState: AtomicRef = AtomicRef(EmptyValue) - - private fun combine(other: E): E = - leftState.updateAndGet { state -> - if (state === EmptyValue) other else EmptyValue.unbox(state).combine(other) - } as - E - - public suspend fun Ior.bind(): B = - when (this) { - is Ior.Left -> shift(value) - is Ior.Right -> value - is Ior.Both -> { - combine(leftValue) - rightValue - } - } - - @Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL") - override suspend fun shift(r: E): B = effect.shift(combine(r)) -} diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/nullable.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/nullable.kt deleted file mode 100644 index 1a485754b41..00000000000 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/nullable.kt +++ /dev/null @@ -1,67 +0,0 @@ -package arrow.core.continuations - -import arrow.core.Option -import kotlin.contracts.ExperimentalContracts -import kotlin.contracts.contract -import kotlin.jvm.JvmInline - -@JvmInline -public value class NullableEffectScope(private val cont: EffectScope) : EffectScope { - override suspend fun shift(r: Nothing?): B = - cont.shift(r) - - public suspend fun Option.bind(): B = - bind { null } - - @OptIn(ExperimentalContracts::class) - public suspend fun B?.bind(): B { - contract { returns() implies (this@bind != null) } - return this ?: shift(null) - } - - public suspend fun ensure(value: Boolean): Unit = - ensure(value) { null } -} - -@JvmInline -public value class NullableEagerEffectScope(private val cont: EagerEffectScope) : EagerEffectScope { - @Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL") - override suspend fun shift(r: Nothing?): B = - cont.shift(r) - - public suspend fun Option.bind(): B = - bind { null } - - @OptIn(ExperimentalContracts::class) - public suspend fun B?.bind(): B { - contract { returns() implies (this@bind != null) } - return this ?: shift(null) - } - - public suspend fun ensure(value: Boolean): Unit = - ensure(value) { null } -} - -@OptIn(ExperimentalContracts::class) -public suspend fun NullableEffectScope.ensureNotNull(value: B?): B { - contract { returns() implies (value != null) } - return ensureNotNull(value) { null } -} - -@OptIn(ExperimentalContracts::class) -public suspend fun NullableEagerEffectScope.ensureNotNull(value: B?): B { - contract { returns() implies (value != null) } - return ensureNotNull(value) { null } -} - -@Suppress("ClassName") -public object nullable { - public inline fun eager(crossinline f: suspend NullableEagerEffectScope.() -> A): A? = - eagerEffect { - @Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL") - f(NullableEagerEffectScope(this)) - }.orNull() - - public suspend inline operator fun invoke(crossinline f: suspend NullableEffectScope.() -> A): A? = - effect { f(NullableEffectScope(this)) }.orNull() -} diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/option.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/option.kt deleted file mode 100644 index 1e518d0d408..00000000000 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/option.kt +++ /dev/null @@ -1,64 +0,0 @@ -package arrow.core.continuations - -import arrow.core.None -import arrow.core.Option -import arrow.core.Some -import arrow.core.identity -import kotlin.contracts.ExperimentalContracts -import kotlin.contracts.contract -import kotlin.jvm.JvmInline - -public suspend fun Effect.toOption(): Option = - fold(::identity) { Some(it) } - -public fun EagerEffect.toOption(): Option = - fold(::identity) { Some(it) } - -@JvmInline -public value class OptionEffectScope(private val cont: EffectScope) : EffectScope { - override suspend fun shift(r: None): B = - cont.shift(r) - - public suspend fun Option.bind(): B = - bind { None } - - public suspend fun ensure(value: Boolean): Unit = - ensure(value) { None } -} - -@OptIn(ExperimentalContracts::class) -public suspend fun OptionEffectScope.ensureNotNull(value: B?): B { - contract { returns() implies (value != null) } - return ensureNotNull(value) { None } -} - -@OptIn(ExperimentalContracts::class) -public suspend fun OptionEagerEffectScope.ensureNotNull(value: B?): B { - contract { returns() implies (value != null) } - return ensureNotNull(value) { None } -} - -@JvmInline -public value class OptionEagerEffectScope(private val cont: EagerEffectScope) : EagerEffectScope { - @Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL") - override suspend fun shift(r: None): B = - cont.shift(r) - - public suspend fun Option.bind(): B = - bind { None } - - public suspend fun ensure(value: Boolean): Unit = - ensure(value) { None } -} - -@Suppress("ClassName") -public object option { - public inline fun eager(crossinline f: suspend OptionEagerEffectScope.() -> A): Option = - eagerEffect { - @Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL") - f(OptionEagerEffectScope(this)) - }.toOption() - - public suspend inline operator fun invoke(crossinline f: suspend OptionEffectScope.() -> A): Option = - effect { f(OptionEffectScope(this)) }.toOption() -} diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/result.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/result.kt deleted file mode 100644 index 9b0d7faa89c..00000000000 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/result.kt +++ /dev/null @@ -1,41 +0,0 @@ -package arrow.core.continuations - -import arrow.core.identity -import kotlin.jvm.JvmInline - -public suspend fun Effect.toResult(): Result = - fold({ Result.failure(it) }, { Result.success(it) }) - -public fun EagerEffect.toResult(): Result = - fold({ Result.failure(it) }, { Result.success(it) }) - -@JvmInline -public value class ResultEffectScope(private val cont: EffectScope) : EffectScope { - override suspend fun shift(r: Throwable): B = - cont.shift(r) - - public suspend fun Result.bind(): B = - fold(::identity) { shift(it) } -} - -@JvmInline -public value class ResultEagerEffectScope(private val cont: EagerEffectScope) : EagerEffectScope { - @Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL") - override suspend fun shift(r: Throwable): B = - cont.shift(r) - - public suspend fun Result.bind(): B = - fold(::identity) { shift(it) } -} - -@Suppress("ClassName") -public object result { - public inline fun eager(crossinline f: suspend ResultEagerEffectScope.() -> A): Result = - eagerEffect { - @Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL") - f(ResultEagerEffectScope(this)) - }.toResult() - - public suspend inline operator fun invoke(crossinline f: suspend ResultEffectScope.() -> A): Result = - effect { f(ResultEffectScope(this)) }.toResult() -} diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt index 7c32aeae4c4..2d85f082dd6 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt @@ -61,9 +61,9 @@ class EagerEffectSpec : StringSpec({ "attempt - catch" { checkAll(Arb.int(), Arb.long()) { i, l -> eagerEffect { - attempt { + catch({ shift(l) - } catch { ll -> + }) { ll -> ll shouldBe l i } @@ -73,10 +73,10 @@ class EagerEffectSpec : StringSpec({ "attempt - no catch" { checkAll(Arb.int(), Arb.long()) { i, l -> - eagerEffect { - attempt { + eagerEffect { + catch({ i - } catch { ll -> + }) { ll -> ll shouldBe l i + 1 } @@ -95,7 +95,7 @@ class EagerEffectSpec : StringSpec({ "ensure null in eager either computation" { checkAll(Arb.boolean(), Arb.int(), Arb.string()) { predicate, success, shift -> - either.eager { + either { ensure(predicate) { shift } success } shouldBe if (predicate) success.right() else shift.left() @@ -107,7 +107,7 @@ class EagerEffectSpec : StringSpec({ checkAll(Arb.int().orNull(), Arb.string()) { i: Int?, shift: String -> val res = - either.eager { + either { val ii = i ensureNotNull(ii) { shift } square(ii) // Smart-cast by contract @@ -124,9 +124,7 @@ class EagerEffectSpec : StringSpec({ eagerEffect { try { effect.bind() - } catch (shiftError: Suspend) { - fail("Should never come here") - } catch (eagerShiftError: Eager) { + } catch (eagerShiftError: ShiftCancellationException) { throw e } catch (otherError: Throwable) { fail("Should never come here") diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt index af23c30c529..aac7a83f2e2 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt @@ -116,9 +116,9 @@ class EffectSpec : "attempt - catch" { checkAll(Arb.int(), Arb.long()) { i, l -> effect { - attempt { + catch({ shift(l) - } catch { ll -> + }) { ll -> ll shouldBe l i } @@ -128,10 +128,10 @@ class EffectSpec : "attempt - no catch" { checkAll(Arb.int(), Arb.long()) { i, l -> - effect { - attempt { + effect { + catch({ i - } catch { ll -> + }) { ll -> ll shouldBe l i + 1 } @@ -241,12 +241,10 @@ class EffectSpec : val effect = effect { shift("Shift") } val e = RuntimeException("test") Either.catch { - effect { + effect { try { effect.bind() - } catch (eagerShiftError: Eager) { - fail("Should never come here") - } catch (shiftError: Suspend) { + } catch (shiftError: ShiftCancellationException) { e.suspend() } catch (otherError: Throwable) { fail("Should never come here") @@ -262,9 +260,7 @@ class EffectSpec : effect { try { effect.bind() - } catch (eagerShiftError: Eager) { - fail("Should never come here") - } catch (shiftError: Suspend) { + } catch (eagerShiftError: ShiftCancellationException) { e.suspend() } catch (otherError: Throwable) { fail("Should never come here") @@ -298,8 +294,8 @@ class EffectSpec : } val newError: Effect, Int> = - failed.handleErrorWith { str -> - effect { shift(str.reversed().toList()) } + failed.catch { str -> + shift(str.reversed().toList()) } newError.toEither() shouldBe Either.Left(error.reversed().toList()) diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/IorSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/IorSpec.kt index 5f759ff52a9..566bb880cb3 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/IorSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/IorSpec.kt @@ -41,7 +41,7 @@ class IorSpec : } "Accumulates eagerly" { - ior.eager(Semigroup.string()) { + ior(Semigroup.string()) { val one = Ior.Both("Hello", 1).bind() val two = Ior.Both(", World!", 2).bind() one + two @@ -49,7 +49,7 @@ class IorSpec : } "Accumulates with Either eagerly" { - ior.eager(Semigroup.string()) { + ior(Semigroup.string()) { val one = Ior.Both("Hello", 1).bind() val two: Int = Either.Left(", World!").bind() one + two diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/OptionSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/OptionSpec.kt index 59d677ac9b7..c062d828802 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/OptionSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/OptionSpec.kt @@ -41,7 +41,7 @@ class OptionSpec : StringSpec({ } "eager short circuit null" { - option.eager { + option { val number: Int = "s".length ensureNotNull(number.takeIf { it > 1 }) throw IllegalStateException("This should not be executed") diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/StructuredConcurrencySpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/StructuredConcurrencySpec.kt index e3049932bc7..8b3b558dfb7 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/StructuredConcurrencySpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/StructuredConcurrencySpec.kt @@ -47,12 +47,10 @@ class StructuredConcurrencySpec : async { started.await() shift("hello") - } - .await() + }.await() never.await() } - } - .runCont() shouldBe "hello" + }.runCont() shouldBe "hello" withTimeout(2.seconds) { cancelled.await().shouldNotBeNull().message shouldBe "Shifted Continuation" diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/predef.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/predef.kt index c608946d68b..226dfec69ff 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/predef.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/predef.kt @@ -4,8 +4,8 @@ import arrow.core.identity suspend fun Effect.value(): A = fold(::identity, ::identity) -suspend fun Effect<*, *>.runCont(): Any? = fold(::identity, ::identity) +suspend fun Effect.runCont(): Any? = fold(::identity, ::identity) fun EagerEffect.value(): A = fold(::identity, ::identity) -fun EagerEffect<*, *>.runCont(): Any? = fold(::identity, ::identity) +fun EagerEffect.runCont(): Any? = fold(::identity, ::identity) diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/arrow/core/continuations/EffectUsage.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/arrow/core/continuations/EffectUsage.kt index b61c79d6db7..1509b0d0028 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/arrow/core/continuations/EffectUsage.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/arrow/core/continuations/EffectUsage.kt @@ -9,15 +9,15 @@ sealed interface OtherError : MyError { object Actual : OtherError } -context(EffectScope) +context(Shift) suspend fun subprogram(): Unit = println("Hello SubProgram!") -context(EffectScope) +context(Shift) suspend fun otherprogram(): Unit = println("Hello OtherProgram!") -context(EffectScope) +context(Shift) suspend fun fail(): MyResponse = shift(OtherError.Actual) @@ -38,11 +38,11 @@ object EmptyResponse : MyResponse data class ErrorResponse(val error: Throwable) : MyResponse data class BodyResponse(val body: String) : MyResponse -context(EffectScope) +context(Shift) suspend fun respondWithBody(): BodyResponse = BodyResponse("Hello Program!") -context(EffectScope) +context(Shift) suspend fun attemptOrError(): MyResponse = ErrorResponse(RuntimeException("Oh no!")) diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-arrow-core-either-contains-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-arrow-core-either-contains-01.kt deleted file mode 100644 index 521fb6e272d..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-arrow-core-either-contains-01.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleArrowCoreEitherContains01 - -import arrow.core.Either.Right -import arrow.core.Either.Left -import arrow.core.contains - -fun main() { - Right("something").contains("something") // Result: true - Right("something").contains("anything") // Result: false - Left("something").contains("something") // Result: false -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-01.kt deleted file mode 100644 index 3516aaae56c..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-01.kt +++ /dev/null @@ -1,17 +0,0 @@ -// This file was automatically generated from EagerEffect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEagerEffect01 - -import arrow.core.continuations.eagerEffect -import io.kotest.matchers.shouldBe - -fun main() { - val shift = eagerEffect { - shift("Hello, World!") - }.fold({ str: String -> str }, { int -> int.toString() }) - shift shouldBe "Hello, World!" - - val res = eagerEffect { - 1000 - }.fold({ str: String -> str.length }, { int -> int }) - res shouldBe 1000 -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-02.kt deleted file mode 100644 index 4fa5402ed60..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-02.kt +++ /dev/null @@ -1,26 +0,0 @@ -// This file was automatically generated from EagerEffect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEagerEffect02 - -import arrow.core.Either -import arrow.core.None -import arrow.core.Option -import arrow.core.Validated -import arrow.core.continuations.eagerEffect -import io.kotest.assertions.fail -import io.kotest.matchers.shouldBe - -fun main() { - eagerEffect { - val x = Either.Right(1).bind() - val y = Validated.Valid(2).bind() - val z = Option(3).bind { "Option was empty" } - x + y + z - }.fold({ fail("Shift can never be the result") }, { it shouldBe 6 }) - - eagerEffect { - val x = Either.Right(1).bind() - val y = Validated.Valid(2).bind() - val z: Int = None.bind { "Option was empty" } - x + y + z - }.fold({ it shouldBe "Option was empty" }, { fail("Int can never be the result") }) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-01.kt deleted file mode 100644 index 5c14e191cc2..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-01.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from EagerEffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEagerEffectScope01 - -import arrow.core.continuations.eagerEffect -import io.kotest.assertions.fail -import io.kotest.matchers.shouldBe - -fun main() { - eagerEffect { - shift("SHIFT ME") - }.fold({ it shouldBe "SHIFT ME" }, { fail("Computation never finishes") }) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-02.kt deleted file mode 100644 index 77b9d72a89e..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-02.kt +++ /dev/null @@ -1,20 +0,0 @@ -// This file was automatically generated from EagerEffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEagerEffectScope02 - -import arrow.core.Either -import arrow.core.continuations.EagerEffect -import arrow.core.continuations.eagerEffect -import arrow.core.identity -import io.kotest.matchers.shouldBe - -fun Either.toEagerEffect(): EagerEffect = eagerEffect { - fold({ e -> shift(e) }, ::identity) -} - -fun main() { - val either = Either.Left("failed") - eagerEffect { - val x: Int = either.toEagerEffect().bind() - x - }.toEither() shouldBe either -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-03.kt deleted file mode 100644 index c3044107ff5..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-03.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from EagerEffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEagerEffectScope03 - -import arrow.core.Either -import arrow.core.continuations.eagerEffect -import io.kotest.matchers.shouldBe - -fun main() { - val either = Either.Right(9) - eagerEffect { - val x: Int = either.bind() - x - }.toEither() shouldBe either -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-04.kt deleted file mode 100644 index c7e36de2b1b..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-04.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from EagerEffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEagerEffectScope04 - -import arrow.core.Validated -import arrow.core.continuations.eagerEffect -import io.kotest.matchers.shouldBe - -fun main() { - val validated = Validated.Valid(40) - eagerEffect { - val x: Int = validated.bind() - x - }.toValidated() shouldBe validated -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-05.kt deleted file mode 100644 index 91d6b7da7e7..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-05.kt +++ /dev/null @@ -1,15 +0,0 @@ -// This file was automatically generated from EagerEffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEagerEffectScope05 - -import arrow.core.continuations.eagerEffect -import arrow.core.identity -import io.kotest.matchers.shouldBe - -private val default = "failed" -fun main() { - val result = Result.success(1) - eagerEffect { - val x: Int = result.bind { _: Throwable -> default } - x - }.fold({ default }, ::identity) shouldBe result.getOrElse { default } -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-06.kt deleted file mode 100644 index a21aa401fe2..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-06.kt +++ /dev/null @@ -1,18 +0,0 @@ -// This file was automatically generated from EagerEffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEagerEffectScope06 - -import arrow.core.None -import arrow.core.Option -import arrow.core.continuations.eagerEffect -import arrow.core.getOrElse -import arrow.core.identity -import io.kotest.matchers.shouldBe - -private val default = "failed" -fun main() { - val option: Option = None - eagerEffect { - val x: Int = option.bind { default } - x - }.fold({ default }, ::identity) shouldBe option.getOrElse { default } -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-07.kt deleted file mode 100644 index 9338d3e4f54..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-07.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from EagerEffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEagerEffectScope07 - -import arrow.core.Either -import arrow.core.continuations.eagerEffect -import io.kotest.matchers.shouldBe - -fun main() { - val condition = true - val failure = "failed" - val int = 4 - eagerEffect { - ensure(condition) { failure } - int - }.toEither() shouldBe if(condition) Either.Right(int) else Either.Left(failure) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-08.kt deleted file mode 100644 index a68c64d66ca..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-08.kt +++ /dev/null @@ -1,20 +0,0 @@ -// This file was automatically generated from EagerEffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEagerEffectScope08 - -import arrow.core.Either -import arrow.core.None -import arrow.core.Option -import arrow.core.Validated -import arrow.core.continuations.eagerEffect -import io.kotest.assertions.fail -import io.kotest.matchers.shouldBe - -fun main() { - eagerEffect { - val x = Either.Right(1).bind() - val y = Validated.Valid(2).bind() - val z = - attempt { None.bind { "Option was empty" } } catch { 0 } - x + y + z - }.fold({ fail("Shift can never be the result") }, { it shouldBe 3 }) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-09.kt deleted file mode 100644 index 9bd00d04b02..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eager-effect-scope-09.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from EagerEffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEagerEffectScope09 - -import arrow.core.continuations.eagerEffect -import arrow.core.continuations.ensureNotNull -import arrow.core.left -import arrow.core.right -import io.kotest.matchers.shouldBe - -fun main() { - val failure = "failed" - val int: Int? = null - eagerEffect { - ensureNotNull(int) { failure } - }.toEither() shouldBe (int?.right() ?: failure.left()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-01.kt deleted file mode 100644 index 19fc37c45ee..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-01.kt +++ /dev/null @@ -1,17 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffect01 - -import arrow.core.continuations.effect -import io.kotest.matchers.shouldBe - -suspend fun main() { - val shift = effect { - shift("Hello, World!") - }.fold({ str: String -> str }, { int -> int.toString() }) - shift shouldBe "Hello, World!" - - val res = effect { - 1000 - }.fold({ str: String -> str.length }, { int -> int }) - res shouldBe 1000 -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-02.kt deleted file mode 100644 index 4073b9fdfce..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-02.kt +++ /dev/null @@ -1,26 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffect02 - -import arrow.core.Either -import arrow.core.None -import arrow.core.Option -import arrow.core.Validated -import arrow.core.continuations.effect -import io.kotest.assertions.fail -import io.kotest.matchers.shouldBe - -suspend fun main() { - effect { - val x = Either.Right(1).bind() - val y = Validated.Valid(2).bind() - val z = Option(3).bind { "Option was empty" } - x + y + z - }.fold({ fail("Shift can never be the result") }, { it shouldBe 6 }) - - effect { - val x = Either.Right(1).bind() - val y = Validated.Valid(2).bind() - val z: Int = None.bind { "Option was empty" } - x + y + z - }.fold({ it shouldBe "Option was empty" }, { fail("Int can never be the result") }) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-01.kt deleted file mode 100644 index 87220b66503..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-01.kt +++ /dev/null @@ -1,17 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectGuide01 - -import arrow.core.continuations.Effect -import arrow.core.continuations.effect -import arrow.core.continuations.ensureNotNull - -object EmptyPath - -fun readFile(path: String): Effect = effect { - if (path.isEmpty()) shift(EmptyPath) else Unit -} - -fun readFile2(path: String?): Effect = effect { - ensureNotNull(path) { EmptyPath } - ensure(path.isNotEmpty()) { EmptyPath } -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-02.kt deleted file mode 100644 index 4db071b7fa8..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-02.kt +++ /dev/null @@ -1,49 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectGuide02 - -import arrow.core.Either -import arrow.core.Ior -import arrow.core.None -import arrow.core.Validated -import arrow.core.continuations.Effect -import arrow.core.continuations.effect -import arrow.core.continuations.ensureNotNull -import io.kotest.matchers.collections.shouldNotBeEmpty -import io.kotest.matchers.shouldBe -import io.kotest.matchers.types.shouldBeInstanceOf -import java.io.File -import java.io.FileNotFoundException - -@JvmInline -value class Content(val body: List) - -sealed interface FileError -@JvmInline value class SecurityError(val msg: String?) : FileError -@JvmInline value class FileNotFound(val path: String) : FileError -object EmptyPath : FileError { - override fun toString() = "EmptyPath" -} - -fun readFile(path: String?): Effect = effect { - ensureNotNull(path) { EmptyPath } - ensure(path.isNotEmpty()) { EmptyPath } - try { - val lines = File(path).readLines() - Content(lines) - } catch (e: FileNotFoundException) { - shift(FileNotFound(path)) - } catch (e: SecurityException) { - shift(SecurityError(e.message)) - } -} - -suspend fun main() { - readFile("").toEither() shouldBe Either.Left(EmptyPath) - readFile("knit.properties").toValidated() shouldBe Validated.Invalid(FileNotFound("knit.properties")) - readFile("gradle.properties").toIor() shouldBe Ior.Left(FileNotFound("gradle.properties")) - readFile("README.MD").toOption { None } shouldBe None - - readFile("build.gradle.kts").fold({ _: FileError -> null }, { it }) - .shouldBeInstanceOf() - .body.shouldNotBeEmpty() -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-03.kt deleted file mode 100644 index cfd166ecda4..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-03.kt +++ /dev/null @@ -1,15 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectGuide03 - -import arrow.core.Either -import arrow.core.None -import arrow.core.Option -import arrow.core.Some -import arrow.core.continuations.Effect -import arrow.core.identity - -suspend fun Effect.toEither(): Either = - fold({ Either.Left(it) }) { Either.Right(it) } - -suspend fun Effect.toOption(): Option = - fold(::identity) { Some(it) } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-04.kt deleted file mode 100644 index 039a7090e29..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-04.kt +++ /dev/null @@ -1,33 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectGuide04 - -import arrow.core.Either -import arrow.core.continuations.Effect -import arrow.core.continuations.effect -import arrow.core.identity -import io.kotest.matchers.shouldBe - -val failed: Effect = - effect { shift("failed") } - -val resolved: Effect = - failed.handleError { it.length } - -val newError: Effect, Int> = - failed.handleErrorWith { str -> - effect { shift(str.reversed().toList()) } - } - -val redeemed: Effect = - failed.redeem({ str -> str.length }, ::identity) - -val captured: Effect> = - effect { 1 }.attempt() - -suspend fun main() { - failed.toEither() shouldBe Either.Left("failed") - resolved.toEither() shouldBe Either.Right(6) - newError.toEither() shouldBe Either.Left(listOf('d', 'e', 'l', 'i', 'a', 'f')) - redeemed.toEither() shouldBe Either.Right(6) - captured.toEither() shouldBe Either.Right(Result.success(1)) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-05.kt deleted file mode 100644 index 4ff9b792766..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-05.kt +++ /dev/null @@ -1,24 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectGuide05 - -import arrow.core.continuations.effect -import arrow.fx.coroutines.ExitCase -import arrow.fx.coroutines.guaranteeCase -import arrow.fx.coroutines.parZip -import io.kotest.assertions.fail -import io.kotest.matchers.shouldBe -import io.kotest.matchers.types.shouldBeTypeOf -import kotlinx.coroutines.CompletableDeferred -import kotlinx.coroutines.awaitCancellation - -suspend fun awaitExitCase(exit: CompletableDeferred): A = - guaranteeCase(::awaitCancellation) { exitCase -> exit.complete(exitCase) } - - suspend fun main() { - val error = "Error" - val exit = CompletableDeferred() - effect { - parZip({ awaitExitCase(exit) }, { shift(error) }) { a, b -> a + b } - }.fold({ it shouldBe error }, { fail("Int can never be the result") }) - exit.await().shouldBeTypeOf() -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-06.kt deleted file mode 100644 index f13306a75eb..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-06.kt +++ /dev/null @@ -1,31 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectGuide06 - -import arrow.core.continuations.effect -import arrow.fx.coroutines.ExitCase -import arrow.fx.coroutines.guaranteeCase -import arrow.fx.coroutines.parTraverse -import io.kotest.assertions.fail -import io.kotest.matchers.shouldBe -import io.kotest.matchers.types.shouldBeTypeOf -import kotlinx.coroutines.CompletableDeferred -import kotlinx.coroutines.awaitCancellation - -suspend fun awaitExitCase(exit: CompletableDeferred): A = - guaranteeCase(::awaitCancellation) { exitCase -> exit.complete(exitCase) } - -suspend fun CompletableDeferred.getOrNull(): A? = - if (isCompleted) await() else null - -suspend fun main() { - val error = "Error" - val exits = (0..3).map { CompletableDeferred() } - effect> { - (0..4).parTraverse { index -> - if (index == 4) shift(error) - else awaitExitCase(exits[index]) - } - }.fold({ msg -> msg shouldBe error }, { fail("Int can never be the result") }) - // It's possible not all parallel task got launched, and in those cases awaitCancellation never ran - exits.forEach { exit -> exit.getOrNull()?.shouldBeTypeOf() } -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-07.kt deleted file mode 100644 index 8d8340ed04e..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-07.kt +++ /dev/null @@ -1,30 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectGuide07 - -import arrow.core.continuations.effect -import arrow.core.merge -import arrow.fx.coroutines.ExitCase -import arrow.fx.coroutines.guaranteeCase -import arrow.fx.coroutines.raceN -import io.kotest.assertions.fail -import io.kotest.matchers.shouldBe -import io.kotest.matchers.types.shouldBeTypeOf -import kotlinx.coroutines.CompletableDeferred -import kotlinx.coroutines.awaitCancellation - -suspend fun awaitExitCase(exit: CompletableDeferred): A = - guaranteeCase(::awaitCancellation) { exitCase -> exit.complete(exitCase) } - -suspend fun CompletableDeferred.getOrNull(): A? = - if (isCompleted) await() else null - -suspend fun main() { - val error = "Error" - val exit = CompletableDeferred() - effect { - raceN({ awaitExitCase(exit) }) { shift(error) } - .merge() // Flatten Either result from race into Int - }.fold({ msg -> msg shouldBe error }, { fail("Int can never be the result") }) - // It's possible not all parallel task got launched, and in those cases awaitCancellation never ran - exit.getOrNull()?.shouldBeTypeOf() -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-08.kt deleted file mode 100644 index d09c8f092a9..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-08.kt +++ /dev/null @@ -1,28 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectGuide08 - -import arrow.core.continuations.effect -import arrow.fx.coroutines.ExitCase -import arrow.fx.coroutines.bracketCase -import io.kotest.assertions.fail -import io.kotest.matchers.shouldBe -import io.kotest.matchers.types.shouldBeTypeOf -import kotlinx.coroutines.CompletableDeferred -import java.io.BufferedReader -import java.io.File - -suspend fun main() { - val error = "Error" - val exit = CompletableDeferred() - effect { - bracketCase( - acquire = { File("build.gradle.kts").bufferedReader() }, - use = { reader: BufferedReader -> shift(error) }, - release = { reader, exitCase -> - reader.close() - exit.complete(exitCase) - } - ) - }.fold({ it shouldBe error }, { fail("Int can never be the result") }) - exit.await().shouldBeTypeOf() -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-09.kt deleted file mode 100644 index 33f99f70dc8..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-09.kt +++ /dev/null @@ -1,30 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectGuide09 - -import arrow.core.continuations.effect -import arrow.fx.coroutines.ExitCase -import arrow.fx.coroutines.Resource -import arrow.fx.coroutines.fromAutoCloseable -import arrow.fx.coroutines.releaseCase -import io.kotest.assertions.fail -import io.kotest.matchers.shouldBe -import io.kotest.matchers.types.shouldBeTypeOf -import kotlinx.coroutines.CompletableDeferred -import java.io.BufferedReader -import java.io.File - -suspend fun main() { - val error = "Error" - val exit = CompletableDeferred() - - fun bufferedReader(path: String): Resource = - Resource.fromAutoCloseable { File(path).bufferedReader() } - .releaseCase { _, exitCase -> exit.complete(exitCase) } - - effect { - val lineCount = bufferedReader("build.gradle.kts") - .use { reader -> shift(error) } - lineCount - }.fold({ it shouldBe error }, { fail("Int can never be the result") }) - exit.await().shouldBeTypeOf() -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-10.kt deleted file mode 100644 index 1dcfa20d166..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-10.kt +++ /dev/null @@ -1,57 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectGuide10 - -import arrow.core.continuations.Effect -import arrow.core.continuations.effect -import arrow.core.continuations.ensureNotNull -import arrow.fx.coroutines.ExitCase -import arrow.fx.coroutines.guaranteeCase -import io.kotest.assertions.fail -import io.kotest.matchers.shouldBe -import io.kotest.matchers.types.shouldBeInstanceOf -import kotlinx.coroutines.CompletableDeferred -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.awaitCancellation -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext -import java.io.File -import java.io.FileNotFoundException - -@JvmInline -value class Content(val body: List) - -sealed interface FileError -@JvmInline value class SecurityError(val msg: String?) : FileError -@JvmInline value class FileNotFound(val path: String) : FileError -object EmptyPath : FileError { - override fun toString() = "EmptyPath" -} - -fun readFile(path: String?): Effect = effect { - ensureNotNull(path) { EmptyPath } - ensure(path.isNotEmpty()) { EmptyPath } - try { - val lines = File(path).readLines() - Content(lines) - } catch (e: FileNotFoundException) { - shift(FileNotFound(path)) - } catch (e: SecurityException) { - shift(SecurityError(e.message)) - } -} - -suspend fun awaitExitCase(exit: CompletableDeferred): A = - guaranteeCase(::awaitCancellation) { exitCase -> exit.complete(exitCase) } - -suspend fun main() { - val exit = CompletableDeferred() - effect { - withContext(Dispatchers.IO) { - val job = launch { awaitExitCase(exit) } - val content = readFile("failure").bind() - job.join() - content.body.size - } - }.fold({ e -> e shouldBe FileNotFound("failure") }, { fail("Int can never be the result") }) - exit.await().shouldBeInstanceOf() -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-11.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-11.kt deleted file mode 100644 index 2ba4c626c4a..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-11.kt +++ /dev/null @@ -1,20 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectGuide11 - -import arrow.core.continuations.effect -import io.kotest.assertions.fail -import io.kotest.matchers.collections.shouldBeIn -import kotlinx.coroutines.async -import kotlinx.coroutines.coroutineScope - -suspend fun main() { - val errorA = "ErrorA" - val errorB = "ErrorB" - coroutineScope { - effect { - val fa = async { shift(errorA) } - val fb = async { shift(errorB) } - fa.await() + fb.await() - }.fold({ error -> error shouldBeIn listOf(errorA, errorB) }, { fail("Int can never be the result") }) - } -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-12.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-12.kt deleted file mode 100644 index 4a5e8be6381..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-12.kt +++ /dev/null @@ -1,21 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectGuide12 - -import arrow.core.continuations.effect -import io.kotest.assertions.fail -import io.kotest.matchers.shouldBe -import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.launch - -suspend fun main() { - val errorA = "ErrorA" - val errorB = "ErrorB" - val int = 45 - effect { - coroutineScope { - launch { shift(errorA) } - launch { shift(errorB) } - int - } - }.fold({ fail("Shift can never finish") }, { it shouldBe int }) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-13.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-13.kt deleted file mode 100644 index e2c8dcdcbea..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-guide-13.kt +++ /dev/null @@ -1,24 +0,0 @@ -// This file was automatically generated from Effect.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectGuide13 - -import arrow.core.continuations.effect -import kotlinx.coroutines.Deferred -import kotlinx.coroutines.async -import kotlinx.coroutines.coroutineScope - -suspend fun main() { - - effect Unit> { - suspend { shift("error") } - }.fold({ }, { leakedShift -> leakedShift.invoke() }) - - val leakedAsync = coroutineScope Deferred> { - suspend { - async { - println("I am never going to run, until I get called invoked from outside") - } - } - } - - leakedAsync.invoke().await() -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-01.kt deleted file mode 100644 index d46e9c0120f..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-01.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from EffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectScope01 - -import arrow.core.continuations.effect -import io.kotest.assertions.fail -import io.kotest.matchers.shouldBe - -suspend fun main() { - effect { - shift("SHIFT ME") - }.fold({ it shouldBe "SHIFT ME" }, { fail("Computation never finishes") }) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-02.kt deleted file mode 100644 index 8add3675e39..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-02.kt +++ /dev/null @@ -1,20 +0,0 @@ -// This file was automatically generated from EffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectScope02 - -import arrow.core.Either -import arrow.core.continuations.Effect -import arrow.core.continuations.effect -import arrow.core.identity -import io.kotest.matchers.shouldBe - -suspend fun Either.toEffect(): Effect = effect { - fold({ e -> shift(e) }, ::identity) -} - -suspend fun main() { - val either = Either.Left("failed") - effect { - val x: Int = either.toEffect().bind() - x - }.toEither() shouldBe either -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-03.kt deleted file mode 100644 index 12a2a3eb6eb..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-03.kt +++ /dev/null @@ -1,21 +0,0 @@ -// This file was automatically generated from EffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectScope03 - -import arrow.core.Either -import arrow.core.continuations.EagerEffect -import arrow.core.continuations.eagerEffect -import arrow.core.continuations.effect -import arrow.core.identity -import io.kotest.matchers.shouldBe - -suspend fun Either.toEagerEffect(): EagerEffect = eagerEffect { - fold({ e -> shift(e) }, ::identity) -} - -suspend fun main() { - val either = Either.Left("failed") - effect { - val x: Int = either.toEagerEffect().bind() - x - }.toEither() shouldBe either -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-04.kt deleted file mode 100644 index 43d90183190..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-04.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from EffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectScope04 - -import arrow.core.Either -import arrow.core.continuations.effect -import io.kotest.matchers.shouldBe - -suspend fun main() { - val either = Either.Right(9) - effect { - val x: Int = either.bind() - x - }.toEither() shouldBe either -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-05.kt deleted file mode 100644 index 2a40e47b9af..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-05.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from EffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectScope05 - -import arrow.core.Validated -import arrow.core.continuations.effect -import io.kotest.matchers.shouldBe - -suspend fun main() { - val validated = Validated.Valid(40) - effect { - val x: Int = validated.bind() - x - }.toValidated() shouldBe validated -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-06.kt deleted file mode 100644 index 996f8515731..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-06.kt +++ /dev/null @@ -1,15 +0,0 @@ -// This file was automatically generated from EffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectScope06 - -import arrow.core.continuations.effect -import arrow.core.identity -import io.kotest.matchers.shouldBe - -private val default = "failed" -suspend fun main() { - val result = Result.success(1) - effect { - val x: Int = result.bind { _: Throwable -> default } - x - }.fold({ default }, ::identity) shouldBe result.getOrElse { default } -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-07.kt deleted file mode 100644 index 877a0362bb1..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-07.kt +++ /dev/null @@ -1,18 +0,0 @@ -// This file was automatically generated from EffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectScope07 - -import arrow.core.None -import arrow.core.Option -import arrow.core.continuations.effect -import arrow.core.getOrElse -import arrow.core.identity -import io.kotest.matchers.shouldBe - -private val default = "failed" -suspend fun main() { - val option: Option = None - effect { - val x: Int = option.bind { default } - x - }.fold({ default }, ::identity) shouldBe option.getOrElse { default } -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-08.kt deleted file mode 100644 index 2df54254d0d..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-08.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from EffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectScope08 - -import arrow.core.Either -import arrow.core.continuations.effect -import io.kotest.matchers.shouldBe - -suspend fun main() { - val condition = true - val failure = "failed" - val int = 4 - effect { - ensure(condition) { failure } - int - }.toEither() shouldBe if(condition) Either.Right(int) else Either.Left(failure) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-09.kt deleted file mode 100644 index 4b4ffa3eb7b..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-09.kt +++ /dev/null @@ -1,20 +0,0 @@ -// This file was automatically generated from EffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectScope09 - -import arrow.core.Either -import arrow.core.None -import arrow.core.Option -import arrow.core.Validated -import arrow.core.continuations.effect -import io.kotest.assertions.fail -import io.kotest.matchers.shouldBe - -suspend fun main() { - effect { - val x = Either.Right(1).bind() - val y = Validated.Valid(2).bind() - val z = - attempt { None.bind { "Option was empty" } } catch { 0 } - x + y + z - }.fold({ fail("Shift can never be the result") }, { it shouldBe 3 }) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-10.kt deleted file mode 100644 index 0d0dc84b89e..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-scope-10.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from EffectScope.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEffectScope10 - -import arrow.core.continuations.effect -import arrow.core.continuations.ensureNotNull -import arrow.core.left -import arrow.core.right -import io.kotest.matchers.shouldBe - -suspend fun main() { - val failure = "failed" - val int: Int? = null - effect { - ensureNotNull(int) { failure } - }.toEither() shouldBe (int?.right() ?: failure.left()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-01.kt deleted file mode 100644 index 313a4846eda..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-01.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither01 - -import arrow.core.andThen - -val throwsSomeStuff: (Int) -> Double = {x -> x.toDouble()} -val throwsOtherThings: (Double) -> String = {x -> x.toString()} -val moreThrowing: (String) -> List = {x -> listOf(x)} -val magic = throwsSomeStuff.andThen(throwsOtherThings).andThen(moreThrowing) -fun main() { - println ("magic = $magic") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-02.kt deleted file mode 100644 index 3050e4d988b..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-02.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither02 - -import arrow.core.Either - -val right: Either = - Either.Right(5) -fun main() { - println(right) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-03.kt deleted file mode 100644 index a30a54e9643..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-03.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither03 - -import arrow.core.Either - -val left: Either = - Either.Left("Something went wrong") -fun main() { - println(left) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-04.kt deleted file mode 100644 index 86bf1302c74..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-04.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither04 - -import arrow.core.Either -import arrow.core.flatMap - -val right: Either = Either.Right(5) -val value = right.flatMap{ Either.Right(it + 1) } -fun main() { - println("value = $value") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-05.kt deleted file mode 100644 index 28c39f3df3f..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-05.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither05 - -import arrow.core.Either -import arrow.core.flatMap - -val left: Either = Either.Left("Something went wrong") -val value = left.flatMap{ Either.Right(it + 1) } -fun main() { - println("value = $value") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-06.kt deleted file mode 100644 index 67df2d4b8cb..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-06.kt +++ /dev/null @@ -1,15 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither06 - -import arrow.core.Either -import arrow.core.flatMap - -fun parse(s: String): Int = - if (s.matches(Regex("-?[0-9]+"))) s.toInt() - else throw NumberFormatException("$s is not a valid integer.") - -fun reciprocal(i: Int): Double = - if (i == 0) throw IllegalArgumentException("Cannot take reciprocal of 0.") - else 1.0 / i - -fun stringify(d: Double): String = d.toString() diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-07.kt deleted file mode 100644 index 6565442240c..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-07.kt +++ /dev/null @@ -1,21 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither07 - -import arrow.core.Either -import arrow.core.flatMap -import arrow.core.left -import arrow.core.right - -// Either Style -fun parse(s: String): Either = - if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt()) - else Either.Left(NumberFormatException("$s is not a valid integer.")) - -fun reciprocal(i: Int): Either = - if (i == 0) Either.Left(IllegalArgumentException("Cannot take reciprocal of 0.")) - else Either.Right(1.0 / i) - -fun stringify(d: Double): String = d.toString() - -fun magic(s: String): Either = - parse(s).flatMap { reciprocal(it) }.map { stringify(it) } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-08.kt deleted file mode 100644 index e25b8aaf693..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-08.kt +++ /dev/null @@ -1,15 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither08 - -import arrow.core.Either - -fun parse(s: String): Either = - if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt()) - else Either.Left(NumberFormatException("$s is not a valid integer.")) - -val notANumber = parse("Not a number") -val number2 = parse("2") -fun main() { - println("notANumber = $notANumber") - println("number2 = $number2") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-09.kt deleted file mode 100644 index 6e01423972f..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-09.kt +++ /dev/null @@ -1,27 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither09 - -import arrow.core.Either -import arrow.core.flatMap - -fun parse(s: String): Either = - if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt()) - else Either.Left(NumberFormatException("$s is not a valid integer.")) - -fun reciprocal(i: Int): Either = - if (i == 0) Either.Left(IllegalArgumentException("Cannot take reciprocal of 0.")) - else Either.Right(1.0 / i) - -fun stringify(d: Double): String = d.toString() - -fun magic(s: String): Either = - parse(s).flatMap{ reciprocal(it) }.map{ stringify(it) } - -val magic0 = magic("0") -val magic1 = magic("1") -val magicNotANumber = magic("Not a number") -fun main() { - println("magic0 = $magic0") - println("magic1 = $magic1") - println("magicNotANumber = $magicNotANumber") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-10.kt deleted file mode 100644 index 84d23516808..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-10.kt +++ /dev/null @@ -1,31 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither10 - -import arrow.core.Either -import arrow.core.flatMap - -fun parse(s: String): Either = - if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt()) - else Either.Left(NumberFormatException("$s is not a valid integer.")) - -fun reciprocal(i: Int): Either = - if (i == 0) Either.Left(IllegalArgumentException("Cannot take reciprocal of 0.")) - else Either.Right(1.0 / i) - -fun stringify(d: Double): String = d.toString() - -fun magic(s: String): Either = - parse(s).flatMap{ reciprocal(it) }.map{ stringify(it) } - -val x = magic("2") -val value = when(x) { - is Either.Left -> when (x.value) { - is NumberFormatException -> "Not a number!" - is IllegalArgumentException -> "Can't take reciprocal of 0!" - else -> "Unknown error" - } - is Either.Right -> "Got reciprocal: ${x.value}" -} -fun main() { - println("value = $value") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-11.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-11.kt deleted file mode 100644 index 3b4172abc69..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-11.kt +++ /dev/null @@ -1,24 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither11 - -import arrow.core.Either -import arrow.core.flatMap -// Either with ADT Style - -sealed class Error { - object NotANumber : Error() - object NoZeroReciprocal : Error() -} - -fun parse(s: String): Either = - if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt()) - else Either.Left(Error.NotANumber) - -fun reciprocal(i: Int): Either = - if (i == 0) Either.Left(Error.NoZeroReciprocal) - else Either.Right(1.0 / i) - -fun stringify(d: Double): String = d.toString() - -fun magic(s: String): Either = - parse(s).flatMap{reciprocal(it)}.map{ stringify(it) } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-12.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-12.kt deleted file mode 100644 index bca693c137c..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-12.kt +++ /dev/null @@ -1,35 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither12 - -import arrow.core.Either -import arrow.core.flatMap - -sealed class Error { - object NotANumber : Error() - object NoZeroReciprocal : Error() -} - -fun parse(s: String): Either = - if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt()) - else Either.Left(Error.NotANumber) - -fun reciprocal(i: Int): Either = - if (i == 0) Either.Left(Error.NoZeroReciprocal) - else Either.Right(1.0 / i) - -fun stringify(d: Double): String = d.toString() - -fun magic(s: String): Either = - parse(s).flatMap{ reciprocal(it) }.map{ stringify(it) } - -val x = magic("2") -val value = when(x) { - is Either.Left -> when (x.value) { - is Error.NotANumber -> "Not a number!" - is Error.NoZeroReciprocal -> "Can't take reciprocal of 0!" - } - is Either.Right -> "Got reciprocal: ${x.value}" -} -fun main() { - println("value = $value") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-13.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-13.kt deleted file mode 100644 index 868a711873d..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-13.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither13 - -import arrow.core.Either - -fun potentialThrowingCode(): String = throw RuntimeException("Blow up!") - -suspend fun makeSureYourLogicDoesNotHaveSideEffects(): Either = - Either.catch { potentialThrowingCode() }.mapLeft { Error.SpecificError } -suspend fun main() { - println("makeSureYourLogicDoesNotHaveSideEffects().isLeft() = ${makeSureYourLogicDoesNotHaveSideEffects().isLeft()}") -} - -sealed class Error { - object SpecificError : Error() -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-14.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-14.kt deleted file mode 100644 index e4aefdd6134..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-14.kt +++ /dev/null @@ -1,79 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither14 - -import arrow.core.Either -import arrow.core.flatMap -import arrow.core.left -import arrow.core.right - -suspend fun httpEndpoint(request: String = "Hello?") = - Either.resolve( - f = { - if (request == "Hello?") "HELLO WORLD!".right() - else Error.SpecificError.left() - }, - success = { a -> handleSuccess({ a: Any -> log(Level.INFO, "This is a: $a") }, a) }, - error = { e -> handleError({ e: Any -> log(Level.WARN, "This is e: $e") }, e) }, - throwable = { throwable -> handleThrowable({ throwable: Throwable -> log(Level.ERROR, "Log the throwable: $throwable.") }, throwable) }, - unrecoverableState = { _ -> Unit.right() } - ) -suspend fun main() { - println("httpEndpoint().status = ${httpEndpoint().status}") -} - -@Suppress("UNUSED_PARAMETER") -suspend fun handleSuccess(log: suspend (a: A) -> Either, a: A): Either = - Either.catch { - Response.Builder(HttpStatus.OK) - .header(CONTENT_TYPE, CONTENT_TYPE_APPLICATION_JSON) - .body(a) - .build() - } - -@Suppress("UNUSED_PARAMETER") -suspend fun handleError(log: suspend (e: E) -> Either, e: E): Either = - createErrorResponse(HttpStatus.NOT_FOUND, ErrorResponse("$ERROR_MESSAGE_PREFIX $e")) - -suspend fun handleThrowable(log: suspend (throwable: Throwable) -> Either, throwable: Throwable): Either = - log(throwable) - .flatMap { createErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, ErrorResponse("$THROWABLE_MESSAGE_PREFIX $throwable")) } - -suspend fun createErrorResponse(httpStatus: HttpStatus, errorResponse: ErrorResponse): Either = - Either.catch { - Response.Builder(httpStatus) - .header(CONTENT_TYPE, CONTENT_TYPE_APPLICATION_JSON) - .body(errorResponse) - .build() - } - -suspend fun log(level: Level, message: String): Either = - Unit.right() // Should implement logging. - -enum class HttpStatus(val value: Int) { OK(200), NOT_FOUND(404), INTERNAL_SERVER_ERROR(500) } - -class Response private constructor( - val status: HttpStatus, - val headers: Map, - val body: Any? -) { - - data class Builder( - val status: HttpStatus, - var headers: Map = emptyMap(), - var body: Any? = null - ) { - fun header(key: String, value: String) = apply { this.headers = this.headers + mapOf(key to value) } - fun body(body: Any?) = apply { this.body = body } - fun build() = Response(status, headers, body) - } -} - -val CONTENT_TYPE = "Content-Type" -val CONTENT_TYPE_APPLICATION_JSON = "application/json" -val ERROR_MESSAGE_PREFIX = "An error has occurred. The error is:" -val THROWABLE_MESSAGE_PREFIX = "An exception was thrown. The exception is:" -sealed class Error { - object SpecificError : Error() -} -data class ErrorResponse(val errorMessage: String) -enum class Level { INFO, WARN, ERROR } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-15.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-15.kt deleted file mode 100644 index 02ba7e88a61..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-15.kt +++ /dev/null @@ -1,13 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither15 - -import arrow.core.Either - -val r : Either = Either.Right(7) -val rightMapLeft = r.mapLeft {it + 1} -val l: Either = Either.Left(7) -val leftMapLeft = l.mapLeft {it + 1} -fun main() { - println("rightMapLeft = $rightMapLeft") - println("leftMapLeft = $leftMapLeft") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-16.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-16.kt deleted file mode 100644 index 20f29c59fcf..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-16.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither16 - -import arrow.core.Either.Left -import arrow.core.Either - -val r: Either = Either.Right(7) -val swapped = r.swap() -fun main() { - println("swapped = $swapped") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-17.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-17.kt deleted file mode 100644 index 4a9e9269241..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-17.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither17 - -import arrow.core.right - -val right7 = - 7.right() -fun main() { - println(right7) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-18.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-18.kt deleted file mode 100644 index 61b4cfa3593..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-18.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither18 - -import arrow.core.left - - val leftHello = - "hello".left() -fun main() { - println(leftHello) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-19.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-19.kt deleted file mode 100644 index 6d1ea1331b2..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-19.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither19 - -import arrow.core.right -import arrow.core.contains - -val x = 7.right() -val contains7 = x.contains(7) -fun main() { - println("contains7 = $contains7") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-20.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-20.kt deleted file mode 100644 index 2dd4aae24ad..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-20.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither20 - -import arrow.core.left -import arrow.core.getOrElse - -val x = "hello".left() -val getOr7 = x.getOrElse { 7 } -fun main() { - println("getOr7 = $getOr7") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-21.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-21.kt deleted file mode 100644 index 67e7eb953bf..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-21.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither21 - -import arrow.core.left -import arrow.core.getOrHandle - -val x = "hello".left() -val value = x.getOrHandle { "$it world!" } -fun main() { - println("value = $value") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-22.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-22.kt deleted file mode 100644 index f9cfa9ae33c..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-22.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither22 - -import arrow.core.Either - -val value = - Either.conditionally(true, { "Error" }, { 42 }) -fun main() { - println(value) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-23.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-23.kt deleted file mode 100644 index 177ba8a52d7..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-23.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither23 - -import arrow.core.Either - -val value = - Either.conditionally(false, { "Error" }, { 42 }) -fun main() { - println(value) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-24.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-24.kt deleted file mode 100644 index 540212da3d0..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-24.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither24 - -import arrow.core.Either -import arrow.core.right - -val x : Either = 7.right() -val fold = x.fold({ 1 }, { it + 3 }) -fun main() { - println("fold = $fold") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-25.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-25.kt deleted file mode 100644 index f212f13b69e..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-25.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither25 - -import arrow.core.Either -import arrow.core.left - -val y : Either = 7.left() -val fold = y.fold({ 1 }, { it + 3 }) -fun main() { - println("fold = $fold") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-26.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-26.kt deleted file mode 100644 index 2efc6e305d4..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-26.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither26 - -import arrow.core.Either -import arrow.core.getOrHandle - -val r: Either = Either.Left(NumberFormatException()) -val httpStatusCode = r.getOrHandle { - when(it) { - is NumberFormatException -> 400 - else -> 500 - } -} -fun main() { - println("httpStatusCode = $httpStatusCode") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-27.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-27.kt deleted file mode 100644 index c446a01405f..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-27.kt +++ /dev/null @@ -1,13 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither27 - -import arrow.core.Either.Right -import arrow.core.leftIfNull - -fun main() { - val value = - //sampleStart - Right(12).leftIfNull({ -1 }) - //sampleEnd - println(value) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-28.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-28.kt deleted file mode 100644 index 9ce8328c1a1..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-28.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither28 - -import arrow.core.Either.Right -import arrow.core.leftIfNull - -val value = - Right(null).leftIfNull({ -1 }) -fun main() { - println(value) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-29.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-29.kt deleted file mode 100644 index b260eb2681a..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-29.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither29 - -import arrow.core.Either.Left -import arrow.core.leftIfNull - -val value = - Left(12).leftIfNull({ -1 }) -fun main() { - println(value) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-30.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-30.kt deleted file mode 100644 index b68274ffcc6..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-30.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither30 - -import arrow.core.rightIfNotNull - -val value = - "value".rightIfNotNull { "left" } -fun main() { - println(value) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-31.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-31.kt deleted file mode 100644 index 2a73a11a0fd..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-31.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither31 - -import arrow.core.rightIfNotNull - -val value = - null.rightIfNotNull { "left" } -fun main() { - println(value) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-32.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-32.kt deleted file mode 100644 index 2fe7fe4f955..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-32.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither32 - -import arrow.core.rightIfNull - -val value = - "value".rightIfNull { "left" } -fun main() { - println(value) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-33.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-33.kt deleted file mode 100644 index bcf492a1a31..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-33.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither33 - -import arrow.core.rightIfNull - -val value = - null.rightIfNull { "left" } -fun main() { - println(value) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-34.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-34.kt deleted file mode 100644 index 45f0b1be289..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-34.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither34 - -import arrow.core.* - -fun main() { - fun possiblyFailingOperation(): Either.Right = - Either.Right(1) - //sampleStart - val result: Either = possiblyFailingOperation() - result.fold( - { println("operation failed with $it") }, - { println("operation succeeded with $it") } - ) - //sampleEnd -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-35.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-35.kt deleted file mode 100644 index 4829bba7255..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-35.kt +++ /dev/null @@ -1,9 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither35 - -import arrow.core.* - -fun main() { - Either.Left("left").swap() // Result: Right("left") - Either.Right("right").swap() // Result: Left("right") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-36.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-36.kt deleted file mode 100644 index 13703d2fd9c..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-36.kt +++ /dev/null @@ -1,9 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither36 - -import arrow.core.* - -fun main() { - Either.Right(12).map { "flower" } // Result: Right("flower") - Either.Left(12).map { "flower" } // Result: Left(12) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-37.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-37.kt deleted file mode 100644 index 70f3bbb16cd..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-37.kt +++ /dev/null @@ -1,9 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither37 - -import arrow.core.* - -fun main() { - Either.Right(12).mapLeft { "flower" } // Result: Right(12) - Either.Left(12).mapLeft { "flower" } // Result: Left("flower") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-38.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-38.kt deleted file mode 100644 index 9964e1011a6..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-38.kt +++ /dev/null @@ -1,9 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither38 - -import arrow.core.* - -fun main() { - Either.Right(12).tapLeft { println("flower") } // Result: Right(12) - Either.Left(12).tapLeft { println("flower") } // Result: prints "flower" and returns: Left(12) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-39.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-39.kt deleted file mode 100644 index 8c03f5ea143..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-39.kt +++ /dev/null @@ -1,9 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither39 - - import arrow.core.* - -fun main() { - Either.Right(12).tap { println("flower") } // Result: prints "flower" and returns: Right(12) - Either.Left(12).tap { println("flower") } // Result: Left(12) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-40.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-40.kt deleted file mode 100644 index 3ec2b39bfa5..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-40.kt +++ /dev/null @@ -1,13 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither40 - -import arrow.core.Either -import arrow.core.Either.Left - -fun main() { - Either.Right(12).exists { it > 10 } // Result: true - Either.Right(7).exists { it > 10 } // Result: false - - val left: Either = Left(12) - left.exists { it > 10 } // Result: false -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-41.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-41.kt deleted file mode 100644 index e8c4e6e06af..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-41.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither41 - -import arrow.core.Either.Left -import arrow.core.Either.Right - -val right = Right(12).orNull() // Result: 12 -val left = Left(12).orNull() // Result: null -fun main() { - println("right = $right") - println("left = $left") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-42.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-42.kt deleted file mode 100644 index cd9256c06b2..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-42.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither42 - -import arrow.core.* - - fun main(args: Array) { - //sampleStart - Either.Left("foo").isEmpty() // Result: true - Either.Right("foo").isEmpty() // Result: false -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-43.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-43.kt deleted file mode 100644 index e98eda5328a..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-43.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither43 - - import arrow.core.* - - fun main(args: Array) { - //sampleStart - Either.Left("foo").isNotEmpty() // Result: false - Either.Right("foo").isNotEmpty() // Result: true - //sampleEnd -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-44.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-44.kt deleted file mode 100644 index 27ed105719f..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-44.kt +++ /dev/null @@ -1,3 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither44 - diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-45.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-45.kt deleted file mode 100644 index df6411d8a14..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-45.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither45 - -import arrow.core.Either.Right -import arrow.core.Either.Left -import arrow.core.getOrElse - -fun main() { - Right(12).getOrElse { 17 } // Result: 12 - Left(12).getOrElse { 17 } // Result: 17 -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-46.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-46.kt deleted file mode 100644 index e0b22d93443..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-46.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither46 - -import arrow.core.Either.Right -import arrow.core.Either.Left - -fun main() { - Right(12).orNull() // Result: 12 - Left(12).orNull() // Result: null -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-47.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-47.kt deleted file mode 100644 index 7e170618794..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-47.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither47 - -import arrow.core.Either.Right -import arrow.core.Either.Left -import arrow.core.getOrHandle - -fun main() { - Right(12).getOrHandle { 17 } // Result: 12 - Left(12).getOrHandle { it + 5 } // Result: 17 -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-48.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-48.kt deleted file mode 100644 index 6d282c44373..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-48.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither48 - -import arrow.core.Either.* -import arrow.core.Either -import arrow.core.filterOrElse - -fun main() { - Right(12).filterOrElse({ it > 10 }, { -1 }) // Result: Right(12) - Right(7).filterOrElse({ it > 10 }, { -1 }) // Result: Left(-1) - - val left: Either = Left(12) - left.filterOrElse({ it > 10 }, { -1 }) // Result: Left(12) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-49.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-49.kt deleted file mode 100644 index 2d14098d02d..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-49.kt +++ /dev/null @@ -1,17 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither49 - -import arrow.core.* - -suspend fun main(): Unit { - //sampleStart - Either.Right(7).filterOrOther({ it == 10 }, { "Value '$it' is not equal to 10" }) - .let(::println) // Either.Left(Value '7' is not equal to 10") - - Either.Right(10).filterOrOther({ it == 10 }, { "Value '$it' is not equal to 10" }) - .let(::println) // Either.Right(10) - - Either.Left(12).filterOrOther({ str: String -> str.contains("impossible") }, { -1 }) - .let(::println) // Either.Left(12) - //sampleEnd -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-50.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-50.kt deleted file mode 100644 index 458058b4dff..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-50.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither50 - -import arrow.core.Either.Left -import arrow.core.Either.Right -import arrow.core.merge - -fun main() { - Right(12).merge() // Result: 12 - Left(12).merge() // Result: 12 -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-51.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-51.kt deleted file mode 100644 index 9826b45ba9a..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-51.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither51 - -import arrow.core.Either.* -import arrow.core.leftIfNull - -fun main() { - Right(12).leftIfNull({ -1 }) // Result: Right(12) - Right(null).leftIfNull({ -1 }) // Result: Left(-1) - - Left(12).leftIfNull({ -1 }) // Result: Left(12) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-52.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-52.kt deleted file mode 100644 index eaf931db471..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-52.kt +++ /dev/null @@ -1,9 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither52 - -import arrow.core.rightIfNotNull - -fun main() { - "value".rightIfNotNull { "left" } // Right(b="value") - null.rightIfNotNull { "left" } // Left(a="left") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-53.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-53.kt deleted file mode 100644 index 37b9c008d27..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-53.kt +++ /dev/null @@ -1,13 +0,0 @@ -// This file was automatically generated from Either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEither53 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val string: Either = "Hello".right() - val chars: Either = - string.widen() - //sampleEnd - println(chars) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-01.kt deleted file mode 100644 index e02ffc279db..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-01.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEitherComputations01 - -import arrow.core.computations.either - -suspend fun main() { - either { - ensure(true) { "" } - println("ensure(true) passes") - ensure(false) { "failed" } - 1 - } - .let(::println) -} -// println: "ensure(true) passes" -// res: Either.Left("failed") diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-02.kt deleted file mode 100644 index ba2f60c2023..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-02.kt +++ /dev/null @@ -1,17 +0,0 @@ -// This file was automatically generated from either.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEitherComputations02 - -import arrow.core.computations.either -import arrow.core.computations.ensureNotNull - -suspend fun main() { - either { - val x: Int? = 1 - ensureNotNull(x) { "passes" } - println(x) - ensureNotNull(null) { "failed" } - } - .let(::println) -} -// println: "1" -// res: Either.Left("failed") diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-01.kt deleted file mode 100644 index a66dcc08cd2..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-01.kt +++ /dev/null @@ -1,21 +0,0 @@ -// This file was automatically generated from Eval.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEval01 - -import arrow.core.Eval - -fun even(n: Int): Eval = - Eval.always { n == 0 }.flatMap { - if(it == true) Eval.now(true) - else odd(n - 1) - } - -fun odd(n: Int): Eval = - Eval.always { n == 0 }.flatMap { - if(it == true) Eval.now(false) - else even(n - 1) - } - -// if not wrapped in eval this type of computation would blow the stack and result in a StackOverflowError -fun main() { - println(odd(100000).value()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-02.kt deleted file mode 100644 index 0923e57eb8d..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-02.kt +++ /dev/null @@ -1,9 +0,0 @@ -// This file was automatically generated from Eval.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEval02 - -import arrow.core.* - -fun main() { - val eager = Eval.now(1).map { it + 1 } - println(eager.value()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-03.kt deleted file mode 100644 index 7fd34596244..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-03.kt +++ /dev/null @@ -1,9 +0,0 @@ -// This file was automatically generated from Eval.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEval03 - -import arrow.core.* - -fun main() { - val lazyEvaled = Eval.later { "expensive computation" } - println(lazyEvaled.value()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-04.kt deleted file mode 100644 index 4b38eb8331a..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-04.kt +++ /dev/null @@ -1,9 +0,0 @@ -// This file was automatically generated from Eval.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleEval04 - -import arrow.core.* - -fun main() { - val alwaysEvaled = Eval.always { "expensive computation" } - println(alwaysEvaled.value()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-01.kt deleted file mode 100644 index a862230d1d2..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-01.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor01 - -import arrow.core.Ior - -fun main() { - Ior.Left("tulip").isRight // Result: false - Ior.Right("venus fly-trap").isRight // Result: true - Ior.Both("venus", "fly-trap").isRight // Result: false -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-02.kt deleted file mode 100644 index 54afb5ce8cb..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-02.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor02 - -import arrow.core.Ior - -fun main() { - Ior.Left("tulip").isLeft // Result: true - Ior.Right("venus fly-trap").isLeft // Result: false - Ior.Both("venus", "fly-trap").isLeft // Result: false -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-03.kt deleted file mode 100644 index 5c61a6f54ee..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-03.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor03 - -import arrow.core.Ior - -fun main() { - Ior.Left("tulip").isBoth // Result: false - Ior.Right("venus fly-trap").isBoth // Result: false - Ior.Both("venus", "fly-trap").isBoth // Result: true -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-04.kt deleted file mode 100644 index 6a71161d52d..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-04.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor04 - -import arrow.core.Ior - -fun main() { - Ior.Right(12).map { "flower" } // Result: Right("flower") - Ior.Left(12).map { "flower" } // Result: Left(12) - Ior.Both(12, "power").map { "flower $it" } // Result: Both(12, "flower power") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-05.kt deleted file mode 100644 index 31da2770112..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-05.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor05 - -import arrow.core.Ior - -fun main() { - Ior.Right(12).bimap ({ "flower" }, { 12 }) // Result: Right(12) - Ior.Left(12).bimap({ "flower" }, { 12 }) // Result: Left("flower") - Ior.Both(12, "power").bimap ({ it * 2 }, { b -> "flower $b" }) // Result: Both("flower power", 24) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-06.kt deleted file mode 100644 index ae89b383b07..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-06.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor06 - -import arrow.core.Ior - -fun main() { - Ior.Right(12).map { "flower" } // Result: Right(12) - Ior.Left(12).map { "flower" } // Result: Left("power") - Ior.Both(12, "power").map { "flower $it" } // Result: Both("flower 12", "power") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-07.kt deleted file mode 100644 index ae4f58d4278..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-07.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor07 - -import arrow.core.Ior - -fun main() { - Ior.Left("left").swap() // Result: Right("left") - Ior.Right("right").swap() // Result: Left("right") - Ior.Both("left", "right").swap() // Result: Both("right", "left") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-08.kt deleted file mode 100644 index 3fad9702f90..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-08.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor08 - -import arrow.core.Ior - -fun main() { - val right = Ior.Right(12).padNull() // Result: Pair(null, 12) - val left = Ior.Left(12).padNull() // Result: Pair(12, null) - val both = Ior.Both("power", 12).padNull() // Result: Pair("power", 12) - - println("right = $right") - println("left = $left") - println("both = $both") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-09.kt deleted file mode 100644 index 1433b18e7d8..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-09.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor09 - -import arrow.core.Ior - -fun main() { - Ior.Right(12).toEither() // Result: Either.Right(12) - Ior.Left(12).toEither() // Result: Either.Left(12) - Ior.Both("power", 12).toEither() // Result: Either.Right(12) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-10.kt deleted file mode 100644 index de4e27ad71d..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-10.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor10 - -import arrow.core.Ior - -fun main() { - val right = Ior.Right(12).orNull() // Result: 12 - val left = Ior.Left(12).orNull() // Result: null - val both = Ior.Both(12, "power").orNull() // Result: "power" - - println("right = $right") - println("left = $left") - println("both = $both") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-11.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-11.kt deleted file mode 100644 index 90e3104ee0c..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-11.kt +++ /dev/null @@ -1,13 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor11 - -import arrow.core.Ior - -fun main() { - val right = Ior.Right(12).leftOrNull() // Result: null - val left = Ior.Left(12).leftOrNull() // Result: 12 - val both = Ior.Both(12, "power").leftOrNull() // Result: 12 - println("right = $right") - println("left = $left") - println("both = $both") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-12.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-12.kt deleted file mode 100644 index 036dce24083..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-12.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor12 - -import arrow.core.Ior - -fun main() { - Ior.Right(12).toValidated() // Result: Valid(12) - Ior.Left(12).toValidated() // Result: Invalid(12) - Ior.Both(12, "power").toValidated() // Result: Valid("power") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-13.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-13.kt deleted file mode 100644 index bc719ff8273..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-13.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor13 - -import arrow.core.Ior - -fun main() { - Ior.Both(5, 12).exists { it > 10 } // Result: true - Ior.Right(12).exists { it > 10 } // Result: true - Ior.Right(7).exists { it > 10 } // Result: false - val left: Ior = Ior.Left(12) - left.exists { it > 10 } // Result: false -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-14.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-14.kt deleted file mode 100644 index 78ff26a2ff5..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-14.kt +++ /dev/null @@ -1,13 +0,0 @@ -// This file was automatically generated from Ior.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIor14 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val string: Ior = Ior.Right("Hello") - val chars: Ior = - string.widen() - //sampleEnd - println(chars) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-01.kt deleted file mode 100644 index e8e55884b74..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-01.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable01 - -import arrow.core.* - -val padRight = listOf(1, 2).padZip(listOf("a")) // Result: [Pair(1, "a"), Pair(2, null)] -val padLeft = listOf(1).padZip(listOf("a", "b")) // Result: [Pair(1, "a"), Pair(null, "b")] -val noPadding = listOf(1, 2).padZip(listOf("a", "b")) // Result: [Pair(1, "a"), Pair(2, "b")] - -fun main() { - println("padRight = $padRight") - println("padLeft = $padLeft") - println("noPadding = $noPadding") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-02.kt deleted file mode 100644 index de7a53a88a3..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-02.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable02 - -import arrow.core.* - -val padZipRight = listOf(1, 2).padZip(listOf("a")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(2, null)] -val padZipLeft = listOf(1).padZip(listOf("a", "b")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(null, "b")] -val noPadding = listOf(1, 2).padZip(listOf("a", "b")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(2, "b")] - -fun main() { - println("padZipRight = $padZipRight") - println("padZipLeft = $padZipLeft") - println("noPadding = $noPadding") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-03.kt deleted file mode 100644 index f15d4349974..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-03.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable03 - -import arrow.core.* - -val left = listOf(1, 2).leftPadZip(listOf("a")) { l, r -> l to r } // Result: [Pair(1, "a")] -val right = listOf(1).leftPadZip(listOf("a", "b")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(null, "b")] -val both = listOf(1, 2).leftPadZip(listOf("a", "b")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(2, "b")] - -fun main() { - println("left = $left") - println("right = $right") - println("both = $both") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-04.kt deleted file mode 100644 index aa5492be95e..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-04.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable04 - -import arrow.core.* - -val padRight = listOf(1, 2).leftPadZip(listOf("a")) // Result: [Pair(1, "a")] -val padLeft = listOf(1).leftPadZip(listOf("a", "b")) // Result: [Pair(1, "a"), Pair(null, "b")] -val noPadding = listOf(1, 2).leftPadZip(listOf("a", "b")) // Result: [Pair(1, "a"), Pair(2, "b")] - -fun main() { - println("padRight = $padRight") - println("padLeft = $padLeft") - println("noPadding = $noPadding") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-05.kt deleted file mode 100644 index 8ad499f39ff..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-05.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable05 - -import arrow.core.* - -val left = listOf(1, 2).rightPadZip(listOf("a")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(null, "b")] -val right = listOf(1).rightPadZip(listOf("a", "b")) { l, r -> l to r } // Result: [Pair(1, "a")] -val both = listOf(1, 2).rightPadZip(listOf("a", "b")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(2, "b")] - -fun main() { - println("left = $left") - println("right = $right") - println("both = $both") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-06.kt deleted file mode 100644 index 0c9feac4761..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-06.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable06 - -import arrow.core.* - -val padRight = listOf(1, 2).rightPadZip(listOf("a")) // Result: [Pair(1, "a"), Pair(2, null)] -val padLeft = listOf(1).rightPadZip(listOf("a", "b")) // Result: [Pair(1, "a")] -val noPadding = listOf(1, 2).rightPadZip(listOf("a", "b")) // Result: [Pair(1, "a"), Pair(2, "b")] - -fun main() { - println("padRight = $padRight") - println("padLeft = $padLeft") - println("noPadding = $noPadding") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-07.kt deleted file mode 100644 index cefd2cecdbe..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-07.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable07 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - listOf("A", "B").align(listOf(1, 2, 3)) { - "$it" - } - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-08.kt deleted file mode 100644 index ecd6d9d8d12..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-08.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable08 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - listOf("A", "B").align(listOf(1, 2, 3)) - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-09.kt deleted file mode 100644 index 77a24907c74..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-09.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable09 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - listOf("A" to 1, "B" to 2).unzip() - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-10.kt deleted file mode 100644 index 18947bafeec..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-10.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable10 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - listOf("A:1", "B:2", "C:3").unzip { e -> - e.split(":").let { - it.first() to it.last() - } - } - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-11.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-11.kt deleted file mode 100644 index 02cd4700e7b..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-11.kt +++ /dev/null @@ -1,13 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable11 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - listOf(("A" to 1).bothIor(), ("B" to 2).bothIor(), "C".leftIor()) - .unalign() - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-12.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-12.kt deleted file mode 100644 index b26baaa2191..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-12.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable12 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - listOf(1, 2, 3).unalign { - it.leftIor() - } - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-13.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-13.kt deleted file mode 100644 index aba3d491dba..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-13.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable13 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - listOf("A", "B", "C").split() - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-14.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-14.kt deleted file mode 100644 index 1d230b0e4e8..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-14.kt +++ /dev/null @@ -1,13 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable14 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val tags = List(10) { "#" } - val result = - tags.interleave(listOf("A", "B", "C")) - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-15.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-15.kt deleted file mode 100644 index 3b4f078741e..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-15.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable15 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - listOf(1,2,3).unweave { i -> listOf("$i, ${i + 1}") } - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-16.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-16.kt deleted file mode 100644 index 5868fe6d58d..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-16.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleIterable16 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - listOf(1,2,3).ifThen(listOf("empty")) { i -> - listOf("$i, ${i + 1}") - } - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-01.kt deleted file mode 100644 index b49b4ae91bd..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-01.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from map.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleMap01 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - mapOf(1 to "A", 2 to "B").zip(mapOf(1 to "1", 2 to "2", 3 to "3")) - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-02.kt deleted file mode 100644 index 49018786155..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-02.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from map.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleMap02 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - mapOf(1 to "A", 2 to "B").zip(mapOf(1 to "1", 2 to "2", 3 to "3")) { - key, a, b -> "$key -> $a # $b" - } - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-03.kt deleted file mode 100644 index e8e18e34b25..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-03.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from map.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleMap03 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - mapOf("1" to 1, "2" to 2).align(mapOf("1" to 1, "2" to 2, "3" to 3)) - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-04.kt deleted file mode 100644 index a6307681fdf..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-04.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from map.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleMap04 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - mapOf("1" to 1, "2" to 2).align(mapOf("1" to 1, "2" to 2, "3" to 3)) { (_, a) -> - "$a" - } - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-05.kt deleted file mode 100644 index 4c612d333a7..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-05.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from map.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleMap05 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - mapOf( - "first" to ("A" to 1).bothIor(), - "second" to ("B" to 2).bothIor(), - "third" to "C".leftIor() - ).unalign() - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-06.kt deleted file mode 100644 index 6efbe31a8ea..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-06.kt +++ /dev/null @@ -1,13 +0,0 @@ -// This file was automatically generated from map.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleMap06 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - mapOf("1" to 1, "2" to 2, "3" to 3) - .unalign { it.leftIor() } - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-07.kt deleted file mode 100644 index 30921292649..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-07.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from map.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleMap07 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - mapOf("first" to ("A" to 1), "second" to ("B" to 2)).unzip() - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-08.kt deleted file mode 100644 index 50b864e1bbf..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-08.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from map.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleMap08 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val result = - mapOf("first" to "A:1", "second" to "B:2", "third" to "C:3").unzip { (_, e) -> - e.split(":").let { - it.first() to it.last() - } - } - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-memoization-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-memoization-01.kt deleted file mode 100644 index cda809e4e34..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-memoization-01.kt +++ /dev/null @@ -1,19 +0,0 @@ -// This file was automatically generated from memoization.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleMemoization01 - -import arrow.core.memoize -fun someWorkIntensiveFunction(someParam: Int): String = "$someParam" - -fun main() { - //sampleStart - val memoizedF = ::someWorkIntensiveFunction.memoize() - - // The first invocation will store the argument and result in a cache inside the `memoizedF` reference. - val value1 = memoizedF(42) - // This second invocation won't really call the `someWorkIntensiveFunction` function - //but retrieve the result from the previous invocation instead. - val value2 = memoizedF(42) - - //sampleEnd - println("$value1 $value2") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-01.kt deleted file mode 100644 index ac3f0223b3c..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-01.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from NonEmptyList.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleNonemptylist01 - -import arrow.core.nonEmptyListOf -import arrow.core.toNonEmptyListOrNull - -fun main() { - println(nonEmptyListOf(1, 2, 3, 4, 5)) - println(listOf(1, 2, 3).toNonEmptyListOrNull()) - println(emptyList().toNonEmptyListOrNull()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-02.kt deleted file mode 100644 index 8c6a37acb50..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-02.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from NonEmptyList.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleNonemptylist02 - -import arrow.core.nonEmptyListOf - -val value = - nonEmptyListOf(1, 2, 3, 4, 5).head -fun main() { - println(value) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-03.kt deleted file mode 100644 index 9e1736be11a..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-03.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from NonEmptyList.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleNonemptylist03 - -import arrow.core.NonEmptyList -import arrow.core.nonEmptyListOf - -fun sumNel(nel: NonEmptyList): Int = - nel.foldLeft(0) { acc, n -> acc + n } -val value = sumNel(nonEmptyListOf(1, 1, 1, 1)) -fun main() { - println("value = $value") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-04.kt deleted file mode 100644 index ff4cf7e26b5..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-04.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from NonEmptyList.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleNonemptylist04 - -import arrow.core.nonEmptyListOf - -val value = - nonEmptyListOf(1, 1, 1, 1).map { it + 1 } -fun main() { - println(value) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-05.kt deleted file mode 100644 index 2815eb54f62..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-05.kt +++ /dev/null @@ -1,17 +0,0 @@ -// This file was automatically generated from NonEmptyList.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleNonemptylist05 - -import arrow.core.NonEmptyList -import arrow.core.nonEmptyListOf - -val nelOne: NonEmptyList = nonEmptyListOf(1, 2, 3) -val nelTwo: NonEmptyList = nonEmptyListOf(4, 5) - -val value = nelOne.flatMap { one -> - nelTwo.map { two -> - one + two - } -} -fun main() { - println("value = $value") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-06.kt deleted file mode 100644 index 45db1b00351..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-06.kt +++ /dev/null @@ -1,21 +0,0 @@ -// This file was automatically generated from NonEmptyList.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleNonemptylist06 - -import arrow.core.NonEmptyList -import arrow.core.nonEmptyListOf -import arrow.core.zip -import java.util.UUID - -data class Person(val id: UUID, val name: String, val year: Int) - -// Note each NonEmptyList is of a different type -val nelId: NonEmptyList = nonEmptyListOf(UUID.randomUUID(), UUID.randomUUID()) -val nelName: NonEmptyList = nonEmptyListOf("William Alvin Howard", "Haskell Curry") -val nelYear: NonEmptyList = nonEmptyListOf(1926, 1900) - -val value = nelId.zip(nelName, nelYear) { id, name, year -> - Person(id, name, year) -} -fun main() { - println("value = $value") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatal-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatal-01.kt deleted file mode 100644 index 32ebd78ac2e..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatal-01.kt +++ /dev/null @@ -1,28 +0,0 @@ -// This file was automatically generated from NonFatal.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleNonfatal01 - -import arrow.* -import arrow.core.* - -fun unsafeFunction(i: Int): String = - when (i) { - 1 -> throw IllegalArgumentException("Non-Fatal") - 2 -> throw OutOfMemoryError("Fatal") - else -> "Hello" - } - -fun main(args: Array) { - val nonFatal: Either = - //sampleStart - try { - Either.Right(unsafeFunction(1)) - } catch (t: Throwable) { - if (NonFatal(t)) { - Either.Left(t) - } else { - throw t - } - } - //sampleEnd - println(nonFatal) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatalorthrow-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatalorthrow-01.kt deleted file mode 100644 index 07bf2372556..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatalorthrow-01.kt +++ /dev/null @@ -1,24 +0,0 @@ -// This file was automatically generated from nonFatalOrThrow.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleNonfatalorthrow01 - -import arrow.* -import arrow.core.* - -fun unsafeFunction(i: Int): String = - when (i) { - 1 -> throw IllegalArgumentException("Non-Fatal") - 2 -> throw OutOfMemoryError("Fatal") - else -> "Hello" - } - -fun main(args: Array) { - val nonFatal: Either = - //sampleStart - try { - Either.Right(unsafeFunction(1)) - } catch (t: Throwable) { - Either.Left(t.nonFatalOrThrow()) - } - //sampleEnd - println(nonFatal) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-01.kt deleted file mode 100644 index 56bda9ce6f8..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-01.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from nullable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleNullableComputations01 - -import arrow.core.computations.nullable - -suspend fun main() { - nullable { - ensure(true) - println("ensure(true) passes") - ensure(false) - 1 - } - .let(::println) -} -// println: "ensure(true) passes" -// res: null diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-02.kt deleted file mode 100644 index a14151a5e3f..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-02.kt +++ /dev/null @@ -1,17 +0,0 @@ -// This file was automatically generated from nullable.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleNullableComputations02 - -import arrow.core.computations.nullable -import arrow.core.computations.ensureNotNull - -suspend fun main() { - nullable { - val x: Int? = 1 - ensureNotNull(x) - println(x) - ensureNotNull(null) - } - .let(::println) -} -// println: "1" -// res: null diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-01.kt deleted file mode 100644 index fb48ae937cf..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-01.kt +++ /dev/null @@ -1,13 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption01 - -import arrow.core.Option -import arrow.core.Some -import arrow.core.none - -val someValue: Option = Some("I am wrapped in something") -val emptyValue: Option = none() -fun main() { - println("value = $someValue") - println("emptyValue = $emptyValue") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-02.kt deleted file mode 100644 index 1432d694e9f..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-02.kt +++ /dev/null @@ -1,9 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption02 - -import arrow.core.None -import arrow.core.Option -import arrow.core.Some - -fun maybeItWillReturnSomething(flag: Boolean): Option = - if (flag) Some("Found value") else None diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-03.kt deleted file mode 100644 index 6da22ce3726..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-03.kt +++ /dev/null @@ -1,17 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption03 - -import arrow.core.None -import arrow.core.Option -import arrow.core.Some -import arrow.core.getOrElse - -fun maybeItWillReturnSomething(flag: Boolean): Option = - if (flag) Some("Found value") else None - -val value1 = - maybeItWillReturnSomething(true) - .getOrElse { "No value" } -fun main() { - println(value1) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-04.kt deleted file mode 100644 index 7c12966a132..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-04.kt +++ /dev/null @@ -1,17 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption04 - -import arrow.core.None -import arrow.core.Option -import arrow.core.Some -import arrow.core.getOrElse - -fun maybeItWillReturnSomething(flag: Boolean): Option = - if (flag) Some("Found value") else None - -val value2 = - maybeItWillReturnSomething(false) - .getOrElse { "No value" } -fun main() { - println(value2) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-05.kt deleted file mode 100644 index a361ab589a3..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-05.kt +++ /dev/null @@ -1,17 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption05 - -import arrow.core.None -import arrow.core.Option -import arrow.core.Some - -fun maybeItWillReturnSomething(flag: Boolean): Option = - if (flag) Some("Found value") else None - - //sampleStart -val valueSome = maybeItWillReturnSomething(true) is None -val valueNone = maybeItWillReturnSomething(false) is None -fun main() { - println("valueSome = $valueSome") - println("valueNone = $valueNone") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-06.kt deleted file mode 100644 index 58aabd86f00..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-06.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption06 - -import arrow.core.Option - -val myString: String? = "Nullable string" -val option: Option = Option.fromNullable(myString) -fun main () { - println("option = $option") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-07.kt deleted file mode 100644 index e1db8c17a18..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-07.kt +++ /dev/null @@ -1,15 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption07 - -import arrow.core.None -import arrow.core.Option -import arrow.core.Some - -val someValue: Option = Some(20.0) -val value = when(someValue) { - is Some -> someValue.value - is None -> 0.0 -} -fun main () { - println("value = $value") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-08.kt deleted file mode 100644 index 724583e8baa..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-08.kt +++ /dev/null @@ -1,15 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption08 - -import arrow.core.None -import arrow.core.Option -import arrow.core.Some - -val noValue: Option = None -val value = when(noValue) { - is Some -> noValue.value - is None -> 0.0 -} -fun main () { - println("value = $value") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-09.kt deleted file mode 100644 index d7a653b6594..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-09.kt +++ /dev/null @@ -1,17 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption09 - -import arrow.core.None -import arrow.core.Option -import arrow.core.Some - -val number: Option = Some(3) -val noNumber: Option = None -val mappedResult1 = number.map { it * 1.5 } -val mappedResult2 = noNumber.map { it * 1.5 } -fun main () { - println("number = $number") - println("noNumber = $noNumber") - println("mappedResult1 = $mappedResult1") - println("mappedResult2 = $mappedResult2") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-10.kt deleted file mode 100644 index d40b355b33f..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-10.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption10 - -import arrow.core.Option -import arrow.core.Some - -val fold = - Some(3).fold({ 1 }, { it * 3 }) -fun main () { - println(fold) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-11.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-11.kt deleted file mode 100644 index 9d6fb8aabf3..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-11.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption11 - -import arrow.core.Option -import arrow.core.none - -val fold = - none().fold({ 1 }, { it * 3 }) -fun main () { - println(fold) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-12.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-12.kt deleted file mode 100644 index 52ce79b7e47..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-12.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption12 - -import arrow.core.some -import arrow.core.none - - val some = 1.some() - val none = none() -fun main () { - println("some = $some") - println("none = $none") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-13.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-13.kt deleted file mode 100644 index b7a2ebd9291..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-13.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption13 - -import arrow.core.toOption - -val nullString: String? = null -val valueFromNull = nullString.toOption() - -val helloString: String? = "Hello" -val valueFromStr = helloString.toOption() -fun main () { - println("valueFromNull = $valueFromNull") - println("valueFromStr = $valueFromStr") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-14.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-14.kt deleted file mode 100644 index 589ff874dd1..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-14.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption14 - -import arrow.core.firstOrNone -import arrow.core.toOption -import arrow.core.Option - -val foxMap = mapOf(1 to "The", 2 to "Quick", 3 to "Brown", 4 to "Fox") - -val empty = foxMap.entries.firstOrNull { it.key == 5 }?.value.let { it?.toCharArray() }.toOption() -val filled = Option.fromNullable(foxMap.entries.firstOrNull { it.key == 5 }?.value.let { it?.toCharArray() }) - -fun main() { - println("empty = $empty") - println("filled = $filled") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-15.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-15.kt deleted file mode 100644 index 26350e3c3cd..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-15.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption15 - -import arrow.core.Some - -fun main() { -val value = - //sampleStart - Some(1).map { it + 1 } - //sampleEnd - println(value) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-16.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-16.kt deleted file mode 100644 index f4cc52a3a80..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-16.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption16 - -import arrow.core.Some - - val value = - Some(1).zip(Some("Hello"), Some(20.0), ::Triple) -fun main() { - println(value) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-17.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-17.kt deleted file mode 100644 index 4fc22facd84..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-17.kt +++ /dev/null @@ -1,17 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption17 - -import arrow.core.computations.option -import arrow.core.Some -import arrow.core.Option - -suspend fun value(): Option = - option { - val a = Some(1).bind() - val b = Some(1 + a).bind() - val c = Some(1 + b).bind() - a + b + c -} -suspend fun main() { - println(value()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-18.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-18.kt deleted file mode 100644 index e47211b5ef6..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-18.kt +++ /dev/null @@ -1,18 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption18 - -import arrow.core.computations.option -import arrow.core.Some -import arrow.core.none -import arrow.core.Option - -suspend fun value(): Option = - option { - val x = none().bind() - val y = Some(1 + x).bind() - val z = Some(1 + y).bind() - x + y + z - } -suspend fun main() { - println(value()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-19.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-19.kt deleted file mode 100644 index 364813486b9..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-19.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption19 - -import arrow.core.Some -import arrow.core.none - -fun main() { - Some(12).tapNone { println("flower") } // Result: Some(12) - none().tapNone { println("flower") } // Result: prints "flower" and returns: None -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-20.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-20.kt deleted file mode 100644 index 2e5652f4aac..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-20.kt +++ /dev/null @@ -1,10 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption20 - -import arrow.core.Some -import arrow.core.none - -fun main() { - Some(12).tap { println("flower") } // Result: prints "flower" and returns: Some(12) - none().tap { println("flower") } // Result: None -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-21.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-21.kt deleted file mode 100644 index 07a9d4ec693..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-21.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption21 - -import arrow.core.Some -import arrow.core.None -import arrow.core.Option - -fun main() { - Some(12).exists { it > 10 } // Result: true - Some(7).exists { it > 10 } // Result: false - - val none: Option = None - none.exists { it > 10 } // Result: false -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-22.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-22.kt deleted file mode 100644 index 9263fd76499..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-22.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption22 - -import arrow.core.Some -import arrow.core.None -import arrow.core.Option - -fun main() { - Some(12).exists { it > 10 } // Result: 12 - Some(7).exists { it > 10 } // Result: null - - val none: Option = None - none.exists { it > 10 } // Result: null -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-23.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-23.kt deleted file mode 100644 index dfcf262a5ae..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-23.kt +++ /dev/null @@ -1,3 +0,0 @@ -// This file was automatically generated from Option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOption23 - diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-01.kt deleted file mode 100644 index bd53693beba..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-01.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOptionComputations01 - -import arrow.core.computations.option - -suspend fun main() { - option { - ensure(true) - println("ensure(true) passes") - ensure(false) - 1 - } - .let(::println) -} -// println: "ensure(true) passes" -// res: None diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-02.kt deleted file mode 100644 index cc9f62c7d1b..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-02.kt +++ /dev/null @@ -1,17 +0,0 @@ -// This file was automatically generated from option.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleOptionComputations02 - -import arrow.core.computations.option -import arrow.core.computations.ensureNotNull - -suspend fun main() { - option { - val x: Int? = 1 - ensureNotNull(x) - println(x) - ensureNotNull(null) - } - .let(::println) -} -// println: "1" -// res: None diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-result-computations-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-result-computations-01.kt deleted file mode 100644 index 66f22e58f7f..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-result-computations-01.kt +++ /dev/null @@ -1,23 +0,0 @@ -// This file was automatically generated from result.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleResultComputations01 - -import arrow.core.* -import arrow.core.computations.result - -fun main() { - result { // We can safely use assertion based operation inside blocks - kotlin.require(false) { "Boom" } - } // Result.Failure(IllegalArgumentException("Boom")) - - result { - Result.failure(RuntimeException("Boom")) - .recover { 1 } - .bind() - } // Result.Success(1) - - result { - val x = Result.success(1).bind() - val y = Result.success(x + 1).bind() - x + y - } // Result.Success(3) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-01.kt deleted file mode 100644 index dcefb6f4865..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-01.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Semiring.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSemiring01 - -import arrow.typeclasses.Semiring - -fun main(args: Array) { - val result = - //sampleStart - Semiring.int().run { 1.combine(2) } - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-02.kt deleted file mode 100644 index b9bc9d80f41..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-02.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Semiring.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSemiring02 - -import arrow.typeclasses.Semiring - -fun main(args: Array) { - val result = - //sampleStart - Semiring.int().run { 2.combineMultiplicate(3) } - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-03.kt deleted file mode 100644 index 663866438fd..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-03.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Semiring.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSemiring03 - -import arrow.typeclasses.Semiring - -fun main(args: Array) { - val result = - //sampleStart - Semiring.int().run { - 1 + 2 - } - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-04.kt deleted file mode 100644 index c96dc96f2a6..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-04.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Semiring.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSemiring04 - -import arrow.typeclasses.Semiring - -fun main(args: Array) { - val result = - //sampleStart - Semiring.int().run { - 2 * 3 - } - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-01.kt deleted file mode 100644 index 12a2be59d90..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-01.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence01 - -import arrow.core.align - -fun main(args: Array) { - //sampleStart - val result = - sequenceOf("A", "B").align(sequenceOf(1, 2, 3)) { - "$it" - } - //sampleEnd - println(result.toList()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-02.kt deleted file mode 100644 index 9566c93918d..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-02.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence02 - -import arrow.core.align - -fun main(args: Array) { - //sampleStart - val result = - sequenceOf("A", "B").align(sequenceOf(1, 2, 3)) - //sampleEnd - println(result.toList()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-03.kt deleted file mode 100644 index 22d99f25487..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-03.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence03 - -import arrow.core.ifThen - -fun main(args: Array) { - //sampleStart - val result = - sequenceOf(1,2,3).ifThen(sequenceOf("empty")) { i -> - sequenceOf("$i, ${i + 1}") - } - //sampleEnd - println(result.toList()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-04.kt deleted file mode 100644 index aa0ab0e390d..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-04.kt +++ /dev/null @@ -1,13 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence04 - -import arrow.core.interleave - -fun main(args: Array) { - //sampleStart - val tags = generateSequence { "#" }.take(10) - val result = - tags.interleave(sequenceOf("A", "B", "C")) - //sampleEnd - println(result.toList()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-05.kt deleted file mode 100644 index 025d052dd5a..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-05.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence05 - -import arrow.core.leftPadZip - -val left = sequenceOf(1, 2).leftPadZip(sequenceOf(3)) { l, r -> l?.plus(r) ?: r } // Result: [4] -val right = sequenceOf(1).leftPadZip(sequenceOf(3, 4)) { l, r -> l?.plus(r) ?: r } // Result: [4, 4] -val both = sequenceOf(1, 2).leftPadZip(sequenceOf(3, 4)) { l, r -> l?.plus(r) ?: r } // Result: [4, 6] - -fun main() { - println("left = $left") - println("right = $right") - println("both = $both") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-06.kt deleted file mode 100644 index bcff445249b..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-06.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence06 - -import arrow.core.leftPadZip - -val padRight = sequenceOf(1, 2).leftPadZip(sequenceOf("a")) // Result: [Pair(1, "a")] -val padLeft = sequenceOf(1).leftPadZip(sequenceOf("a", "b")) // Result: [Pair(1, "a"), Pair(null, "b")] -val noPadding = sequenceOf(1, 2).leftPadZip(sequenceOf("a", "b")) // Result: [Pair(1, "a"), Pair(2, "b")] - -fun main() { - println("padRight = $padRight") - println("padLeft = $padLeft") - println("noPadding = $noPadding") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-07.kt deleted file mode 100644 index d4f7e45722e..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-07.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence07 - -import arrow.core.padZip - -val padRight = sequenceOf(1, 2).padZip(sequenceOf("a")) // Result: [Pair(1, "a"), Pair(2, null)] -val padLeft = sequenceOf(1).padZip(sequenceOf("a", "b")) // Result: [Pair(1, "a"), Pair(null, "b")] -val noPadding = sequenceOf(1, 2).padZip(sequenceOf("a", "b")) // Result: [Pair(1, "a"), Pair(2, "b")] - -fun main() { - println("padRight = $padRight") - println("padLeft = $padLeft") - println("noPadding = $noPadding") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-08.kt deleted file mode 100644 index 1b85b72cfbc..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-08.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence08 - -import arrow.core.padZip - -val padZipRight = sequenceOf(1, 2).padZip(sequenceOf(3)) { l, r -> (l?:0) + (r?:0) } // Result: [4, 2] -val padZipLeft = sequenceOf(1).padZip(sequenceOf(3, 4)) { l, r -> (l?:0) + (r?:0) } // Result: [4, 4] -val noPadding = sequenceOf(1, 2).padZip(sequenceOf(3, 4)) { l, r -> (l?:0) + (r?:0) } // Result: [4, 6] - -fun main() { - println("padZipRight = $padZipRight") - println("padZipLeft = $padZipLeft") - println("noPadding = $noPadding") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-09.kt deleted file mode 100644 index ae75fa6cd22..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-09.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence09 - -import arrow.core.rightPadZip - -val left = sequenceOf(1, 2).rightPadZip(sequenceOf(3)) { l, r -> l + (r?:0) } // Result: [4, 2] -val right = sequenceOf(1).rightPadZip(sequenceOf(3, 4)) { l, r -> l + (r?:0) } // Result: [4] -val both = sequenceOf(1, 2).rightPadZip(sequenceOf(3, 4)) { l, r -> l + (r?:0) } // Result: [4, 6] - -fun main() { - println("left = $left") - println("right = $right") - println("both = $both") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-10.kt deleted file mode 100644 index ecc6a38dab9..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-10.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence10 - -import arrow.core.rightPadZip - -val padRight = sequenceOf(1, 2).rightPadZip(sequenceOf("a")) // Result: [Pair(1, "a"), Pair(2, null)] -val padLeft = sequenceOf(1).rightPadZip(sequenceOf("a", "b")) // Result: [Pair(1, "a")] -val noPadding = sequenceOf(1, 2).rightPadZip(sequenceOf("a", "b")) // Result: [Pair(1, "a"), Pair(2, "b")] - -fun main() { - println("padRight = $padRight") - println("padLeft = $padLeft") - println("noPadding = $noPadding") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-11.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-11.kt deleted file mode 100644 index 0e7ce3cdaaf..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-11.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence11 - -import arrow.core.split - -fun main(args: Array) { - //sampleStart - val result = sequenceOf("A", "B", "C").split() - //sampleEnd - result?.let { println("(${it.first.toList()}, ${it.second.toList()})") } -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-12.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-12.kt deleted file mode 100644 index 3ad3a8684f2..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-12.kt +++ /dev/null @@ -1,13 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence12 - -import arrow.core.bothIor -import arrow.core.leftIor -import arrow.core.unalign - -fun main(args: Array) { - //sampleStart - val result = sequenceOf(("A" to 1).bothIor(), ("B" to 2).bothIor(), "C".leftIor()).unalign() - //sampleEnd - println("(${result.first.toList()}, ${result.second.toList()})") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-13.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-13.kt deleted file mode 100644 index 5f18e813bc1..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-13.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence13 - -import arrow.core.leftIor -import arrow.core.unalign - -fun main(args: Array) { - //sampleStart - val result = sequenceOf(1, 2, 3).unalign { it.leftIor() } - //sampleEnd - println("(${result.first.toList()}, ${result.second.toList()})") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-14.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-14.kt deleted file mode 100644 index 511d154024a..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-14.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence14 - -import arrow.core.unweave - -fun main(args: Array) { - //sampleStart - val result = sequenceOf(1,2,3).unweave { i -> sequenceOf("$i, ${i + 1}") } - //sampleEnd - println(result.toList()) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-15.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-15.kt deleted file mode 100644 index b5f5cf116ae..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-15.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence15 - -import arrow.core.unzip - -fun main(args: Array) { - //sampleStart - val result = sequenceOf("A" to 1, "B" to 2).unzip() - //sampleEnd - println("(${result.first.toList()}, ${result.second.toList()})") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-16.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-16.kt deleted file mode 100644 index 4a8ab2e451d..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-16.kt +++ /dev/null @@ -1,16 +0,0 @@ -// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleSequence16 - -import arrow.core.unzip - -fun main(args: Array) { - //sampleStart - val result = - sequenceOf("A:1", "B:2", "C:3").unzip { e -> - e.split(":").let { - it.first() to it.last() - } - } - //sampleEnd - println("(${result.first.toList()}, ${result.second.toList()})") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-01.kt deleted file mode 100644 index fc02b28723c..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-01.kt +++ /dev/null @@ -1,14 +0,0 @@ -// This file was automatically generated from Validated.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleValidated01 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val f = Validated.lift(String::toUpperCase, Int::inc) - val res1 = f("test".invalid()) - val res2 = f(1.valid()) - //sampleEnd - println("res1: $res1") - println("res2: $res2") -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-02.kt deleted file mode 100644 index 68849d20db2..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-02.kt +++ /dev/null @@ -1,12 +0,0 @@ -// This file was automatically generated from Validated.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleValidated02 - -import arrow.core.* - -fun main(args: Array) { - val result = - //sampleStart - "Hello World".valid().void() - //sampleEnd - println(result) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-03.kt deleted file mode 100644 index e5b5f1336cf..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-03.kt +++ /dev/null @@ -1,9 +0,0 @@ -// This file was automatically generated from Validated.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleValidated03 - -import arrow.core.Validated - -fun main() { - Validated.Valid(12).tapInvalid { println("flower") } // Result: Valid(12) - Validated.Invalid(12).tapInvalid { println("flower") } // Result: prints "flower" and returns: Invalid(12) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-04.kt deleted file mode 100644 index c13fa930955..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-04.kt +++ /dev/null @@ -1,9 +0,0 @@ -// This file was automatically generated from Validated.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleValidated04 - -import arrow.core.Validated - -fun main() { - Validated.Valid(12).tap { println("flower") } // Result: prints "flower" and returns: Valid(12) - Validated.Invalid(12).tap { println("flower") } // Result: Invalid(12) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-05.kt deleted file mode 100644 index af73083b797..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-05.kt +++ /dev/null @@ -1,13 +0,0 @@ -// This file was automatically generated from Validated.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleValidated05 - -import arrow.core.* - -fun main(args: Array) { - //sampleStart - val string: Validated = "Hello".valid() - val chars: Validated = - string.widen() - //sampleEnd - println(chars) -} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-06.kt deleted file mode 100644 index 4cf01a61751..00000000000 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-06.kt +++ /dev/null @@ -1,11 +0,0 @@ -// This file was automatically generated from Validated.kt by Knit tool. Do not edit. -package arrow.core.examples.exampleValidated06 - -import arrow.core.Validated -import arrow.core.andThen - -fun main() { - Validated.Valid(5).andThen { Validated.Valid(10) } // Result: Valid(10) - Validated.Valid(5).andThen { Validated.Invalid(10) } // Result: Invalid(10) - Validated.Invalid(5).andThen { Validated.Valid(10) } // Result: Invalid(5) -} From a01c496df740fa124244f1b6982179c0b491c271 Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Tue, 16 Aug 2022 22:30:41 +0200 Subject: [PATCH 02/20] Do some clean-up, make API on par with Arrow 1.x.x --- .../arrow/core/continuations/Builders.kt | 103 ++++++------------ .../arrow/core/continuations/ErrorHandlers.kt | 58 +++++----- .../kotlin/arrow/core/continuations/Fold.kt | 21 ++-- .../arrow/core/continuations/Mappers.kt | 30 +++-- .../kotlin/arrow/core/continuations/Shift.kt | 52 ++++----- .../core/continuations/EagerEffectSpec.kt | 2 +- .../arrow/core/continuations/EffectSpec.kt | 28 ++--- 7 files changed, 123 insertions(+), 171 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt index 5dd670462dc..bce7f16bc3e 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt @@ -1,5 +1,6 @@ @file:JvmMultifileClass @file:JvmName("Effect") +@file:OptIn(ExperimentalTypeInference::class, ExperimentalContracts::class) package arrow.core.continuations @@ -22,112 +23,68 @@ public typealias EagerEffect = Shift.() -> A public typealias Effect = suspend Shift.() -> A -@OptIn(ExperimentalTypeInference::class) -public inline fun eagerEffect(@BuilderInference noinline f: Shift.() -> A): EagerEffect = f +public inline fun eagerEffect(@BuilderInference noinline block: Shift.() -> A): EagerEffect = block -@OptIn(ExperimentalTypeInference::class) -public inline fun effect(@BuilderInference noinline f: suspend Shift.() -> A): Effect = f +public inline fun effect(@BuilderInference noinline block: suspend Shift.() -> A): Effect = block -public inline fun either(action: Shift.() -> A): Either = - fold>( - { action() }, - { throw it }, - { Either.Left(it) }, - { Either.Right(it) } - ) +public inline fun either(@BuilderInference block: Shift.() -> A): Either = + fold({ block.invoke(this) }, { Either.Left(it) }, { Either.Right(it) }) -public inline fun nullable(f: NullableShift.() -> A): A? = - fold( - { f(NullableShift(this)) }, - { throw it }, - { null }, - { it } - ) +public inline fun nullable(@BuilderInference block: NullableShift.() -> A): A? = + fold({ block(NullableShift(this)) }, { null }, ::identity) + +public inline fun result(@BuilderInference action: ResultShift.() -> A): Result = + fold({ action(ResultShift(this)) }, Result.Companion::failure, Result.Companion::success) + +public inline fun option(@BuilderInference action: OptionShift.() -> A): Option = + fold({ action(OptionShift(this)) }, ::identity, ::Some) + +public inline fun ior(semigroup: Semigroup, @BuilderInference action: IorShift.() -> A): Ior = + fold({ IorShift(semigroup, this).invoke(action) }, { Ior.Left(it) }, ::identity) @JvmInline public value class NullableShift(private val cont: Shift) : Shift { @EffectDSL public fun ensure(value: Boolean): Unit = ensure(value) { null } override fun shift(r: Nothing?): B = cont.shift(r) - public fun Option.bind(): B = bind { shift(null) } - @OptIn(ExperimentalContracts::class) public fun B?.bind(): B { contract { returns() implies (this@bind != null) } return this ?: shift(null) } - @OptIn(ExperimentalContracts::class) public fun ensureNotNull(value: B?): B { contract { returns() implies (value != null) } return ensureNotNull(value) { null } } } -public inline fun result(action: ResultShift.() -> A): Result = - fold>( - { action(ResultShift(this)) }, - { throw it }, - { Result.failure(it) }, - { Result.success(it) } - ) - @JvmInline public value class ResultShift(private val cont: Shift) : Shift { override fun shift(r: Throwable): B = cont.shift(r) public fun Result.bind(): B = fold(::identity) { shift(it) } } -public inline fun option(action: OptionShift.() -> A): Option = - fold>( - { action(OptionShift(this)) }, - { throw it }, - { None }, - { Some(it) } - ) - @JvmInline public value class OptionShift(private val cont: Shift) : Shift { - override fun shift(r: None): B = - cont.shift(r) + override fun shift(r: None): B = cont.shift(r) + public fun Option.bind(): B = bind { shift(None) } + public fun ensure(value: Boolean): Unit = ensure(value) { None } - // public fun Option.bind(): B = bind { None } - - public fun ensure(value: Boolean): Unit = - ensure(value) { None } - - @OptIn(ExperimentalContracts::class) public fun ensureNotNull(value: B?): B { contract { returns() implies (value != null) } return ensureNotNull(value) { None } } } -public inline fun ior( - semigroup: Semigroup, - action: IorShift.() -> A, -): Ior = fold, Ior>( - { - val effect = IorShift(semigroup, this) - val res = action(effect) - val leftState = effect.leftState.get() - if (leftState === EmptyValue) Ior.Right(res) else Ior.Both(EmptyValue.unbox(leftState), res) - }, - { throw it }, - { Ior.Left(it) }, - { it } -) - -public class IorShift(semigroup: Semigroup, private val effect: Shift) : Shift, Semigroup by semigroup { +public class IorShift @PublishedApi internal constructor(semigroup: Semigroup, private val effect: Shift) : + Shift, Semigroup by semigroup { + // TODO this is a mess... @PublishedApi internal var leftState: AtomicRef = AtomicRef(EmptyValue) - - private fun combine(other: E): E = - leftState.updateAndGet { state -> - if (state === EmptyValue) other else EmptyValue.unbox(state).combine(other) - } as E + override fun shift(r: E): B = effect.shift(combine(r)) public fun Ior.bind(): B = when (this) { @@ -139,5 +96,17 @@ public class IorShift(semigroup: Semigroup, private val effect: Shift) } } - override fun shift(r: E): B = effect.shift(combine(r)) + @PublishedApi + internal inline operator fun invoke(action: IorShift.() -> A): Ior { + val res = action(this) + val leftState = leftState.get() + return if (leftState === EmptyValue) Ior.Right(res) + else Ior.Both(EmptyValue.unbox(leftState), res) + } + + @Suppress("UNCHECKED_CAST") + private fun combine(other: E): E = + leftState.updateAndGet { state -> + if (state === EmptyValue) other else EmptyValue.unbox(state).combine(other) + } as E } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt index 539c7c198dd..43d4ce9987b 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt @@ -1,48 +1,42 @@ @file:JvmMultifileClass @file:JvmName("Effect") +@file:OptIn(ExperimentalTypeInference::class) package arrow.core.continuations +import arrow.core.nonFatalOrThrow import kotlin.experimental.ExperimentalTypeInference import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName -@OptIn(ExperimentalTypeInference::class) -public infix fun Effect.catch(@BuilderInference resolve: suspend Shift.(E) -> A): Effect = - effect { - catch({ bind() }) { e -> resolve(e) } - } +@BuilderInference +public infix fun Effect.catch(resolve: suspend Shift.(shifted: E) -> A): Effect = + effect { catch({ invoke() }) { e -> resolve(e) } } -@OptIn(ExperimentalTypeInference::class) -public infix fun Effect.attempt(@BuilderInference recover: suspend Shift.(Throwable) -> A): Effect = - effect { - attempt({ bind() }) { t -> recover(t) } - } +@BuilderInference +public infix fun Effect.attempt(recover: suspend Shift.(throwable: Throwable) -> A): Effect = + effect { attempt({ invoke() }) { t -> recover(t) } } -@OptIn(ExperimentalTypeInference::class) +@BuilderInference @JvmName("attemptOrThrow") public inline infix fun Effect.attempt( - @BuilderInference crossinline recover: suspend Shift.(T) -> A, -): Effect = effect { - attempt({ bind() }) { t: T -> recover(t) } -} - -@OptIn(ExperimentalTypeInference::class) -public infix fun EagerEffect.catch(@BuilderInference resolve: Shift.(E) -> A): EagerEffect = - eagerEffect { - catch({ bind() }) { e -> resolve(e) } - } - -@OptIn(ExperimentalTypeInference::class) -public infix fun EagerEffect.attempt(@BuilderInference recover: Shift.(Throwable) -> A): EagerEffect = - eagerEffect { - attempt({ bind() }) { t -> recover(t) } - } - -@OptIn(ExperimentalTypeInference::class) + crossinline recover: suspend Shift.(T) -> A, +): Effect = + effect { attempt({ invoke() }) { t: T -> recover(t) } } + +public fun Effect.attempt(): Effect> = + effect{ kotlin.runCatching { invoke() }.onFailure { it.nonFatalOrThrow() } } + +@BuilderInference +public infix fun EagerEffect.catch(resolve: Shift.(shifted: E) -> A): EagerEffect = + eagerEffect { catch({ invoke() }) { e -> resolve(e) } } + +@BuilderInference +public infix fun EagerEffect.attempt(recover: Shift.(throwable: Throwable) -> A): EagerEffect = + eagerEffect { attempt({ invoke() }) { t -> recover(t) } } + @JvmName("attemptOrThrow") public inline infix fun EagerEffect.attempt( @BuilderInference crossinline recover: Shift.(T) -> A, -): EagerEffect = eagerEffect { - attempt({ bind() }) { t: T -> recover(t) } -} +): EagerEffect = + eagerEffect { attempt({ invoke() }) { t: T -> recover(t) } } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt index eaf936eb55e..acb9f087fad 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt @@ -1,5 +1,6 @@ @file:JvmMultifileClass @file:JvmName("Effect") +@file:OptIn(ExperimentalTypeInference::class) package arrow.core.continuations @@ -18,12 +19,7 @@ public suspend fun Effect.fold( error: suspend (error: Throwable) -> B, recover: suspend (shifted: R) -> B, transform: suspend (value: A) -> B, -): B = fold( - { bind() }, - { error(it) }, - { recover(it) }, - { transform(it) } -) +): B = fold({ invoke() }, { error(it) }, { recover(it) }, { transform(it) }) public fun EagerEffect.fold(recover: (R) -> B, transform: (A) -> B): B = fold({ throw it }, recover, transform) @@ -34,7 +30,13 @@ public inline fun EagerEffect.fold( transform: (value: A) -> B, ): B = fold({ invoke(this) }, error, recover, transform) -@OptIn(ExperimentalTypeInference::class) +@JvmName("_foldOrThrow") +public inline fun fold( + @BuilderInference program: Shift.() -> A, + recover: (shifted: R) -> B, + transform: (value: A) -> B, +): B = fold(program, { throw it }, recover, transform) + @JvmName("_fold") public inline fun fold( @BuilderInference program: Shift.() -> A, @@ -46,14 +48,14 @@ public inline fun fold( return try { transform(program(shift)) } catch (e: ShiftCancellationException) { + @Suppress("UNCHECKED_CAST") if (shift === e.shift) recover(e.shifted as R) else throw e } catch (e: Throwable) { error(e.nonFatalOrThrow()) } } -@PublishedApi -internal class ShiftCancellationException( +public class ShiftCancellationException( @PublishedApi internal val shifted: Any?, @PublishedApi @@ -62,6 +64,7 @@ internal class ShiftCancellationException( @PublishedApi internal class DefaultShift : Shift { + @Suppress("UNCHECKED_CAST") override fun shift(r: R): B = throw ShiftCancellationException(r, this as DefaultShift) } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt index dd3cee5738c..31115512a00 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt @@ -7,31 +7,27 @@ import arrow.core.Either import arrow.core.None import arrow.core.Option import arrow.core.Some -import arrow.core.identity import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName -public suspend fun Effect.toEither(): Either = - fold({ Either.Left(it) }, { Either.Right(it) }) +public suspend fun Effect.toEither(): Either = either { invoke() } -public fun EagerEffect.toEither(): Either = - fold({ Either.Left(it) }, { Either.Right(it) }) +public fun EagerEffect.toEither(): Either = either { invoke() } +public suspend fun Effect.orNull(): A? = fold({ _: E -> null }) { it } -public suspend fun Effect.orNull(): A? = - fold({ null }, { it }) +public fun EagerEffect.orNull(): A? = fold({ _: E -> null }) { it } -public fun EagerEffect.orNull(): A? = - fold({ null }, { it }) +public suspend fun Effect.toOption(): Option = option { invoke() } -public suspend fun Effect.toOption(): Option = - fold(::identity) { Some(it) } +public fun EagerEffect.toOption(): Option = option { invoke() } -public fun EagerEffect.toOption(): Option = - fold(::identity) { Some(it) } +public suspend fun Effect.toResult(): Result = result { invoke() } -public suspend fun Effect.toResult(): Result = - fold({ Result.failure(it) }) { Result.success(it) } +public fun EagerEffect.toResult(): Result = result { invoke() } -public fun EagerEffect.toResult(): Result = - fold({ Result.failure(it) }) { Result.success(it) } +public suspend fun Effect.toOption(orElse: suspend (E) -> Option): Option = + fold(orElse) { Some(it) } + +public fun EagerEffect.toOption(orElse: (E) -> Option): Option = + fold(orElse) { Some(it) } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt index 7dfacaa4203..35d1b36de70 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt @@ -1,9 +1,12 @@ +@file:OptIn(ExperimentalTypeInference::class) + package arrow.core.continuations import arrow.core.Either import arrow.core.None import arrow.core.Option import arrow.core.Some +import arrow.core.identity import kotlin.contracts.ExperimentalContracts import kotlin.contracts.contract import kotlin.experimental.ExperimentalTypeInference @@ -14,68 +17,55 @@ public annotation class EffectDSL public interface Shift { public fun shift(r: R): B + public fun EagerEffect.bind(): B = invoke(this@Shift) + public operator fun EagerEffect.invoke(): B = invoke(this@Shift) + public suspend fun Effect.bind(): B = invoke(this@Shift) - public fun Either.bind(): B = when(this) { + public suspend operator fun Effect.invoke(): B = invoke(this@Shift) + + public fun Either.bind(): B = when (this) { is Either.Left -> shift(value) is Either.Right -> value } + + // TODO can be inlined with context receivers, and top-level public fun Option.bind(transform: Shift.(None) -> B): B = - when(this) { + when (this) { None -> transform(None) is Some -> value } + + // TODO can be inlined with context receivers, and top-level + public fun Result.bind(transform: (Throwable) -> R): B = + fold(::identity) { throwable -> shift(transform(throwable)) } } -@OptIn(ExperimentalTypeInference::class) @EffectDSL public inline fun Shift.catch( @BuilderInference action: Shift.() -> A, - @BuilderInference resolve: Shift.(E) -> A, -): A = fold( - { action(this) }, - { throw it }, - { resolve(it) }, - { it } -) + @BuilderInference resolve: Shift.(E) -> A +): A = fold({ action(this) }, { throw it }, { resolve(it) }, { it }) -@OptIn(ExperimentalTypeInference::class) @EffectDSL public inline fun Shift.catch( @BuilderInference action: Shift.() -> A, @BuilderInference resolve: Shift.(E) -> A, @BuilderInference recover: Shift.(Throwable) -> A, -): A = fold( - { action(this) }, - { recover(it) }, - { resolve(it) }, - { it } -) +): A = fold({ action(this) }, { recover(it) }, { resolve(it) }, { it }) -@OptIn(ExperimentalTypeInference::class) @EffectDSL public inline fun Shift.attempt( @BuilderInference action: Shift.() -> A, @BuilderInference recover: Shift.(Throwable) -> A, -): A = fold( - { action(this) }, - { recover(it) }, - { shift(it) }, - { it } -) +): A = fold({ action(this) }, { recover(it) }, { shift(it) }, { it }) -@OptIn(ExperimentalTypeInference::class) @EffectDSL @JvmName("attemptOrThrow") public inline fun Shift.attempt( @BuilderInference action: Shift.() -> A, @BuilderInference recover: Shift.(T) -> A, -): A = fold( - { action(this) }, - { if (it is T) recover(it) else throw it }, - { shift(it) }, - { it } -) +): A = attempt(action) { if (it is T) recover(it) else throw it } @EffectDSL public inline fun Shift.ensure(condition: Boolean, shift: () -> R): Unit = diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt index 2d85f082dd6..6e9f7ce8723 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt @@ -123,7 +123,7 @@ class EagerEffectSpec : StringSpec({ Either.catch { eagerEffect { try { - effect.bind() + effect() } catch (eagerShiftError: ShiftCancellationException) { throw e } catch (otherError: Throwable) { diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt index aac7a83f2e2..228a886d438 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt @@ -31,7 +31,7 @@ class EffectSpec : StringSpec({ "try/catch - can recover from shift" { checkAll(Arb.int(), Arb.string()) { i, s -> - effect { + effect { try { shift(s) } catch (e: Throwable) { @@ -43,7 +43,7 @@ class EffectSpec : "try/catch - can recover from shift suspended" { checkAll(Arb.int(), Arb.string()) { i, s -> - effect { + effect { try { shift(s.suspend()) } catch (e: Throwable) { @@ -144,8 +144,8 @@ class EffectSpec : val eager: EagerEffect = eagerEffect { a } - effect { - val aa = eager.bind() + effect { + val aa = eager() aa + b.suspend() }.runCont() shouldBe (a + b) } @@ -156,8 +156,8 @@ class EffectSpec : val eager: EagerEffect = eagerEffect { shift(a) } - effect { - val aa = eager.bind() + effect { + val aa = eager() aa + b.suspend() }.runCont() shouldBe a } @@ -243,7 +243,7 @@ class EffectSpec : Either.catch { effect { try { - effect.bind() + effect() } catch (shiftError: ShiftCancellationException) { e.suspend() } catch (otherError: Throwable) { @@ -257,9 +257,9 @@ class EffectSpec : val effect = eagerEffect { shift("Shift") } val e = RuntimeException("test") Either.catch { - effect { + effect { try { - effect.bind() + effect() } catch (eagerShiftError: ShiftCancellationException) { e.suspend() } catch (otherError: Throwable) { @@ -271,13 +271,13 @@ class EffectSpec : "#2760 - dispatching in nested Effect blocks does not make the nested Continuation to hang" { checkAll(Arb.string()) { msg -> - fun failure(): Effect = effect { + val failure: Effect = effect { withContext(Dispatchers.Default) {} shift(Failure(msg)) } - effect { - failure().bind() + effect { + failure() 1 }.fold( recover = { it }, @@ -314,7 +314,7 @@ class EffectSpec : failed.fold({ r -> effect, Int> { shift(r.reversed().toList()) - }.bind() + }.invoke() }, ::identity) } @@ -336,7 +336,7 @@ class EffectSpec : "Can shift from thrown exceptions" { checkAll(Arb.string().suspend(), Arb.string().suspend()) { msg, fallback -> - effect { + effect { effect { throw RuntimeException(msg()) }.fold( From 0b97f6c3d434f3939f4847ec6b4842d5bbb8923b Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Tue, 16 Aug 2022 23:00:09 +0200 Subject: [PATCH 03/20] Add some extra methods --- .../arrow/core/continuations/ErrorHandlers.kt | 4 ++-- .../kotlin/arrow/core/continuations/Shift.kt | 20 +++++++++++++++++++ .../core/continuations/EagerEffectSpec.kt | 10 +++++----- .../arrow/core/continuations/EffectSpec.kt | 10 +++++----- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt index 43d4ce9987b..5dfedf065e9 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt @@ -11,7 +11,7 @@ import kotlin.jvm.JvmName @BuilderInference public infix fun Effect.catch(resolve: suspend Shift.(shifted: E) -> A): Effect = - effect { catch({ invoke() }) { e -> resolve(e) } } + effect { catch { e -> resolve(e) } } @BuilderInference public infix fun Effect.attempt(recover: suspend Shift.(throwable: Throwable) -> A): Effect = @@ -29,7 +29,7 @@ public fun Effect.attempt(): Effect> = @BuilderInference public infix fun EagerEffect.catch(resolve: Shift.(shifted: E) -> A): EagerEffect = - eagerEffect { catch({ invoke() }) { e -> resolve(e) } } + eagerEffect { catch { e -> resolve(e) } } @BuilderInference public infix fun EagerEffect.attempt(recover: Shift.(throwable: Throwable) -> A): EagerEffect = diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt index 35d1b36de70..00df32800d8 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt @@ -39,6 +39,26 @@ public interface Shift { // TODO can be inlined with context receivers, and top-level public fun Result.bind(transform: (Throwable) -> R): B = fold(::identity) { throwable -> shift(transform(throwable)) } + + @EffectDSL + public suspend infix fun Effect.catch(@BuilderInference resolve: suspend Shift.(E) -> A): A = + catch({ invoke() }) { resolve(it) } + + @EffectDSL + public infix fun EagerEffect.catch(@BuilderInference resolve: Shift.(E) -> A): A = + catch({ invoke() }, resolve) + + @EffectDSL + public suspend fun Effect.catch( + @BuilderInference action: suspend Shift.() -> A, + @BuilderInference resolve: suspend Shift.(E) -> A, + @BuilderInference recover: suspend Shift.(Throwable) -> A, + ): A = fold({ action(this) }, { recover(it) }, { resolve(it) }, { it }) + + @EffectDSL + public suspend fun Effect.attempt( + @BuilderInference recover: Shift.(Throwable) -> A, + ): A = fold({ recover(it) }, { shift(it) }, { it }) } @EffectDSL diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt index 6e9f7ce8723..a73c477cd8f 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt @@ -61,9 +61,9 @@ class EagerEffectSpec : StringSpec({ "attempt - catch" { checkAll(Arb.int(), Arb.long()) { i, l -> eagerEffect { - catch({ + eagerEffect { shift(l) - }) { ll -> + } catch { ll -> ll shouldBe l i } @@ -73,10 +73,10 @@ class EagerEffectSpec : StringSpec({ "attempt - no catch" { checkAll(Arb.int(), Arb.long()) { i, l -> - eagerEffect { - catch({ + eagerEffect { + eagerEffect { i - }) { ll -> + } catch { ll -> ll shouldBe l i + 1 } diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt index 228a886d438..a7a3a6d774f 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt @@ -116,9 +116,9 @@ class EffectSpec : "attempt - catch" { checkAll(Arb.int(), Arb.long()) { i, l -> effect { - catch({ + effect { shift(l) - }) { ll -> + } catch { ll -> ll shouldBe l i } @@ -128,10 +128,10 @@ class EffectSpec : "attempt - no catch" { checkAll(Arb.int(), Arb.long()) { i, l -> - effect { - catch({ + effect { + effect { i - }) { ll -> + } catch { ll -> ll shouldBe l i + 1 } From 2ff582d9bf2bec11cb6c8a7e61429119542c16bf Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Wed, 17 Aug 2022 07:59:23 +0200 Subject: [PATCH 04/20] Re-add Effect doc, and some missing APIs --- .../arrow/core/continuations/Builders.kt | 8 - .../kotlin/arrow/core/continuations/Effect.kt | 674 ++++++++++++++++++ .../kotlin/arrow/core/continuations/Fold.kt | 8 +- .../arrow/core/continuations/Mappers.kt | 14 + .../kotlin/arrow/core/continuations/Shift.kt | 22 +- .../example-arrow-core-either-contains-01.kt | 12 + .../kotlin/examples/example-effect-01.kt | 18 + .../kotlin/examples/example-effect-02.kt | 55 ++ .../kotlin/examples/example-effect-03.kt | 16 + .../kotlin/examples/example-effect-04.kt | 58 ++ .../kotlin/examples/example-effect-05.kt | 25 + .../kotlin/examples/example-effect-06.kt | 32 + .../kotlin/examples/example-effect-07.kt | 31 + .../kotlin/examples/example-effect-08.kt | 29 + .../kotlin/examples/example-effect-09.kt | 31 + .../kotlin/examples/example-effect-10.kt | 59 ++ .../kotlin/examples/example-effect-11.kt | 21 + .../kotlin/examples/example-effect-12.kt | 22 + .../kotlin/examples/example-effect-13.kt | 25 + .../kotlin/examples/example-either-01.kt | 12 + .../kotlin/examples/example-either-02.kt | 10 + .../kotlin/examples/example-either-03.kt | 10 + .../kotlin/examples/example-either-04.kt | 11 + .../kotlin/examples/example-either-05.kt | 11 + .../kotlin/examples/example-either-06.kt | 15 + .../kotlin/examples/example-either-07.kt | 21 + .../kotlin/examples/example-either-08.kt | 15 + .../kotlin/examples/example-either-09.kt | 27 + .../kotlin/examples/example-either-10.kt | 31 + .../kotlin/examples/example-either-11.kt | 24 + .../kotlin/examples/example-either-12.kt | 35 + .../kotlin/examples/example-either-13.kt | 16 + .../kotlin/examples/example-either-14.kt | 79 ++ .../kotlin/examples/example-either-15.kt | 13 + .../kotlin/examples/example-either-16.kt | 11 + .../kotlin/examples/example-either-17.kt | 10 + .../kotlin/examples/example-either-18.kt | 10 + .../kotlin/examples/example-either-19.kt | 11 + .../kotlin/examples/example-either-20.kt | 11 + .../kotlin/examples/example-either-21.kt | 11 + .../kotlin/examples/example-either-22.kt | 10 + .../kotlin/examples/example-either-23.kt | 10 + .../kotlin/examples/example-either-24.kt | 11 + .../kotlin/examples/example-either-25.kt | 11 + .../kotlin/examples/example-either-26.kt | 16 + .../kotlin/examples/example-either-27.kt | 13 + .../kotlin/examples/example-either-28.kt | 11 + .../kotlin/examples/example-either-29.kt | 11 + .../kotlin/examples/example-either-30.kt | 10 + .../kotlin/examples/example-either-31.kt | 10 + .../kotlin/examples/example-either-32.kt | 10 + .../kotlin/examples/example-either-33.kt | 10 + .../kotlin/examples/example-either-34.kt | 16 + .../kotlin/examples/example-either-35.kt | 9 + .../kotlin/examples/example-either-36.kt | 9 + .../kotlin/examples/example-either-37.kt | 9 + .../kotlin/examples/example-either-38.kt | 9 + .../kotlin/examples/example-either-39.kt | 9 + .../kotlin/examples/example-either-40.kt | 13 + .../kotlin/examples/example-either-41.kt | 12 + .../kotlin/examples/example-either-42.kt | 10 + .../kotlin/examples/example-either-43.kt | 11 + .../kotlin/examples/example-either-44.kt | 3 + .../kotlin/examples/example-either-45.kt | 11 + .../kotlin/examples/example-either-46.kt | 10 + .../kotlin/examples/example-either-47.kt | 11 + .../kotlin/examples/example-either-48.kt | 14 + .../kotlin/examples/example-either-49.kt | 17 + .../kotlin/examples/example-either-50.kt | 11 + .../kotlin/examples/example-either-51.kt | 12 + .../kotlin/examples/example-either-52.kt | 9 + .../kotlin/examples/example-either-53.kt | 13 + .../example-either-computations-01.kt | 16 + .../example-either-computations-02.kt | 17 + .../kotlin/examples/example-eval-01.kt | 21 + .../kotlin/examples/example-eval-02.kt | 9 + .../kotlin/examples/example-eval-03.kt | 9 + .../kotlin/examples/example-eval-04.kt | 9 + .../jvmTest/kotlin/examples/example-ior-01.kt | 10 + .../jvmTest/kotlin/examples/example-ior-02.kt | 10 + .../jvmTest/kotlin/examples/example-ior-03.kt | 10 + .../jvmTest/kotlin/examples/example-ior-04.kt | 10 + .../jvmTest/kotlin/examples/example-ior-05.kt | 10 + .../jvmTest/kotlin/examples/example-ior-06.kt | 10 + .../jvmTest/kotlin/examples/example-ior-07.kt | 10 + .../jvmTest/kotlin/examples/example-ior-08.kt | 14 + .../jvmTest/kotlin/examples/example-ior-09.kt | 10 + .../jvmTest/kotlin/examples/example-ior-10.kt | 14 + .../jvmTest/kotlin/examples/example-ior-11.kt | 13 + .../jvmTest/kotlin/examples/example-ior-12.kt | 10 + .../jvmTest/kotlin/examples/example-ior-13.kt | 12 + .../jvmTest/kotlin/examples/example-ior-14.kt | 13 + .../kotlin/examples/example-iterable-01.kt | 14 + .../kotlin/examples/example-iterable-02.kt | 14 + .../kotlin/examples/example-iterable-03.kt | 14 + .../kotlin/examples/example-iterable-04.kt | 14 + .../kotlin/examples/example-iterable-05.kt | 14 + .../kotlin/examples/example-iterable-06.kt | 14 + .../kotlin/examples/example-iterable-07.kt | 14 + .../kotlin/examples/example-iterable-08.kt | 12 + .../kotlin/examples/example-iterable-09.kt | 12 + .../kotlin/examples/example-iterable-10.kt | 16 + .../kotlin/examples/example-iterable-11.kt | 13 + .../kotlin/examples/example-iterable-12.kt | 14 + .../kotlin/examples/example-iterable-13.kt | 12 + .../kotlin/examples/example-iterable-14.kt | 13 + .../kotlin/examples/example-iterable-15.kt | 12 + .../kotlin/examples/example-iterable-16.kt | 14 + .../jvmTest/kotlin/examples/example-map-01.kt | 12 + .../jvmTest/kotlin/examples/example-map-02.kt | 14 + .../jvmTest/kotlin/examples/example-map-03.kt | 12 + .../jvmTest/kotlin/examples/example-map-04.kt | 14 + .../jvmTest/kotlin/examples/example-map-05.kt | 16 + .../jvmTest/kotlin/examples/example-map-06.kt | 13 + .../jvmTest/kotlin/examples/example-map-07.kt | 12 + .../jvmTest/kotlin/examples/example-map-08.kt | 16 + .../kotlin/examples/example-memoization-01.kt | 19 + .../examples/example-nonemptylist-01.kt | 11 + .../examples/example-nonemptylist-02.kt | 10 + .../examples/example-nonemptylist-03.kt | 12 + .../examples/example-nonemptylist-04.kt | 10 + .../examples/example-nonemptylist-05.kt | 17 + .../examples/example-nonemptylist-06.kt | 21 + .../kotlin/examples/example-nonfatal-01.kt | 28 + .../examples/example-nonfatalorthrow-01.kt | 24 + .../example-nullable-computations-01.kt | 16 + .../example-nullable-computations-02.kt | 17 + .../kotlin/examples/example-option-01.kt | 13 + .../kotlin/examples/example-option-02.kt | 9 + .../kotlin/examples/example-option-03.kt | 17 + .../kotlin/examples/example-option-04.kt | 17 + .../kotlin/examples/example-option-05.kt | 17 + .../kotlin/examples/example-option-06.kt | 10 + .../kotlin/examples/example-option-07.kt | 15 + .../kotlin/examples/example-option-08.kt | 15 + .../kotlin/examples/example-option-09.kt | 17 + .../kotlin/examples/example-option-10.kt | 11 + .../kotlin/examples/example-option-11.kt | 11 + .../kotlin/examples/example-option-12.kt | 12 + .../kotlin/examples/example-option-13.kt | 14 + .../kotlin/examples/example-option-14.kt | 16 + .../kotlin/examples/example-option-15.kt | 12 + .../kotlin/examples/example-option-16.kt | 10 + .../kotlin/examples/example-option-17.kt | 17 + .../kotlin/examples/example-option-18.kt | 18 + .../kotlin/examples/example-option-19.kt | 10 + .../kotlin/examples/example-option-20.kt | 10 + .../kotlin/examples/example-option-21.kt | 14 + .../kotlin/examples/example-option-22.kt | 14 + .../kotlin/examples/example-option-23.kt | 3 + .../example-option-computations-01.kt | 16 + .../example-option-computations-02.kt | 17 + .../example-result-computations-01.kt | 23 + .../kotlin/examples/example-semiring-01.kt | 12 + .../kotlin/examples/example-semiring-02.kt | 12 + .../kotlin/examples/example-semiring-03.kt | 14 + .../kotlin/examples/example-semiring-04.kt | 14 + .../kotlin/examples/example-sequence-01.kt | 14 + .../kotlin/examples/example-sequence-02.kt | 12 + .../kotlin/examples/example-sequence-03.kt | 14 + .../kotlin/examples/example-sequence-04.kt | 13 + .../kotlin/examples/example-sequence-05.kt | 14 + .../kotlin/examples/example-sequence-06.kt | 14 + .../kotlin/examples/example-sequence-07.kt | 14 + .../kotlin/examples/example-sequence-08.kt | 14 + .../kotlin/examples/example-sequence-09.kt | 14 + .../kotlin/examples/example-sequence-10.kt | 14 + .../kotlin/examples/example-sequence-11.kt | 11 + .../kotlin/examples/example-sequence-12.kt | 13 + .../kotlin/examples/example-sequence-13.kt | 12 + .../kotlin/examples/example-sequence-14.kt | 11 + .../kotlin/examples/example-sequence-15.kt | 11 + .../kotlin/examples/example-sequence-16.kt | 16 + .../kotlin/examples/example-validated-01.kt | 14 + .../kotlin/examples/example-validated-02.kt | 12 + .../kotlin/examples/example-validated-03.kt | 9 + .../kotlin/examples/example-validated-04.kt | 9 + .../kotlin/examples/example-validated-05.kt | 13 + .../kotlin/examples/example-validated-06.kt | 11 + 179 files changed, 3320 insertions(+), 21 deletions(-) create mode 100644 arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-arrow-core-either-contains-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-03.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-05.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-06.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-07.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-08.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-09.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-10.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-11.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-12.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-13.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-03.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-04.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-05.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-06.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-07.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-08.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-09.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-10.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-11.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-12.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-13.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-14.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-15.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-16.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-17.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-18.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-19.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-20.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-21.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-22.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-23.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-24.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-25.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-26.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-27.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-28.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-29.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-30.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-31.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-32.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-33.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-34.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-35.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-36.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-37.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-38.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-39.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-40.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-41.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-42.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-43.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-44.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-45.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-46.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-47.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-48.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-49.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-50.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-51.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-52.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-53.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-03.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-04.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-03.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-04.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-05.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-06.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-07.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-08.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-09.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-10.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-11.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-12.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-13.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-14.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-03.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-04.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-05.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-06.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-07.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-08.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-09.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-10.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-11.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-12.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-13.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-14.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-15.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-16.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-03.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-04.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-05.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-06.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-07.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-08.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-memoization-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-03.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-04.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-05.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-06.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatal-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatalorthrow-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-03.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-04.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-05.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-06.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-07.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-08.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-09.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-10.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-11.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-12.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-13.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-14.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-15.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-16.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-17.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-18.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-19.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-20.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-21.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-22.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-23.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-result-computations-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-03.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-04.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-03.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-04.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-05.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-06.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-07.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-08.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-09.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-10.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-11.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-12.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-13.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-14.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-15.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-16.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-03.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-04.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-05.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-06.kt diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt index bce7f16bc3e..cb80b32a1b0 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt @@ -19,14 +19,6 @@ import kotlin.jvm.JvmInline import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName -public typealias EagerEffect = Shift.() -> A - -public typealias Effect = suspend Shift.() -> A - -public inline fun eagerEffect(@BuilderInference noinline block: Shift.() -> A): EagerEffect = block - -public inline fun effect(@BuilderInference noinline block: suspend Shift.() -> A): Effect = block - public inline fun either(@BuilderInference block: Shift.() -> A): Either = fold({ block.invoke(this) }, { Either.Left(it) }, { Either.Right(it) }) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt new file mode 100644 index 00000000000..8864bd00f18 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt @@ -0,0 +1,674 @@ +@file:JvmMultifileClass +@file:OptIn(ExperimentalTypeInference::class) + +package arrow.core.continuations + +import kotlin.experimental.ExperimentalTypeInference +import kotlin.jvm.JvmMultifileClass + +/** + * [Effect] represents a function of `suspend Shift.() -> A`, that short-circuit with a value of `R` or `Throwable`, or completes with a value of `A`. + * + * So [Effect] is defined by `suspend fun fold(recover: suspend (Throwable) -> B, resolve: suspend (R) -> B, transform: suspend (A) -> B): B`, + * to map all values of `R`, `Throwable` and `A` to a value of `B`. + * + * + + * [Writing a program with Effect](#writing-a-program-with-effect) + * [Handling errors](#handling-errors) + * [catch](#catch) + * [attempt](#attempt) + * [Structured Concurrency](#structured-concurrency) + * [Arrow Fx Coroutines](#arrow-fx-coroutines) + * [parZip](#parzip) + * [parTraverse](#partraverse) + * [raceN](#racen) + * [bracketCase / Resource](#bracketcase--resource) + * [KotlinX](#kotlinx) + * [withContext](#withcontext) + * [async](#async) + * [launch](#launch) + * [Strange edge cases](#strange-edge-cases) + + * + * + * ## Writing a program with Effect + * + * Let's write a small program to read a file from disk, and instead of having the program work exception based we want to + * turn it into a polymorphic type-safe program. + * + * We'll start by defining a small function that accepts a [String], and does some simply validation to check that the path + * is not empty. If the path is empty, we want to program to result in `EmptyPath`. So we're immediately going to see how + * we can raise an error of any arbitrary type `R` by using the function `shift`. The name `shift` comes shifting (or + * changing, especially unexpectedly), away from the computation and finishing the `Continuation` with `R`. + * + * + * ```kotlin + * object EmptyPath + * + * fun readFile(path: String): Effect = effect { + * if (path.isEmpty()) shift(EmptyPath) else Unit + * } + * ``` + * + * Here we see how we can define an `Effect` which has `EmptyPath` for the shift type `R`, and `Unit` for the success type `A`. + * + * Patterns like validating a [Boolean] is very common, and the [Effect] DSL offers utility functions like [kotlin.require] + * and [kotlin.requireNotNull]. They're named [ensure] and [ensureNotNull] to avoid conflicts with the `kotlin` namespace. + * So let's rewrite the function from above to use the DSL instead. + * + * ```kotlin + * fun readFile2(path: String?): Effect = effect { + * ensureNotNull(path) { EmptyPath } + * ensure(path.isNotEmpty()) { EmptyPath } + * } + * ``` + * + * + * Now that we have the path, we can read from the `File` and return it as a domain model `Content`. + * We also want to take a look at what exceptions reading from a file might occur `FileNotFoundException` & `SecurityError`, + * so lets make some domain errors for those too. Grouping them as a sealed interface is useful since that way we can resolve *all* errors in a type safe manner. + * + * + * ```kotlin + * @JvmInline + * value class Content(val body: List) + * + * sealed interface FileError + * @JvmInline value class SecurityError(val msg: String?) : FileError + * @JvmInline value class FileNotFound(val path: String) : FileError + * object EmptyPath : FileError { + * override fun toString() = "EmptyPath" + * } + * ``` + * + * We can finish our function, but we need to refactor the return type from `Unit` to `Content` and the error type from `EmptyPath` to `FileError`. + * + * ```kotlin + * fun readFile(path: String?): Effect = effect { + * ensureNotNull(path) { EmptyPath } + * ensure(path.isNotEmpty()) { EmptyPath } + * try { + * val lines = File(path).readLines() + * Content(lines) + * } catch (e: FileNotFoundException) { + * shift(FileNotFound(path)) + * } catch (e: SecurityException) { + * shift(SecurityError(e.message)) + * } + * } + * ``` + * + * The `readFile` function defines a `suspend fun` that will return: + * + * - the `Content` of a given `path` + * - a `FileError` + * - An unexpected fatal error (`OutOfMemoryException`) + * + * Since these are the properties of our `Effect` function, we can turn it into a value. + * + * ```kotlin + * suspend fun main() { + * readFile("").toEither() shouldBe Either.Left(EmptyPath) + * readFile("knit.properties").toValidated() shouldBe Validated.Invalid(FileNotFound("knit.properties")) + * readFile("gradle.properties").toIor() shouldBe Ior.Left(FileNotFound("gradle.properties")) + * readFile("README.MD").toOption { None } shouldBe None + * + * readFile("build.gradle.kts").fold({ _: FileError -> null }, { it }) + * .shouldBeInstanceOf() + * .body.shouldNotBeEmpty() + * } + * ``` + * + * + * The functions above are available out of the box, but it's easy to define your own extension functions in terms + * of `fold`. Implementing the `toEither()` operator is as simple as: + * + * + * ```kotlin + * suspend fun Effect.toEither(): Either = + * fold({ Either.Left(it) }) { Either.Right(it) } + * + * suspend fun Effect.toOption(): Option = + * fold(::identity) { Some(it) } + * ``` + * + * + * Adding your own syntax to `Shift` is not advised, yet, but will be easy once "Multiple Receivers" become available. + * + * ``` + * context(Shift) + * suspend fun Either.bind(): A = + * when (this) { + * is Either.Left -> shift(value) + * is Either.Right -> value + * } + * + * context(Shift) + * fun Option.bind(): A = + * fold({ shift(it) }, ::identity) + * ``` + * + * ## Handling errors + * + * An Effect has 2 error channels: `Throwable` and `R` + * There are two separate handlers to transform either of the error channels. + * + * - `catch` to handle, and transform any error of type `R`. + * - `attempt` to handle, and transform and error of type `Throwable`. + * + * ### catch + * + * `catch` handles the error of type `R`, + * by providing a new value of type `A`, raising a different error of type `E`, or throwing an exception. + * + * Let's take a look at some examples: + * + * We define a `val failed` of type `Effect`, that represents a failed effect with value "failed". + * + * + * ```kotlin + * val failed: Effect = + * effect { shift("failed") } + * ``` + * + * We can `catch` the failure, and resolve it by providing a default value of `-1` or the length of the `error: String`. + * + * ```kotlin + * val default: Effect = + * failed.catch { -1 } + * + * val resolved: Effect = + * failed.catch { it.length } + * ``` + * + * As you can see the resulting `error` is now of type `Nothing`, since we did not raise any new errors. + * So our `Effect` knows that no short-circuiting will occur during execution. Awesome! + * But it can also infer to any other error type that you might want instead, because it's never going to occur. + * So as you see below, we can even assign our `Effect` to `Effect`, where `E` can be any type. + * + * ```kotlin + * val default2: Effect = default + * val resolved2: Effect = resolved + * ``` + * + * `catch` also allows us to _change_ the error type when we resolve the error of type `R`. + * Below we handle our error of `String` and turn it into `List` using `reversed().toList()`. + * This is a powerful operation, since it allows us to transform our error types across boundaries or layers. + * + * ```kotlin + * val newError: Effect, Int> = + * failed.catch { str -> + * shift(str.reversed().toList()) + * } + * ``` + * + * Finally, since `catch` supports `suspend` we can safely call other `suspend` code and throw `Throwable` into the `suspend` system. + * This is typically undesired, since you should prefer lifting `Throwable` into typed values of `R` to make them compile-time tracked. + * + * ```kotlin + * val newException: Effect = + * failed.catch { str -> throw RuntimeException(str) } + * ``` + * + * ### attempt + * + * `attempt` gives us the same powers as `catch`, but instead of resolving `R` we're recovering from any unexpected `Throwable`. + * Unexpected, because the expectation is that all `Throwable` get turned into `R` unless it's a fatal/unexpected. + * This operator is useful when you need to work/wrap foreign code, especially Java SDKs or any code that is heavily based on exceptions. + * + * Below we've defined a `foreign` value that represents wrapping a foreign API which might throw `RuntimeException`. + * + * ```kotlin + * val foreign = effect { + * throw RuntimeException("BOOM!") + * } + * ``` + * + * We can `attempt` to run the effect recovering from any exception, + * and recover it by providing a default value of `-1` or the length of the [Throwable.message]. + * + * ```kotlin + * val default3: Effect = + * foreign.attempt { -1 } + * + * val resolved3: Effect = + * foreign.attempt { it.message?.length ?: -1 } + * ``` + * + * A big difference with `catch` is that `attempt` **cannot** change the error type of `R` because it doesn't resolve it, so it stays unchanged. + * You can however compose `catch`, and `attempt` to resolve the error type **and** recover the exception. + * + * ```kotlin + * val default4: Effect = + * foreign + * .catch { -1 } + * .attempt { -2 } + * ``` + * + * `attempt` however offers an overload that can _refine the exception_. + * Let's say you're wrapping some database interactions that might throw `java.sql.SqlException`, or `org.postgresql.util.PSQLException`, + * then you might only be interested in those exceptions and not `Throwable`. `attempt` allows you to install multiple handlers for specific exceptions. + * If the desired exception is not matched, then it stays in the `suspend` exception channel and will be thrown or recovered at a later point. + * + * ```kotlin + * val default5: Effect = + * foreign + * .attempt { ex: RuntimeException -> -1 } + * .attempt { ex: java.sql.SQLException -> -2 } + * ``` + * + * Finally, since `attempt` also supports `suspend` we can safely call other `suspend` code and throw `Throwable` into the `suspend` system. + * This can be useful if refinement of exceptions is not sufficient, for example in the case of `org.postgresql.util.PSQLException` you might want to + * check the `SQLState` to check for a `foreign key violation` and rethrow the exception if not matched. + * + * ```kotlin + * suspend fun java.sql.SQLException.isForeignKeyViolation(): Boolean = true + * + * val rethrown: Effect = + * failed.attempt { ex: java.sql.SQLException -> + * if(ex.isForeignKeyViolation()) shift("foreign key violation") + * else throw ex + * } + * ``` + * + * + * + * Note: + * Handling errors can also be done with `try/catch` but this is **not recommended**, it uses `CancellationException` which is used to cancel `Coroutine`s and is advised not to capture in Kotlin. + * The `CancellationException` from `Effect` is `ShiftCancellationException`, this a public type, thus can be distinguished from any other `CancellationException` if necessary. + * + * ## Structured Concurrency + * + * `Effect` relies on `kotlin.cancellation.CancellationException` to `shift` error values of type `R` inside the `Continuation` since it effectively cancels/short-circuits it. + * For this reason `shift` adheres to the same rules as [`Structured Concurrency`](https://kotlinlang.org/docs/coroutines-basics.html#structured-concurrency) + * + * Let's overview below how `shift` behaves with the different concurrency builders from Arrow Fx & KotlinX Coroutines. + * In the examples below we're going to be using a utility to show how _sibling tasks_ get cancelled. + * The utility function show below called `awaitExitCase` will `never` finish suspending, and completes a `Deferred` with the `ExitCase`. + * `ExitCase` is a sealed class that can be a value of `Failure(Throwable)`, `Cancelled(CancellationException)`, or `Completed`. + * Since `awaitExitCase` suspends forever, it can only result in `Cancelled(CancellationException)`. + * + * + * ```kotlin + * suspend fun awaitExitCase(exit: CompletableDeferred): A = + * guaranteeCase(::awaitCancellation) { exitCase -> exit.complete(exitCase) } + * + * ``` + * + * ### Arrow Fx Coroutines + * All operators in Arrow Fx Coroutines run in place, so they have no way of leaking `shift`. + * It's there always safe to compose `effect` with any Arrow Fx combinator. Let's see some small examples below. + * + * #### parZip + * + * ```kotlin + * suspend fun main() { + * val error = "Error" + * val exit = CompletableDeferred() + * effect { + * parZip({ awaitExitCase(exit) }, { shift(error) }) { a, b -> a + b } + * }.fold({ it shouldBe error }, { fail("Int can never be the result") }) + * exit.await().shouldBeTypeOf() + * } + * ``` + * + * + * #### parTraverse + * + * ```kotlin + * suspend fun main() { + * val error = "Error" + * val exits = (0..3).map { CompletableDeferred() } + * effect> { + * (0..4).parTraverse { index -> + * if (index == 4) shift(error) + * else awaitExitCase(exits[index]) + * } + * }.fold({ msg -> msg shouldBe error }, { fail("Int can never be the result") }) + * // It's possible not all parallel task got launched, and in those cases awaitCancellation never ran + * exits.forEach { exit -> exit.getOrNull()?.shouldBeTypeOf() } + * } + * ``` + * + * + * `parTraverse` will launch 5 tasks, for every element in `1..5`. + * The last task to get scheduled will `shift` with "error", and it will cancel the other launched tasks before returning. + * + * #### raceN + * + * ```kotlin + * suspend fun main() { + * val error = "Error" + * val exit = CompletableDeferred() + * effect { + * raceN({ awaitExitCase(exit) }) { shift(error) } + * .merge() // Flatten Either result from race into Int + * }.fold({ msg -> msg shouldBe error }, { fail("Int can never be the result") }) + * // It's possible not all parallel task got launched, and in those cases awaitCancellation never ran + * exit.getOrNull()?.shouldBeTypeOf() + * } + * ``` + * + * + * `raceN` races `n` suspend functions in parallel, and cancels all participating functions when a winner is found. + * We can consider the function that `shift`s the winner of the race, except with a shifted value instead of a successful one. + * So when a function in the race `shift`s, and thus short-circuiting the race, it will cancel all the participating functions. + * + * #### bracketCase / Resource + * + * ```kotlin + * suspend fun main() { + * val error = "Error" + * val exit = CompletableDeferred() + * effect { + * bracketCase( + * acquire = { File("build.gradle.kts").bufferedReader() }, + * use = { reader: BufferedReader -> shift(error) }, + * release = { reader, exitCase -> + * reader.close() + * exit.complete(exitCase) + * } + * ) + * }.fold({ it shouldBe error }, { fail("Int can never be the result") }) + * exit.await().shouldBeTypeOf() + * } + * ``` + * + * + * + * ```kotlin + * suspend fun main() { + * val error = "Error" + * val exit = CompletableDeferred() + * + * fun bufferedReader(path: String): Resource = + * Resource.fromAutoCloseable { File(path).bufferedReader() } + * .releaseCase { _, exitCase -> exit.complete(exitCase) } + * + * effect { + * val lineCount = bufferedReader("build.gradle.kts") + * .use { reader -> shift(error) } + * lineCount + * }.fold({ it shouldBe error }, { fail("Int can never be the result") }) + * exit.await().shouldBeTypeOf() + * } + * ``` + * + * + * ### KotlinX + * #### withContext + * It's always safe to call `shift` from `withContext` since it runs in place, so it has no way of leaking `shift`. + * When `shift` is called from within `withContext` it will cancel all `Job`s running inside the `CoroutineScope` of `withContext`. + * + * + * ```kotlin + * suspend fun main() { + * val exit = CompletableDeferred() + * effect { + * withContext(Dispatchers.IO) { + * val job = launch { awaitExitCase(exit) } + * val content = readFile("failure").bind() + * job.join() + * content.body.size + * } + * }.fold({ e -> e shouldBe FileNotFound("failure") }, { fail("Int can never be the result") }) + * exit.await().shouldBeInstanceOf() + * } + * ``` + * + * + * #### async + * + * When calling `shift` from `async` you should **always** call `await`, otherwise `shift` can leak out of its scope. + * + * + * ```kotlin + * suspend fun main() { + * val errorA = "ErrorA" + * val errorB = "ErrorB" + * coroutineScope { + * effect { + * val fa = async { shift(errorA) } + * val fb = async { shift(errorB) } + * fa.await() + fb.await() + * }.fold({ error -> error shouldBeIn listOf(errorA, errorB) }, { fail("Int can never be the result") }) + * } + * } + * ``` + * + * + * #### launch + * + * + * ```kotlin + * suspend fun main() { + * val errorA = "ErrorA" + * val errorB = "ErrorB" + * val int = 45 + * effect { + * coroutineScope { + * launch { shift(errorA) } + * launch { shift(errorB) } + * int + * } + * }.fold({ fail("Shift can never finish") }, { it shouldBe int }) + * } + * ``` + * + * + * #### Strange edge cases + * + * **NOTE** + * Capturing `shift` into a lambda, and leaking it outside of `Effect` to be invoked outside will yield unexpected results. + * Below we capture `shift` from inside the DSL, and then invoke it outside its context `Shift`. + * + * + * + * ```kotlin + * effect Unit> { + * suspend { shift("error") } + * }.fold({ }, { leakedShift -> leakedShift.invoke() }) + * ``` + * + * The same violation is possible in all DSLs in Kotlin, including Structured Concurrency. + * + * ```kotlin + * val leakedAsync = coroutineScope Deferred> { + * suspend { + * async { + * println("I am never going to run, until I get called invoked from outside") + * } + * } + * } + * + * leakedAsync.invoke().await() + * ``` + * + */ +public typealias Effect = suspend Shift.() -> A + +public inline fun effect(@BuilderInference noinline block: suspend Shift.() -> A): Effect = block + +/** The same behavior and API as [Effect] except without requiring _suspend_. */ +public typealias EagerEffect = Shift.() -> A + +public inline fun eagerEffect(@BuilderInference noinline block: Shift.() -> A): EagerEffect = block diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt index acb9f087fad..a0cf008e02d 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt @@ -44,7 +44,7 @@ public inline fun fold( recover: (shifted: R) -> B, transform: (value: A) -> B, ): B { - val shift = DefaultShift() + val shift = DefaultShift() return try { transform(program(shift)) } catch (e: ShiftCancellationException) { @@ -63,8 +63,6 @@ public class ShiftCancellationException( ) : CancellationException("Shifted Continuation") @PublishedApi -internal class DefaultShift : Shift { - @Suppress("UNCHECKED_CAST") - override fun shift(r: R): B = - throw ShiftCancellationException(r, this as DefaultShift) +internal class DefaultShift : Shift { + override fun shift(r: Any?): B = throw ShiftCancellationException(r, this) } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt index 31115512a00..b11d8fd9be7 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt @@ -4,9 +4,11 @@ package arrow.core.continuations import arrow.core.Either +import arrow.core.Ior import arrow.core.None import arrow.core.Option import arrow.core.Some +import arrow.core.Validated import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName @@ -14,6 +16,18 @@ public suspend fun Effect.toEither(): Either = either { invok public fun EagerEffect.toEither(): Either = either { invoke() } +public suspend fun Effect.toValidated(): Validated = + fold({ Validated.Invalid(it) }) { Validated.Valid(it) } + +public fun EagerEffect.toValidated(): Validated = + fold({ Validated.Invalid(it) }) { Validated.Valid(it) } + +public suspend fun Effect.toIor(): Ior = + fold({ Ior.Left(it) }) { Ior.Right(it) } + +public fun EagerEffect.toIor(): Ior = + fold({ Ior.Left(it) }) { Ior.Right(it) } + public suspend fun Effect.orNull(): A? = fold({ _: E -> null }) { it } public fun EagerEffect.orNull(): A? = fold({ _: E -> null }) { it } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt index 00df32800d8..ee2bb6e2a39 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt @@ -6,6 +6,7 @@ import arrow.core.Either import arrow.core.None import arrow.core.Option import arrow.core.Some +import arrow.core.Validated import arrow.core.identity import kotlin.contracts.ExperimentalContracts import kotlin.contracts.contract @@ -16,28 +17,33 @@ import kotlin.jvm.JvmName public annotation class EffectDSL public interface Shift { - public fun shift(r: R): B + public fun shift(r: R): A - public fun EagerEffect.bind(): B = invoke(this@Shift) - public operator fun EagerEffect.invoke(): B = invoke(this@Shift) + public fun EagerEffect.bind(): A = invoke(this@Shift) + public operator fun EagerEffect.invoke(): A = invoke(this@Shift) - public suspend fun Effect.bind(): B = invoke(this@Shift) - public suspend operator fun Effect.invoke(): B = invoke(this@Shift) + public suspend fun Effect.bind(): A = invoke(this@Shift) + public suspend operator fun Effect.invoke(): A = invoke(this@Shift) - public fun Either.bind(): B = when (this) { + public fun Either.bind(): A = when (this) { is Either.Left -> shift(value) is Either.Right -> value } + public fun Validated.bind(): A = when (this) { + is Validated.Invalid -> shift(value) + is Validated.Valid -> value + } + // TODO can be inlined with context receivers, and top-level - public fun Option.bind(transform: Shift.(None) -> B): B = + public fun Option.bind(transform: Shift.(None) -> A): A = when (this) { None -> transform(None) is Some -> value } // TODO can be inlined with context receivers, and top-level - public fun Result.bind(transform: (Throwable) -> R): B = + public fun Result.bind(transform: (Throwable) -> R): A = fold(::identity) { throwable -> shift(transform(throwable)) } @EffectDSL diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-arrow-core-either-contains-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-arrow-core-either-contains-01.kt new file mode 100644 index 00000000000..521fb6e272d --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-arrow-core-either-contains-01.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleArrowCoreEitherContains01 + +import arrow.core.Either.Right +import arrow.core.Either.Left +import arrow.core.contains + +fun main() { + Right("something").contains("something") // Result: true + Right("something").contains("anything") // Result: false + Left("something").contains("something") // Result: false +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-01.kt new file mode 100644 index 00000000000..7653712b8f4 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-01.kt @@ -0,0 +1,18 @@ +// This file was automatically generated from Effect.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffect01 + +import arrow.core.continuations.Effect +import arrow.core.continuations.effect +import arrow.core.continuations.ensureNotNull +import arrow.core.continuations.ensure + +object EmptyPath + +fun readFile(path: String): Effect = effect { + if (path.isEmpty()) shift(EmptyPath) else Unit +} + +fun readFile2(path: String?): Effect = effect { + ensureNotNull(path) { EmptyPath } + ensure(path.isNotEmpty()) { EmptyPath } +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-02.kt new file mode 100644 index 00000000000..1e323abc263 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-02.kt @@ -0,0 +1,55 @@ +// This file was automatically generated from Effect.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffect02 + +import arrow.core.Either +import arrow.core.Ior +import arrow.core.None +import arrow.core.Validated +import arrow.core.continuations.Effect +import arrow.core.continuations.effect +import arrow.core.continuations.fold +import arrow.core.continuations.toEither +import arrow.core.continuations.toValidated +import arrow.core.continuations.toIor +import arrow.core.continuations.toOption +import arrow.core.continuations.ensureNotNull +import arrow.core.continuations.ensure +import io.kotest.matchers.collections.shouldNotBeEmpty +import io.kotest.matchers.shouldBe +import io.kotest.matchers.types.shouldBeInstanceOf +import java.io.File +import java.io.FileNotFoundException + +@JvmInline +value class Content(val body: List) + +sealed interface FileError +@JvmInline value class SecurityError(val msg: String?) : FileError +@JvmInline value class FileNotFound(val path: String) : FileError +object EmptyPath : FileError { + override fun toString() = "EmptyPath" +} + +fun readFile(path: String?): Effect = effect { + ensureNotNull(path) { EmptyPath } + ensure(path.isNotEmpty()) { EmptyPath } + try { + val lines = File(path).readLines() + Content(lines) + } catch (e: FileNotFoundException) { + shift(FileNotFound(path)) + } catch (e: SecurityException) { + shift(SecurityError(e.message)) + } +} + +suspend fun main() { + readFile("").toEither() shouldBe Either.Left(EmptyPath) + readFile("knit.properties").toValidated() shouldBe Validated.Invalid(FileNotFound("knit.properties")) + readFile("gradle.properties").toIor() shouldBe Ior.Left(FileNotFound("gradle.properties")) + readFile("README.MD").toOption { None } shouldBe None + + readFile("build.gradle.kts").fold({ _: FileError -> null }, { it }) + .shouldBeInstanceOf() + .body.shouldNotBeEmpty() +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-03.kt new file mode 100644 index 00000000000..d4c272eb814 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-03.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from Effect.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffect03 + +import arrow.core.Either +import arrow.core.None +import arrow.core.Option +import arrow.core.Some +import arrow.core.continuations.Effect +import arrow.core.continuations.fold +import arrow.core.identity + +suspend fun Effect.toEither(): Either = + fold({ Either.Left(it) }) { Either.Right(it) } + +suspend fun Effect.toOption(): Option = + fold(::identity) { Some(it) } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt new file mode 100644 index 00000000000..00971be0e26 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt @@ -0,0 +1,58 @@ +// This file was automatically generated from Effect.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffect04 + +import arrow.core.Either +import arrow.core.continuations.Effect +import arrow.core.continuations.effect +import arrow.core.continuations.catch +import arrow.core.continuations.attempt +import arrow.core.continuations.toEither +import io.kotest.matchers.shouldBe + +val failed: Effect = + effect { shift("failed") } + +val default: Effect = + failed.catch { -1 } + +val resolved: Effect = + failed.catch { it.length } + +val default2: Effect = default +val resolved2: Effect = resolved + +val newError: Effect, Int> = + failed.catch { str -> + shift(str.reversed().toList()) + } + +val newException: Effect = + failed.catch { str -> throw RuntimeException(str) } + +val foreign = effect { + throw RuntimeException("BOOM!") +} + +val default3: Effect = + foreign.attempt { -1 } + +val resolved3: Effect = + foreign.attempt { it.message?.length ?: -1 } + +val default4: Effect = + foreign + .catch { -1 } + .attempt { -2 } + +val default5: Effect = + foreign + .attempt { ex: RuntimeException -> -1 } + .attempt { ex: java.sql.SQLException -> -2 } + +suspend fun java.sql.SQLException.isForeignKeyViolation(): Boolean = true + +val rethrown: Effect = + failed.attempt { ex: java.sql.SQLException -> + if(ex.isForeignKeyViolation()) shift("foreign key violation") + else throw ex + } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-05.kt new file mode 100644 index 00000000000..5701d7b3a08 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-05.kt @@ -0,0 +1,25 @@ +// This file was automatically generated from Effect.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffect05 + +import arrow.core.continuations.effect +import arrow.core.continuations.fold +import arrow.fx.coroutines.ExitCase +import arrow.fx.coroutines.guaranteeCase +import arrow.fx.coroutines.parZip +import io.kotest.assertions.fail +import io.kotest.matchers.shouldBe +import io.kotest.matchers.types.shouldBeTypeOf +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.awaitCancellation + +suspend fun awaitExitCase(exit: CompletableDeferred): A = + guaranteeCase(::awaitCancellation) { exitCase -> exit.complete(exitCase) } + + suspend fun main() { + val error = "Error" + val exit = CompletableDeferred() + effect { + parZip({ awaitExitCase(exit) }, { shift(error) }) { a, b -> a + b } + }.fold({ it shouldBe error }, { fail("Int can never be the result") }) + exit.await().shouldBeTypeOf() +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-06.kt new file mode 100644 index 00000000000..29a0a136457 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-06.kt @@ -0,0 +1,32 @@ +// This file was automatically generated from Effect.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffect06 + +import arrow.core.continuations.effect +import arrow.core.continuations.fold +import arrow.fx.coroutines.ExitCase +import arrow.fx.coroutines.guaranteeCase +import arrow.fx.coroutines.parTraverse +import io.kotest.assertions.fail +import io.kotest.matchers.shouldBe +import io.kotest.matchers.types.shouldBeTypeOf +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.awaitCancellation + +suspend fun awaitExitCase(exit: CompletableDeferred): A = + guaranteeCase(::awaitCancellation) { exitCase -> exit.complete(exitCase) } + +suspend fun CompletableDeferred.getOrNull(): A? = + if (isCompleted) await() else null + +suspend fun main() { + val error = "Error" + val exits = (0..3).map { CompletableDeferred() } + effect> { + (0..4).parTraverse { index -> + if (index == 4) shift(error) + else awaitExitCase(exits[index]) + } + }.fold({ msg -> msg shouldBe error }, { fail("Int can never be the result") }) + // It's possible not all parallel task got launched, and in those cases awaitCancellation never ran + exits.forEach { exit -> exit.getOrNull()?.shouldBeTypeOf() } +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-07.kt new file mode 100644 index 00000000000..f0b411a222e --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-07.kt @@ -0,0 +1,31 @@ +// This file was automatically generated from Effect.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffect07 + +import arrow.core.continuations.effect +import arrow.core.continuations.fold +import arrow.core.merge +import arrow.fx.coroutines.ExitCase +import arrow.fx.coroutines.guaranteeCase +import arrow.fx.coroutines.raceN +import io.kotest.assertions.fail +import io.kotest.matchers.shouldBe +import io.kotest.matchers.types.shouldBeTypeOf +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.awaitCancellation + +suspend fun awaitExitCase(exit: CompletableDeferred): A = + guaranteeCase(::awaitCancellation) { exitCase -> exit.complete(exitCase) } + +suspend fun CompletableDeferred.getOrNull(): A? = + if (isCompleted) await() else null + +suspend fun main() { + val error = "Error" + val exit = CompletableDeferred() + effect { + raceN({ awaitExitCase(exit) }) { shift(error) } + .merge() // Flatten Either result from race into Int + }.fold({ msg -> msg shouldBe error }, { fail("Int can never be the result") }) + // It's possible not all parallel task got launched, and in those cases awaitCancellation never ran + exit.getOrNull()?.shouldBeTypeOf() +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-08.kt new file mode 100644 index 00000000000..7fe68e33a6b --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-08.kt @@ -0,0 +1,29 @@ +// This file was automatically generated from Effect.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffect08 + +import arrow.core.continuations.effect +import arrow.core.continuations.fold +import arrow.fx.coroutines.ExitCase +import arrow.fx.coroutines.bracketCase +import io.kotest.assertions.fail +import io.kotest.matchers.shouldBe +import io.kotest.matchers.types.shouldBeTypeOf +import kotlinx.coroutines.CompletableDeferred +import java.io.BufferedReader +import java.io.File + +suspend fun main() { + val error = "Error" + val exit = CompletableDeferred() + effect { + bracketCase( + acquire = { File("build.gradle.kts").bufferedReader() }, + use = { reader: BufferedReader -> shift(error) }, + release = { reader, exitCase -> + reader.close() + exit.complete(exitCase) + } + ) + }.fold({ it shouldBe error }, { fail("Int can never be the result") }) + exit.await().shouldBeTypeOf() +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-09.kt new file mode 100644 index 00000000000..63ad21f21d6 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-09.kt @@ -0,0 +1,31 @@ +// This file was automatically generated from Effect.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffect09 + +import arrow.core.continuations.effect +import arrow.core.continuations.fold +import arrow.fx.coroutines.ExitCase +import arrow.fx.coroutines.Resource +import arrow.fx.coroutines.fromAutoCloseable +import arrow.fx.coroutines.releaseCase +import io.kotest.assertions.fail +import io.kotest.matchers.shouldBe +import io.kotest.matchers.types.shouldBeTypeOf +import kotlinx.coroutines.CompletableDeferred +import java.io.BufferedReader +import java.io.File + +suspend fun main() { + val error = "Error" + val exit = CompletableDeferred() + + fun bufferedReader(path: String): Resource = + Resource.fromAutoCloseable { File(path).bufferedReader() } + .releaseCase { _, exitCase -> exit.complete(exitCase) } + + effect { + val lineCount = bufferedReader("build.gradle.kts") + .use { reader -> shift(error) } + lineCount + }.fold({ it shouldBe error }, { fail("Int can never be the result") }) + exit.await().shouldBeTypeOf() +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-10.kt new file mode 100644 index 00000000000..3e1eb29a8ae --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-10.kt @@ -0,0 +1,59 @@ +// This file was automatically generated from Effect.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffect10 + +import arrow.core.continuations.Effect +import arrow.core.continuations.effect +import arrow.core.continuations.fold +import arrow.core.continuations.ensureNotNull +import arrow.core.continuations.ensure +import arrow.fx.coroutines.ExitCase +import arrow.fx.coroutines.guaranteeCase +import io.kotest.assertions.fail +import io.kotest.matchers.shouldBe +import io.kotest.matchers.types.shouldBeInstanceOf +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import java.io.File +import java.io.FileNotFoundException + +@JvmInline +value class Content(val body: List) + +sealed interface FileError +@JvmInline value class SecurityError(val msg: String?) : FileError +@JvmInline value class FileNotFound(val path: String) : FileError +object EmptyPath : FileError { + override fun toString() = "EmptyPath" +} + +fun readFile(path: String?): Effect = effect { + ensureNotNull(path) { EmptyPath } + ensure(path.isNotEmpty()) { EmptyPath } + try { + val lines = File(path).readLines() + Content(lines) + } catch (e: FileNotFoundException) { + shift(FileNotFound(path)) + } catch (e: SecurityException) { + shift(SecurityError(e.message)) + } +} + +suspend fun awaitExitCase(exit: CompletableDeferred): A = + guaranteeCase(::awaitCancellation) { exitCase -> exit.complete(exitCase) } + +suspend fun main() { + val exit = CompletableDeferred() + effect { + withContext(Dispatchers.IO) { + val job = launch { awaitExitCase(exit) } + val content = readFile("failure").bind() + job.join() + content.body.size + } + }.fold({ e -> e shouldBe FileNotFound("failure") }, { fail("Int can never be the result") }) + exit.await().shouldBeInstanceOf() +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-11.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-11.kt new file mode 100644 index 00000000000..98d14585a5b --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-11.kt @@ -0,0 +1,21 @@ +// This file was automatically generated from Effect.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffect11 + +import arrow.core.continuations.effect +import arrow.core.continuations.fold +import io.kotest.assertions.fail +import io.kotest.matchers.collections.shouldBeIn +import kotlinx.coroutines.async +import kotlinx.coroutines.coroutineScope + +suspend fun main() { + val errorA = "ErrorA" + val errorB = "ErrorB" + coroutineScope { + effect { + val fa = async { shift(errorA) } + val fb = async { shift(errorB) } + fa.await() + fb.await() + }.fold({ error -> error shouldBeIn listOf(errorA, errorB) }, { fail("Int can never be the result") }) + } +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-12.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-12.kt new file mode 100644 index 00000000000..8b4105198ab --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-12.kt @@ -0,0 +1,22 @@ +// This file was automatically generated from Effect.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffect12 + +import arrow.core.continuations.effect +import arrow.core.continuations.fold +import io.kotest.assertions.fail +import io.kotest.matchers.shouldBe +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.launch + +suspend fun main() { + val errorA = "ErrorA" + val errorB = "ErrorB" + val int = 45 + effect { + coroutineScope { + launch { shift(errorA) } + launch { shift(errorB) } + int + } + }.fold({ fail("Shift can never finish") }, { it shouldBe int }) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-13.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-13.kt new file mode 100644 index 00000000000..74de4ee270e --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-13.kt @@ -0,0 +1,25 @@ +// This file was automatically generated from Effect.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffect13 + +import arrow.core.continuations.effect +import arrow.core.continuations.fold +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.async +import kotlinx.coroutines.coroutineScope + +suspend fun main() { + + effect Unit> { + suspend { shift("error") } + }.fold({ }, { leakedShift -> leakedShift.invoke() }) + + val leakedAsync = coroutineScope Deferred> { + suspend { + async { + println("I am never going to run, until I get called invoked from outside") + } + } + } + + leakedAsync.invoke().await() +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-01.kt new file mode 100644 index 00000000000..313a4846eda --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-01.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither01 + +import arrow.core.andThen + +val throwsSomeStuff: (Int) -> Double = {x -> x.toDouble()} +val throwsOtherThings: (Double) -> String = {x -> x.toString()} +val moreThrowing: (String) -> List = {x -> listOf(x)} +val magic = throwsSomeStuff.andThen(throwsOtherThings).andThen(moreThrowing) +fun main() { + println ("magic = $magic") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-02.kt new file mode 100644 index 00000000000..3050e4d988b --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-02.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither02 + +import arrow.core.Either + +val right: Either = + Either.Right(5) +fun main() { + println(right) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-03.kt new file mode 100644 index 00000000000..a30a54e9643 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-03.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither03 + +import arrow.core.Either + +val left: Either = + Either.Left("Something went wrong") +fun main() { + println(left) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-04.kt new file mode 100644 index 00000000000..86bf1302c74 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-04.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither04 + +import arrow.core.Either +import arrow.core.flatMap + +val right: Either = Either.Right(5) +val value = right.flatMap{ Either.Right(it + 1) } +fun main() { + println("value = $value") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-05.kt new file mode 100644 index 00000000000..28c39f3df3f --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-05.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither05 + +import arrow.core.Either +import arrow.core.flatMap + +val left: Either = Either.Left("Something went wrong") +val value = left.flatMap{ Either.Right(it + 1) } +fun main() { + println("value = $value") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-06.kt new file mode 100644 index 00000000000..67df2d4b8cb --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-06.kt @@ -0,0 +1,15 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither06 + +import arrow.core.Either +import arrow.core.flatMap + +fun parse(s: String): Int = + if (s.matches(Regex("-?[0-9]+"))) s.toInt() + else throw NumberFormatException("$s is not a valid integer.") + +fun reciprocal(i: Int): Double = + if (i == 0) throw IllegalArgumentException("Cannot take reciprocal of 0.") + else 1.0 / i + +fun stringify(d: Double): String = d.toString() diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-07.kt new file mode 100644 index 00000000000..6565442240c --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-07.kt @@ -0,0 +1,21 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither07 + +import arrow.core.Either +import arrow.core.flatMap +import arrow.core.left +import arrow.core.right + +// Either Style +fun parse(s: String): Either = + if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt()) + else Either.Left(NumberFormatException("$s is not a valid integer.")) + +fun reciprocal(i: Int): Either = + if (i == 0) Either.Left(IllegalArgumentException("Cannot take reciprocal of 0.")) + else Either.Right(1.0 / i) + +fun stringify(d: Double): String = d.toString() + +fun magic(s: String): Either = + parse(s).flatMap { reciprocal(it) }.map { stringify(it) } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-08.kt new file mode 100644 index 00000000000..e25b8aaf693 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-08.kt @@ -0,0 +1,15 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither08 + +import arrow.core.Either + +fun parse(s: String): Either = + if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt()) + else Either.Left(NumberFormatException("$s is not a valid integer.")) + +val notANumber = parse("Not a number") +val number2 = parse("2") +fun main() { + println("notANumber = $notANumber") + println("number2 = $number2") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-09.kt new file mode 100644 index 00000000000..6e01423972f --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-09.kt @@ -0,0 +1,27 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither09 + +import arrow.core.Either +import arrow.core.flatMap + +fun parse(s: String): Either = + if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt()) + else Either.Left(NumberFormatException("$s is not a valid integer.")) + +fun reciprocal(i: Int): Either = + if (i == 0) Either.Left(IllegalArgumentException("Cannot take reciprocal of 0.")) + else Either.Right(1.0 / i) + +fun stringify(d: Double): String = d.toString() + +fun magic(s: String): Either = + parse(s).flatMap{ reciprocal(it) }.map{ stringify(it) } + +val magic0 = magic("0") +val magic1 = magic("1") +val magicNotANumber = magic("Not a number") +fun main() { + println("magic0 = $magic0") + println("magic1 = $magic1") + println("magicNotANumber = $magicNotANumber") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-10.kt new file mode 100644 index 00000000000..84d23516808 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-10.kt @@ -0,0 +1,31 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither10 + +import arrow.core.Either +import arrow.core.flatMap + +fun parse(s: String): Either = + if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt()) + else Either.Left(NumberFormatException("$s is not a valid integer.")) + +fun reciprocal(i: Int): Either = + if (i == 0) Either.Left(IllegalArgumentException("Cannot take reciprocal of 0.")) + else Either.Right(1.0 / i) + +fun stringify(d: Double): String = d.toString() + +fun magic(s: String): Either = + parse(s).flatMap{ reciprocal(it) }.map{ stringify(it) } + +val x = magic("2") +val value = when(x) { + is Either.Left -> when (x.value) { + is NumberFormatException -> "Not a number!" + is IllegalArgumentException -> "Can't take reciprocal of 0!" + else -> "Unknown error" + } + is Either.Right -> "Got reciprocal: ${x.value}" +} +fun main() { + println("value = $value") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-11.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-11.kt new file mode 100644 index 00000000000..3b4172abc69 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-11.kt @@ -0,0 +1,24 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither11 + +import arrow.core.Either +import arrow.core.flatMap +// Either with ADT Style + +sealed class Error { + object NotANumber : Error() + object NoZeroReciprocal : Error() +} + +fun parse(s: String): Either = + if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt()) + else Either.Left(Error.NotANumber) + +fun reciprocal(i: Int): Either = + if (i == 0) Either.Left(Error.NoZeroReciprocal) + else Either.Right(1.0 / i) + +fun stringify(d: Double): String = d.toString() + +fun magic(s: String): Either = + parse(s).flatMap{reciprocal(it)}.map{ stringify(it) } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-12.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-12.kt new file mode 100644 index 00000000000..bca693c137c --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-12.kt @@ -0,0 +1,35 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither12 + +import arrow.core.Either +import arrow.core.flatMap + +sealed class Error { + object NotANumber : Error() + object NoZeroReciprocal : Error() +} + +fun parse(s: String): Either = + if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt()) + else Either.Left(Error.NotANumber) + +fun reciprocal(i: Int): Either = + if (i == 0) Either.Left(Error.NoZeroReciprocal) + else Either.Right(1.0 / i) + +fun stringify(d: Double): String = d.toString() + +fun magic(s: String): Either = + parse(s).flatMap{ reciprocal(it) }.map{ stringify(it) } + +val x = magic("2") +val value = when(x) { + is Either.Left -> when (x.value) { + is Error.NotANumber -> "Not a number!" + is Error.NoZeroReciprocal -> "Can't take reciprocal of 0!" + } + is Either.Right -> "Got reciprocal: ${x.value}" +} +fun main() { + println("value = $value") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-13.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-13.kt new file mode 100644 index 00000000000..868a711873d --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-13.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither13 + +import arrow.core.Either + +fun potentialThrowingCode(): String = throw RuntimeException("Blow up!") + +suspend fun makeSureYourLogicDoesNotHaveSideEffects(): Either = + Either.catch { potentialThrowingCode() }.mapLeft { Error.SpecificError } +suspend fun main() { + println("makeSureYourLogicDoesNotHaveSideEffects().isLeft() = ${makeSureYourLogicDoesNotHaveSideEffects().isLeft()}") +} + +sealed class Error { + object SpecificError : Error() +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-14.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-14.kt new file mode 100644 index 00000000000..e4aefdd6134 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-14.kt @@ -0,0 +1,79 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither14 + +import arrow.core.Either +import arrow.core.flatMap +import arrow.core.left +import arrow.core.right + +suspend fun httpEndpoint(request: String = "Hello?") = + Either.resolve( + f = { + if (request == "Hello?") "HELLO WORLD!".right() + else Error.SpecificError.left() + }, + success = { a -> handleSuccess({ a: Any -> log(Level.INFO, "This is a: $a") }, a) }, + error = { e -> handleError({ e: Any -> log(Level.WARN, "This is e: $e") }, e) }, + throwable = { throwable -> handleThrowable({ throwable: Throwable -> log(Level.ERROR, "Log the throwable: $throwable.") }, throwable) }, + unrecoverableState = { _ -> Unit.right() } + ) +suspend fun main() { + println("httpEndpoint().status = ${httpEndpoint().status}") +} + +@Suppress("UNUSED_PARAMETER") +suspend fun handleSuccess(log: suspend (a: A) -> Either, a: A): Either = + Either.catch { + Response.Builder(HttpStatus.OK) + .header(CONTENT_TYPE, CONTENT_TYPE_APPLICATION_JSON) + .body(a) + .build() + } + +@Suppress("UNUSED_PARAMETER") +suspend fun handleError(log: suspend (e: E) -> Either, e: E): Either = + createErrorResponse(HttpStatus.NOT_FOUND, ErrorResponse("$ERROR_MESSAGE_PREFIX $e")) + +suspend fun handleThrowable(log: suspend (throwable: Throwable) -> Either, throwable: Throwable): Either = + log(throwable) + .flatMap { createErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, ErrorResponse("$THROWABLE_MESSAGE_PREFIX $throwable")) } + +suspend fun createErrorResponse(httpStatus: HttpStatus, errorResponse: ErrorResponse): Either = + Either.catch { + Response.Builder(httpStatus) + .header(CONTENT_TYPE, CONTENT_TYPE_APPLICATION_JSON) + .body(errorResponse) + .build() + } + +suspend fun log(level: Level, message: String): Either = + Unit.right() // Should implement logging. + +enum class HttpStatus(val value: Int) { OK(200), NOT_FOUND(404), INTERNAL_SERVER_ERROR(500) } + +class Response private constructor( + val status: HttpStatus, + val headers: Map, + val body: Any? +) { + + data class Builder( + val status: HttpStatus, + var headers: Map = emptyMap(), + var body: Any? = null + ) { + fun header(key: String, value: String) = apply { this.headers = this.headers + mapOf(key to value) } + fun body(body: Any?) = apply { this.body = body } + fun build() = Response(status, headers, body) + } +} + +val CONTENT_TYPE = "Content-Type" +val CONTENT_TYPE_APPLICATION_JSON = "application/json" +val ERROR_MESSAGE_PREFIX = "An error has occurred. The error is:" +val THROWABLE_MESSAGE_PREFIX = "An exception was thrown. The exception is:" +sealed class Error { + object SpecificError : Error() +} +data class ErrorResponse(val errorMessage: String) +enum class Level { INFO, WARN, ERROR } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-15.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-15.kt new file mode 100644 index 00000000000..02ba7e88a61 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-15.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither15 + +import arrow.core.Either + +val r : Either = Either.Right(7) +val rightMapLeft = r.mapLeft {it + 1} +val l: Either = Either.Left(7) +val leftMapLeft = l.mapLeft {it + 1} +fun main() { + println("rightMapLeft = $rightMapLeft") + println("leftMapLeft = $leftMapLeft") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-16.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-16.kt new file mode 100644 index 00000000000..20f29c59fcf --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-16.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither16 + +import arrow.core.Either.Left +import arrow.core.Either + +val r: Either = Either.Right(7) +val swapped = r.swap() +fun main() { + println("swapped = $swapped") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-17.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-17.kt new file mode 100644 index 00000000000..4a9e9269241 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-17.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither17 + +import arrow.core.right + +val right7 = + 7.right() +fun main() { + println(right7) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-18.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-18.kt new file mode 100644 index 00000000000..61b4cfa3593 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-18.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither18 + +import arrow.core.left + + val leftHello = + "hello".left() +fun main() { + println(leftHello) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-19.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-19.kt new file mode 100644 index 00000000000..6d1ea1331b2 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-19.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither19 + +import arrow.core.right +import arrow.core.contains + +val x = 7.right() +val contains7 = x.contains(7) +fun main() { + println("contains7 = $contains7") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-20.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-20.kt new file mode 100644 index 00000000000..2dd4aae24ad --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-20.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither20 + +import arrow.core.left +import arrow.core.getOrElse + +val x = "hello".left() +val getOr7 = x.getOrElse { 7 } +fun main() { + println("getOr7 = $getOr7") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-21.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-21.kt new file mode 100644 index 00000000000..67e7eb953bf --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-21.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither21 + +import arrow.core.left +import arrow.core.getOrHandle + +val x = "hello".left() +val value = x.getOrHandle { "$it world!" } +fun main() { + println("value = $value") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-22.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-22.kt new file mode 100644 index 00000000000..f9cfa9ae33c --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-22.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither22 + +import arrow.core.Either + +val value = + Either.conditionally(true, { "Error" }, { 42 }) +fun main() { + println(value) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-23.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-23.kt new file mode 100644 index 00000000000..177ba8a52d7 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-23.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither23 + +import arrow.core.Either + +val value = + Either.conditionally(false, { "Error" }, { 42 }) +fun main() { + println(value) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-24.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-24.kt new file mode 100644 index 00000000000..540212da3d0 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-24.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither24 + +import arrow.core.Either +import arrow.core.right + +val x : Either = 7.right() +val fold = x.fold({ 1 }, { it + 3 }) +fun main() { + println("fold = $fold") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-25.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-25.kt new file mode 100644 index 00000000000..f212f13b69e --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-25.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither25 + +import arrow.core.Either +import arrow.core.left + +val y : Either = 7.left() +val fold = y.fold({ 1 }, { it + 3 }) +fun main() { + println("fold = $fold") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-26.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-26.kt new file mode 100644 index 00000000000..2efc6e305d4 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-26.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither26 + +import arrow.core.Either +import arrow.core.getOrHandle + +val r: Either = Either.Left(NumberFormatException()) +val httpStatusCode = r.getOrHandle { + when(it) { + is NumberFormatException -> 400 + else -> 500 + } +} +fun main() { + println("httpStatusCode = $httpStatusCode") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-27.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-27.kt new file mode 100644 index 00000000000..c446a01405f --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-27.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither27 + +import arrow.core.Either.Right +import arrow.core.leftIfNull + +fun main() { + val value = + //sampleStart + Right(12).leftIfNull({ -1 }) + //sampleEnd + println(value) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-28.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-28.kt new file mode 100644 index 00000000000..9ce8328c1a1 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-28.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither28 + +import arrow.core.Either.Right +import arrow.core.leftIfNull + +val value = + Right(null).leftIfNull({ -1 }) +fun main() { + println(value) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-29.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-29.kt new file mode 100644 index 00000000000..b260eb2681a --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-29.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither29 + +import arrow.core.Either.Left +import arrow.core.leftIfNull + +val value = + Left(12).leftIfNull({ -1 }) +fun main() { + println(value) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-30.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-30.kt new file mode 100644 index 00000000000..b68274ffcc6 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-30.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither30 + +import arrow.core.rightIfNotNull + +val value = + "value".rightIfNotNull { "left" } +fun main() { + println(value) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-31.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-31.kt new file mode 100644 index 00000000000..2a73a11a0fd --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-31.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither31 + +import arrow.core.rightIfNotNull + +val value = + null.rightIfNotNull { "left" } +fun main() { + println(value) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-32.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-32.kt new file mode 100644 index 00000000000..2fe7fe4f955 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-32.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither32 + +import arrow.core.rightIfNull + +val value = + "value".rightIfNull { "left" } +fun main() { + println(value) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-33.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-33.kt new file mode 100644 index 00000000000..bcf492a1a31 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-33.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither33 + +import arrow.core.rightIfNull + +val value = + null.rightIfNull { "left" } +fun main() { + println(value) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-34.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-34.kt new file mode 100644 index 00000000000..45f0b1be289 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-34.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither34 + +import arrow.core.* + +fun main() { + fun possiblyFailingOperation(): Either.Right = + Either.Right(1) + //sampleStart + val result: Either = possiblyFailingOperation() + result.fold( + { println("operation failed with $it") }, + { println("operation succeeded with $it") } + ) + //sampleEnd +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-35.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-35.kt new file mode 100644 index 00000000000..4829bba7255 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-35.kt @@ -0,0 +1,9 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither35 + +import arrow.core.* + +fun main() { + Either.Left("left").swap() // Result: Right("left") + Either.Right("right").swap() // Result: Left("right") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-36.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-36.kt new file mode 100644 index 00000000000..13703d2fd9c --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-36.kt @@ -0,0 +1,9 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither36 + +import arrow.core.* + +fun main() { + Either.Right(12).map { "flower" } // Result: Right("flower") + Either.Left(12).map { "flower" } // Result: Left(12) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-37.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-37.kt new file mode 100644 index 00000000000..70f3bbb16cd --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-37.kt @@ -0,0 +1,9 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither37 + +import arrow.core.* + +fun main() { + Either.Right(12).mapLeft { "flower" } // Result: Right(12) + Either.Left(12).mapLeft { "flower" } // Result: Left("flower") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-38.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-38.kt new file mode 100644 index 00000000000..9964e1011a6 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-38.kt @@ -0,0 +1,9 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither38 + +import arrow.core.* + +fun main() { + Either.Right(12).tapLeft { println("flower") } // Result: Right(12) + Either.Left(12).tapLeft { println("flower") } // Result: prints "flower" and returns: Left(12) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-39.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-39.kt new file mode 100644 index 00000000000..8c03f5ea143 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-39.kt @@ -0,0 +1,9 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither39 + + import arrow.core.* + +fun main() { + Either.Right(12).tap { println("flower") } // Result: prints "flower" and returns: Right(12) + Either.Left(12).tap { println("flower") } // Result: Left(12) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-40.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-40.kt new file mode 100644 index 00000000000..3ec2b39bfa5 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-40.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither40 + +import arrow.core.Either +import arrow.core.Either.Left + +fun main() { + Either.Right(12).exists { it > 10 } // Result: true + Either.Right(7).exists { it > 10 } // Result: false + + val left: Either = Left(12) + left.exists { it > 10 } // Result: false +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-41.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-41.kt new file mode 100644 index 00000000000..e8c4e6e06af --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-41.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither41 + +import arrow.core.Either.Left +import arrow.core.Either.Right + +val right = Right(12).orNull() // Result: 12 +val left = Left(12).orNull() // Result: null +fun main() { + println("right = $right") + println("left = $left") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-42.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-42.kt new file mode 100644 index 00000000000..cd9256c06b2 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-42.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither42 + +import arrow.core.* + + fun main(args: Array) { + //sampleStart + Either.Left("foo").isEmpty() // Result: true + Either.Right("foo").isEmpty() // Result: false +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-43.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-43.kt new file mode 100644 index 00000000000..e98eda5328a --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-43.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither43 + + import arrow.core.* + + fun main(args: Array) { + //sampleStart + Either.Left("foo").isNotEmpty() // Result: false + Either.Right("foo").isNotEmpty() // Result: true + //sampleEnd +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-44.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-44.kt new file mode 100644 index 00000000000..27ed105719f --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-44.kt @@ -0,0 +1,3 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither44 + diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-45.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-45.kt new file mode 100644 index 00000000000..df6411d8a14 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-45.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither45 + +import arrow.core.Either.Right +import arrow.core.Either.Left +import arrow.core.getOrElse + +fun main() { + Right(12).getOrElse { 17 } // Result: 12 + Left(12).getOrElse { 17 } // Result: 17 +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-46.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-46.kt new file mode 100644 index 00000000000..e0b22d93443 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-46.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither46 + +import arrow.core.Either.Right +import arrow.core.Either.Left + +fun main() { + Right(12).orNull() // Result: 12 + Left(12).orNull() // Result: null +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-47.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-47.kt new file mode 100644 index 00000000000..7e170618794 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-47.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither47 + +import arrow.core.Either.Right +import arrow.core.Either.Left +import arrow.core.getOrHandle + +fun main() { + Right(12).getOrHandle { 17 } // Result: 12 + Left(12).getOrHandle { it + 5 } // Result: 17 +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-48.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-48.kt new file mode 100644 index 00000000000..6d282c44373 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-48.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither48 + +import arrow.core.Either.* +import arrow.core.Either +import arrow.core.filterOrElse + +fun main() { + Right(12).filterOrElse({ it > 10 }, { -1 }) // Result: Right(12) + Right(7).filterOrElse({ it > 10 }, { -1 }) // Result: Left(-1) + + val left: Either = Left(12) + left.filterOrElse({ it > 10 }, { -1 }) // Result: Left(12) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-49.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-49.kt new file mode 100644 index 00000000000..2d14098d02d --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-49.kt @@ -0,0 +1,17 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither49 + +import arrow.core.* + +suspend fun main(): Unit { + //sampleStart + Either.Right(7).filterOrOther({ it == 10 }, { "Value '$it' is not equal to 10" }) + .let(::println) // Either.Left(Value '7' is not equal to 10") + + Either.Right(10).filterOrOther({ it == 10 }, { "Value '$it' is not equal to 10" }) + .let(::println) // Either.Right(10) + + Either.Left(12).filterOrOther({ str: String -> str.contains("impossible") }, { -1 }) + .let(::println) // Either.Left(12) + //sampleEnd +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-50.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-50.kt new file mode 100644 index 00000000000..458058b4dff --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-50.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither50 + +import arrow.core.Either.Left +import arrow.core.Either.Right +import arrow.core.merge + +fun main() { + Right(12).merge() // Result: 12 + Left(12).merge() // Result: 12 +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-51.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-51.kt new file mode 100644 index 00000000000..9826b45ba9a --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-51.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither51 + +import arrow.core.Either.* +import arrow.core.leftIfNull + +fun main() { + Right(12).leftIfNull({ -1 }) // Result: Right(12) + Right(null).leftIfNull({ -1 }) // Result: Left(-1) + + Left(12).leftIfNull({ -1 }) // Result: Left(12) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-52.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-52.kt new file mode 100644 index 00000000000..eaf931db471 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-52.kt @@ -0,0 +1,9 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither52 + +import arrow.core.rightIfNotNull + +fun main() { + "value".rightIfNotNull { "left" } // Right(b="value") + null.rightIfNotNull { "left" } // Left(a="left") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-53.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-53.kt new file mode 100644 index 00000000000..37b9c008d27 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-53.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither53 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val string: Either = "Hello".right() + val chars: Either = + string.widen() + //sampleEnd + println(chars) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-01.kt new file mode 100644 index 00000000000..e02ffc279db --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-01.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEitherComputations01 + +import arrow.core.computations.either + +suspend fun main() { + either { + ensure(true) { "" } + println("ensure(true) passes") + ensure(false) { "failed" } + 1 + } + .let(::println) +} +// println: "ensure(true) passes" +// res: Either.Left("failed") diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-02.kt new file mode 100644 index 00000000000..ba2f60c2023 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-computations-02.kt @@ -0,0 +1,17 @@ +// This file was automatically generated from either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEitherComputations02 + +import arrow.core.computations.either +import arrow.core.computations.ensureNotNull + +suspend fun main() { + either { + val x: Int? = 1 + ensureNotNull(x) { "passes" } + println(x) + ensureNotNull(null) { "failed" } + } + .let(::println) +} +// println: "1" +// res: Either.Left("failed") diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-01.kt new file mode 100644 index 00000000000..a66dcc08cd2 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-01.kt @@ -0,0 +1,21 @@ +// This file was automatically generated from Eval.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEval01 + +import arrow.core.Eval + +fun even(n: Int): Eval = + Eval.always { n == 0 }.flatMap { + if(it == true) Eval.now(true) + else odd(n - 1) + } + +fun odd(n: Int): Eval = + Eval.always { n == 0 }.flatMap { + if(it == true) Eval.now(false) + else even(n - 1) + } + +// if not wrapped in eval this type of computation would blow the stack and result in a StackOverflowError +fun main() { + println(odd(100000).value()) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-02.kt new file mode 100644 index 00000000000..0923e57eb8d --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-02.kt @@ -0,0 +1,9 @@ +// This file was automatically generated from Eval.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEval02 + +import arrow.core.* + +fun main() { + val eager = Eval.now(1).map { it + 1 } + println(eager.value()) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-03.kt new file mode 100644 index 00000000000..7fd34596244 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-03.kt @@ -0,0 +1,9 @@ +// This file was automatically generated from Eval.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEval03 + +import arrow.core.* + +fun main() { + val lazyEvaled = Eval.later { "expensive computation" } + println(lazyEvaled.value()) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-04.kt new file mode 100644 index 00000000000..4b38eb8331a --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-eval-04.kt @@ -0,0 +1,9 @@ +// This file was automatically generated from Eval.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEval04 + +import arrow.core.* + +fun main() { + val alwaysEvaled = Eval.always { "expensive computation" } + println(alwaysEvaled.value()) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-01.kt new file mode 100644 index 00000000000..a862230d1d2 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-01.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor01 + +import arrow.core.Ior + +fun main() { + Ior.Left("tulip").isRight // Result: false + Ior.Right("venus fly-trap").isRight // Result: true + Ior.Both("venus", "fly-trap").isRight // Result: false +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-02.kt new file mode 100644 index 00000000000..54afb5ce8cb --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-02.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor02 + +import arrow.core.Ior + +fun main() { + Ior.Left("tulip").isLeft // Result: true + Ior.Right("venus fly-trap").isLeft // Result: false + Ior.Both("venus", "fly-trap").isLeft // Result: false +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-03.kt new file mode 100644 index 00000000000..5c61a6f54ee --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-03.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor03 + +import arrow.core.Ior + +fun main() { + Ior.Left("tulip").isBoth // Result: false + Ior.Right("venus fly-trap").isBoth // Result: false + Ior.Both("venus", "fly-trap").isBoth // Result: true +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-04.kt new file mode 100644 index 00000000000..6a71161d52d --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-04.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor04 + +import arrow.core.Ior + +fun main() { + Ior.Right(12).map { "flower" } // Result: Right("flower") + Ior.Left(12).map { "flower" } // Result: Left(12) + Ior.Both(12, "power").map { "flower $it" } // Result: Both(12, "flower power") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-05.kt new file mode 100644 index 00000000000..31da2770112 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-05.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor05 + +import arrow.core.Ior + +fun main() { + Ior.Right(12).bimap ({ "flower" }, { 12 }) // Result: Right(12) + Ior.Left(12).bimap({ "flower" }, { 12 }) // Result: Left("flower") + Ior.Both(12, "power").bimap ({ it * 2 }, { b -> "flower $b" }) // Result: Both("flower power", 24) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-06.kt new file mode 100644 index 00000000000..ae89b383b07 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-06.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor06 + +import arrow.core.Ior + +fun main() { + Ior.Right(12).map { "flower" } // Result: Right(12) + Ior.Left(12).map { "flower" } // Result: Left("power") + Ior.Both(12, "power").map { "flower $it" } // Result: Both("flower 12", "power") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-07.kt new file mode 100644 index 00000000000..ae4f58d4278 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-07.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor07 + +import arrow.core.Ior + +fun main() { + Ior.Left("left").swap() // Result: Right("left") + Ior.Right("right").swap() // Result: Left("right") + Ior.Both("left", "right").swap() // Result: Both("right", "left") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-08.kt new file mode 100644 index 00000000000..3fad9702f90 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-08.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor08 + +import arrow.core.Ior + +fun main() { + val right = Ior.Right(12).padNull() // Result: Pair(null, 12) + val left = Ior.Left(12).padNull() // Result: Pair(12, null) + val both = Ior.Both("power", 12).padNull() // Result: Pair("power", 12) + + println("right = $right") + println("left = $left") + println("both = $both") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-09.kt new file mode 100644 index 00000000000..1433b18e7d8 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-09.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor09 + +import arrow.core.Ior + +fun main() { + Ior.Right(12).toEither() // Result: Either.Right(12) + Ior.Left(12).toEither() // Result: Either.Left(12) + Ior.Both("power", 12).toEither() // Result: Either.Right(12) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-10.kt new file mode 100644 index 00000000000..de4e27ad71d --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-10.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor10 + +import arrow.core.Ior + +fun main() { + val right = Ior.Right(12).orNull() // Result: 12 + val left = Ior.Left(12).orNull() // Result: null + val both = Ior.Both(12, "power").orNull() // Result: "power" + + println("right = $right") + println("left = $left") + println("both = $both") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-11.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-11.kt new file mode 100644 index 00000000000..90e3104ee0c --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-11.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor11 + +import arrow.core.Ior + +fun main() { + val right = Ior.Right(12).leftOrNull() // Result: null + val left = Ior.Left(12).leftOrNull() // Result: 12 + val both = Ior.Both(12, "power").leftOrNull() // Result: 12 + println("right = $right") + println("left = $left") + println("both = $both") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-12.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-12.kt new file mode 100644 index 00000000000..036dce24083 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-12.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor12 + +import arrow.core.Ior + +fun main() { + Ior.Right(12).toValidated() // Result: Valid(12) + Ior.Left(12).toValidated() // Result: Invalid(12) + Ior.Both(12, "power").toValidated() // Result: Valid("power") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-13.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-13.kt new file mode 100644 index 00000000000..bc719ff8273 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-13.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor13 + +import arrow.core.Ior + +fun main() { + Ior.Both(5, 12).exists { it > 10 } // Result: true + Ior.Right(12).exists { it > 10 } // Result: true + Ior.Right(7).exists { it > 10 } // Result: false + val left: Ior = Ior.Left(12) + left.exists { it > 10 } // Result: false +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-14.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-14.kt new file mode 100644 index 00000000000..78ff26a2ff5 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-ior-14.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from Ior.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIor14 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val string: Ior = Ior.Right("Hello") + val chars: Ior = + string.widen() + //sampleEnd + println(chars) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-01.kt new file mode 100644 index 00000000000..e8e55884b74 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-01.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable01 + +import arrow.core.* + +val padRight = listOf(1, 2).padZip(listOf("a")) // Result: [Pair(1, "a"), Pair(2, null)] +val padLeft = listOf(1).padZip(listOf("a", "b")) // Result: [Pair(1, "a"), Pair(null, "b")] +val noPadding = listOf(1, 2).padZip(listOf("a", "b")) // Result: [Pair(1, "a"), Pair(2, "b")] + +fun main() { + println("padRight = $padRight") + println("padLeft = $padLeft") + println("noPadding = $noPadding") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-02.kt new file mode 100644 index 00000000000..de7a53a88a3 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-02.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable02 + +import arrow.core.* + +val padZipRight = listOf(1, 2).padZip(listOf("a")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(2, null)] +val padZipLeft = listOf(1).padZip(listOf("a", "b")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(null, "b")] +val noPadding = listOf(1, 2).padZip(listOf("a", "b")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(2, "b")] + +fun main() { + println("padZipRight = $padZipRight") + println("padZipLeft = $padZipLeft") + println("noPadding = $noPadding") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-03.kt new file mode 100644 index 00000000000..f15d4349974 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-03.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable03 + +import arrow.core.* + +val left = listOf(1, 2).leftPadZip(listOf("a")) { l, r -> l to r } // Result: [Pair(1, "a")] +val right = listOf(1).leftPadZip(listOf("a", "b")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(null, "b")] +val both = listOf(1, 2).leftPadZip(listOf("a", "b")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(2, "b")] + +fun main() { + println("left = $left") + println("right = $right") + println("both = $both") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-04.kt new file mode 100644 index 00000000000..aa5492be95e --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-04.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable04 + +import arrow.core.* + +val padRight = listOf(1, 2).leftPadZip(listOf("a")) // Result: [Pair(1, "a")] +val padLeft = listOf(1).leftPadZip(listOf("a", "b")) // Result: [Pair(1, "a"), Pair(null, "b")] +val noPadding = listOf(1, 2).leftPadZip(listOf("a", "b")) // Result: [Pair(1, "a"), Pair(2, "b")] + +fun main() { + println("padRight = $padRight") + println("padLeft = $padLeft") + println("noPadding = $noPadding") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-05.kt new file mode 100644 index 00000000000..8ad499f39ff --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-05.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable05 + +import arrow.core.* + +val left = listOf(1, 2).rightPadZip(listOf("a")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(null, "b")] +val right = listOf(1).rightPadZip(listOf("a", "b")) { l, r -> l to r } // Result: [Pair(1, "a")] +val both = listOf(1, 2).rightPadZip(listOf("a", "b")) { l, r -> l to r } // Result: [Pair(1, "a"), Pair(2, "b")] + +fun main() { + println("left = $left") + println("right = $right") + println("both = $both") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-06.kt new file mode 100644 index 00000000000..0c9feac4761 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-06.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable06 + +import arrow.core.* + +val padRight = listOf(1, 2).rightPadZip(listOf("a")) // Result: [Pair(1, "a"), Pair(2, null)] +val padLeft = listOf(1).rightPadZip(listOf("a", "b")) // Result: [Pair(1, "a")] +val noPadding = listOf(1, 2).rightPadZip(listOf("a", "b")) // Result: [Pair(1, "a"), Pair(2, "b")] + +fun main() { + println("padRight = $padRight") + println("padLeft = $padLeft") + println("noPadding = $noPadding") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-07.kt new file mode 100644 index 00000000000..cefd2cecdbe --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-07.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable07 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + listOf("A", "B").align(listOf(1, 2, 3)) { + "$it" + } + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-08.kt new file mode 100644 index 00000000000..ecd6d9d8d12 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-08.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable08 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + listOf("A", "B").align(listOf(1, 2, 3)) + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-09.kt new file mode 100644 index 00000000000..77a24907c74 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-09.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable09 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + listOf("A" to 1, "B" to 2).unzip() + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-10.kt new file mode 100644 index 00000000000..18947bafeec --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-10.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable10 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + listOf("A:1", "B:2", "C:3").unzip { e -> + e.split(":").let { + it.first() to it.last() + } + } + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-11.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-11.kt new file mode 100644 index 00000000000..02cd4700e7b --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-11.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable11 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + listOf(("A" to 1).bothIor(), ("B" to 2).bothIor(), "C".leftIor()) + .unalign() + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-12.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-12.kt new file mode 100644 index 00000000000..b26baaa2191 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-12.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable12 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + listOf(1, 2, 3).unalign { + it.leftIor() + } + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-13.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-13.kt new file mode 100644 index 00000000000..aba3d491dba --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-13.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable13 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + listOf("A", "B", "C").split() + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-14.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-14.kt new file mode 100644 index 00000000000..1d230b0e4e8 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-14.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable14 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val tags = List(10) { "#" } + val result = + tags.interleave(listOf("A", "B", "C")) + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-15.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-15.kt new file mode 100644 index 00000000000..3b4f078741e --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-15.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable15 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + listOf(1,2,3).unweave { i -> listOf("$i, ${i + 1}") } + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-16.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-16.kt new file mode 100644 index 00000000000..5868fe6d58d --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-iterable-16.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Iterable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleIterable16 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + listOf(1,2,3).ifThen(listOf("empty")) { i -> + listOf("$i, ${i + 1}") + } + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-01.kt new file mode 100644 index 00000000000..b49b4ae91bd --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-01.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from map.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleMap01 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + mapOf(1 to "A", 2 to "B").zip(mapOf(1 to "1", 2 to "2", 3 to "3")) + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-02.kt new file mode 100644 index 00000000000..49018786155 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-02.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from map.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleMap02 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + mapOf(1 to "A", 2 to "B").zip(mapOf(1 to "1", 2 to "2", 3 to "3")) { + key, a, b -> "$key -> $a # $b" + } + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-03.kt new file mode 100644 index 00000000000..e8e18e34b25 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-03.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from map.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleMap03 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + mapOf("1" to 1, "2" to 2).align(mapOf("1" to 1, "2" to 2, "3" to 3)) + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-04.kt new file mode 100644 index 00000000000..a6307681fdf --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-04.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from map.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleMap04 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + mapOf("1" to 1, "2" to 2).align(mapOf("1" to 1, "2" to 2, "3" to 3)) { (_, a) -> + "$a" + } + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-05.kt new file mode 100644 index 00000000000..4c612d333a7 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-05.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from map.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleMap05 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + mapOf( + "first" to ("A" to 1).bothIor(), + "second" to ("B" to 2).bothIor(), + "third" to "C".leftIor() + ).unalign() + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-06.kt new file mode 100644 index 00000000000..6efbe31a8ea --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-06.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from map.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleMap06 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + mapOf("1" to 1, "2" to 2, "3" to 3) + .unalign { it.leftIor() } + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-07.kt new file mode 100644 index 00000000000..30921292649 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-07.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from map.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleMap07 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + mapOf("first" to ("A" to 1), "second" to ("B" to 2)).unzip() + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-08.kt new file mode 100644 index 00000000000..50b864e1bbf --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-map-08.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from map.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleMap08 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val result = + mapOf("first" to "A:1", "second" to "B:2", "third" to "C:3").unzip { (_, e) -> + e.split(":").let { + it.first() to it.last() + } + } + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-memoization-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-memoization-01.kt new file mode 100644 index 00000000000..cda809e4e34 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-memoization-01.kt @@ -0,0 +1,19 @@ +// This file was automatically generated from memoization.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleMemoization01 + +import arrow.core.memoize +fun someWorkIntensiveFunction(someParam: Int): String = "$someParam" + +fun main() { + //sampleStart + val memoizedF = ::someWorkIntensiveFunction.memoize() + + // The first invocation will store the argument and result in a cache inside the `memoizedF` reference. + val value1 = memoizedF(42) + // This second invocation won't really call the `someWorkIntensiveFunction` function + //but retrieve the result from the previous invocation instead. + val value2 = memoizedF(42) + + //sampleEnd + println("$value1 $value2") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-01.kt new file mode 100644 index 00000000000..ac3f0223b3c --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-01.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from NonEmptyList.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleNonemptylist01 + +import arrow.core.nonEmptyListOf +import arrow.core.toNonEmptyListOrNull + +fun main() { + println(nonEmptyListOf(1, 2, 3, 4, 5)) + println(listOf(1, 2, 3).toNonEmptyListOrNull()) + println(emptyList().toNonEmptyListOrNull()) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-02.kt new file mode 100644 index 00000000000..8c6a37acb50 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-02.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from NonEmptyList.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleNonemptylist02 + +import arrow.core.nonEmptyListOf + +val value = + nonEmptyListOf(1, 2, 3, 4, 5).head +fun main() { + println(value) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-03.kt new file mode 100644 index 00000000000..9e1736be11a --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-03.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from NonEmptyList.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleNonemptylist03 + +import arrow.core.NonEmptyList +import arrow.core.nonEmptyListOf + +fun sumNel(nel: NonEmptyList): Int = + nel.foldLeft(0) { acc, n -> acc + n } +val value = sumNel(nonEmptyListOf(1, 1, 1, 1)) +fun main() { + println("value = $value") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-04.kt new file mode 100644 index 00000000000..ff4cf7e26b5 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-04.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from NonEmptyList.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleNonemptylist04 + +import arrow.core.nonEmptyListOf + +val value = + nonEmptyListOf(1, 1, 1, 1).map { it + 1 } +fun main() { + println(value) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-05.kt new file mode 100644 index 00000000000..2815eb54f62 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-05.kt @@ -0,0 +1,17 @@ +// This file was automatically generated from NonEmptyList.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleNonemptylist05 + +import arrow.core.NonEmptyList +import arrow.core.nonEmptyListOf + +val nelOne: NonEmptyList = nonEmptyListOf(1, 2, 3) +val nelTwo: NonEmptyList = nonEmptyListOf(4, 5) + +val value = nelOne.flatMap { one -> + nelTwo.map { two -> + one + two + } +} +fun main() { + println("value = $value") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-06.kt new file mode 100644 index 00000000000..45db1b00351 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonemptylist-06.kt @@ -0,0 +1,21 @@ +// This file was automatically generated from NonEmptyList.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleNonemptylist06 + +import arrow.core.NonEmptyList +import arrow.core.nonEmptyListOf +import arrow.core.zip +import java.util.UUID + +data class Person(val id: UUID, val name: String, val year: Int) + +// Note each NonEmptyList is of a different type +val nelId: NonEmptyList = nonEmptyListOf(UUID.randomUUID(), UUID.randomUUID()) +val nelName: NonEmptyList = nonEmptyListOf("William Alvin Howard", "Haskell Curry") +val nelYear: NonEmptyList = nonEmptyListOf(1926, 1900) + +val value = nelId.zip(nelName, nelYear) { id, name, year -> + Person(id, name, year) +} +fun main() { + println("value = $value") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatal-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatal-01.kt new file mode 100644 index 00000000000..32ebd78ac2e --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatal-01.kt @@ -0,0 +1,28 @@ +// This file was automatically generated from NonFatal.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleNonfatal01 + +import arrow.* +import arrow.core.* + +fun unsafeFunction(i: Int): String = + when (i) { + 1 -> throw IllegalArgumentException("Non-Fatal") + 2 -> throw OutOfMemoryError("Fatal") + else -> "Hello" + } + +fun main(args: Array) { + val nonFatal: Either = + //sampleStart + try { + Either.Right(unsafeFunction(1)) + } catch (t: Throwable) { + if (NonFatal(t)) { + Either.Left(t) + } else { + throw t + } + } + //sampleEnd + println(nonFatal) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatalorthrow-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatalorthrow-01.kt new file mode 100644 index 00000000000..07bf2372556 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nonfatalorthrow-01.kt @@ -0,0 +1,24 @@ +// This file was automatically generated from nonFatalOrThrow.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleNonfatalorthrow01 + +import arrow.* +import arrow.core.* + +fun unsafeFunction(i: Int): String = + when (i) { + 1 -> throw IllegalArgumentException("Non-Fatal") + 2 -> throw OutOfMemoryError("Fatal") + else -> "Hello" + } + +fun main(args: Array) { + val nonFatal: Either = + //sampleStart + try { + Either.Right(unsafeFunction(1)) + } catch (t: Throwable) { + Either.Left(t.nonFatalOrThrow()) + } + //sampleEnd + println(nonFatal) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-01.kt new file mode 100644 index 00000000000..56bda9ce6f8 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-01.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from nullable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleNullableComputations01 + +import arrow.core.computations.nullable + +suspend fun main() { + nullable { + ensure(true) + println("ensure(true) passes") + ensure(false) + 1 + } + .let(::println) +} +// println: "ensure(true) passes" +// res: null diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-02.kt new file mode 100644 index 00000000000..a14151a5e3f --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-nullable-computations-02.kt @@ -0,0 +1,17 @@ +// This file was automatically generated from nullable.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleNullableComputations02 + +import arrow.core.computations.nullable +import arrow.core.computations.ensureNotNull + +suspend fun main() { + nullable { + val x: Int? = 1 + ensureNotNull(x) + println(x) + ensureNotNull(null) + } + .let(::println) +} +// println: "1" +// res: null diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-01.kt new file mode 100644 index 00000000000..fb48ae937cf --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-01.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption01 + +import arrow.core.Option +import arrow.core.Some +import arrow.core.none + +val someValue: Option = Some("I am wrapped in something") +val emptyValue: Option = none() +fun main() { + println("value = $someValue") + println("emptyValue = $emptyValue") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-02.kt new file mode 100644 index 00000000000..1432d694e9f --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-02.kt @@ -0,0 +1,9 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption02 + +import arrow.core.None +import arrow.core.Option +import arrow.core.Some + +fun maybeItWillReturnSomething(flag: Boolean): Option = + if (flag) Some("Found value") else None diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-03.kt new file mode 100644 index 00000000000..6da22ce3726 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-03.kt @@ -0,0 +1,17 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption03 + +import arrow.core.None +import arrow.core.Option +import arrow.core.Some +import arrow.core.getOrElse + +fun maybeItWillReturnSomething(flag: Boolean): Option = + if (flag) Some("Found value") else None + +val value1 = + maybeItWillReturnSomething(true) + .getOrElse { "No value" } +fun main() { + println(value1) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-04.kt new file mode 100644 index 00000000000..7c12966a132 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-04.kt @@ -0,0 +1,17 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption04 + +import arrow.core.None +import arrow.core.Option +import arrow.core.Some +import arrow.core.getOrElse + +fun maybeItWillReturnSomething(flag: Boolean): Option = + if (flag) Some("Found value") else None + +val value2 = + maybeItWillReturnSomething(false) + .getOrElse { "No value" } +fun main() { + println(value2) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-05.kt new file mode 100644 index 00000000000..a361ab589a3 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-05.kt @@ -0,0 +1,17 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption05 + +import arrow.core.None +import arrow.core.Option +import arrow.core.Some + +fun maybeItWillReturnSomething(flag: Boolean): Option = + if (flag) Some("Found value") else None + + //sampleStart +val valueSome = maybeItWillReturnSomething(true) is None +val valueNone = maybeItWillReturnSomething(false) is None +fun main() { + println("valueSome = $valueSome") + println("valueNone = $valueNone") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-06.kt new file mode 100644 index 00000000000..58aabd86f00 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-06.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption06 + +import arrow.core.Option + +val myString: String? = "Nullable string" +val option: Option = Option.fromNullable(myString) +fun main () { + println("option = $option") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-07.kt new file mode 100644 index 00000000000..e1db8c17a18 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-07.kt @@ -0,0 +1,15 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption07 + +import arrow.core.None +import arrow.core.Option +import arrow.core.Some + +val someValue: Option = Some(20.0) +val value = when(someValue) { + is Some -> someValue.value + is None -> 0.0 +} +fun main () { + println("value = $value") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-08.kt new file mode 100644 index 00000000000..724583e8baa --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-08.kt @@ -0,0 +1,15 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption08 + +import arrow.core.None +import arrow.core.Option +import arrow.core.Some + +val noValue: Option = None +val value = when(noValue) { + is Some -> noValue.value + is None -> 0.0 +} +fun main () { + println("value = $value") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-09.kt new file mode 100644 index 00000000000..d7a653b6594 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-09.kt @@ -0,0 +1,17 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption09 + +import arrow.core.None +import arrow.core.Option +import arrow.core.Some + +val number: Option = Some(3) +val noNumber: Option = None +val mappedResult1 = number.map { it * 1.5 } +val mappedResult2 = noNumber.map { it * 1.5 } +fun main () { + println("number = $number") + println("noNumber = $noNumber") + println("mappedResult1 = $mappedResult1") + println("mappedResult2 = $mappedResult2") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-10.kt new file mode 100644 index 00000000000..d40b355b33f --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-10.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption10 + +import arrow.core.Option +import arrow.core.Some + +val fold = + Some(3).fold({ 1 }, { it * 3 }) +fun main () { + println(fold) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-11.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-11.kt new file mode 100644 index 00000000000..9d6fb8aabf3 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-11.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption11 + +import arrow.core.Option +import arrow.core.none + +val fold = + none().fold({ 1 }, { it * 3 }) +fun main () { + println(fold) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-12.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-12.kt new file mode 100644 index 00000000000..52ce79b7e47 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-12.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption12 + +import arrow.core.some +import arrow.core.none + + val some = 1.some() + val none = none() +fun main () { + println("some = $some") + println("none = $none") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-13.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-13.kt new file mode 100644 index 00000000000..b7a2ebd9291 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-13.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption13 + +import arrow.core.toOption + +val nullString: String? = null +val valueFromNull = nullString.toOption() + +val helloString: String? = "Hello" +val valueFromStr = helloString.toOption() +fun main () { + println("valueFromNull = $valueFromNull") + println("valueFromStr = $valueFromStr") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-14.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-14.kt new file mode 100644 index 00000000000..589ff874dd1 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-14.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption14 + +import arrow.core.firstOrNone +import arrow.core.toOption +import arrow.core.Option + +val foxMap = mapOf(1 to "The", 2 to "Quick", 3 to "Brown", 4 to "Fox") + +val empty = foxMap.entries.firstOrNull { it.key == 5 }?.value.let { it?.toCharArray() }.toOption() +val filled = Option.fromNullable(foxMap.entries.firstOrNull { it.key == 5 }?.value.let { it?.toCharArray() }) + +fun main() { + println("empty = $empty") + println("filled = $filled") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-15.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-15.kt new file mode 100644 index 00000000000..26350e3c3cd --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-15.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption15 + +import arrow.core.Some + +fun main() { +val value = + //sampleStart + Some(1).map { it + 1 } + //sampleEnd + println(value) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-16.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-16.kt new file mode 100644 index 00000000000..f4cc52a3a80 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-16.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption16 + +import arrow.core.Some + + val value = + Some(1).zip(Some("Hello"), Some(20.0), ::Triple) +fun main() { + println(value) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-17.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-17.kt new file mode 100644 index 00000000000..4fc22facd84 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-17.kt @@ -0,0 +1,17 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption17 + +import arrow.core.computations.option +import arrow.core.Some +import arrow.core.Option + +suspend fun value(): Option = + option { + val a = Some(1).bind() + val b = Some(1 + a).bind() + val c = Some(1 + b).bind() + a + b + c +} +suspend fun main() { + println(value()) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-18.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-18.kt new file mode 100644 index 00000000000..e47211b5ef6 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-18.kt @@ -0,0 +1,18 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption18 + +import arrow.core.computations.option +import arrow.core.Some +import arrow.core.none +import arrow.core.Option + +suspend fun value(): Option = + option { + val x = none().bind() + val y = Some(1 + x).bind() + val z = Some(1 + y).bind() + x + y + z + } +suspend fun main() { + println(value()) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-19.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-19.kt new file mode 100644 index 00000000000..364813486b9 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-19.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption19 + +import arrow.core.Some +import arrow.core.none + +fun main() { + Some(12).tapNone { println("flower") } // Result: Some(12) + none().tapNone { println("flower") } // Result: prints "flower" and returns: None +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-20.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-20.kt new file mode 100644 index 00000000000..2e5652f4aac --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-20.kt @@ -0,0 +1,10 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption20 + +import arrow.core.Some +import arrow.core.none + +fun main() { + Some(12).tap { println("flower") } // Result: prints "flower" and returns: Some(12) + none().tap { println("flower") } // Result: None +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-21.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-21.kt new file mode 100644 index 00000000000..07a9d4ec693 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-21.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption21 + +import arrow.core.Some +import arrow.core.None +import arrow.core.Option + +fun main() { + Some(12).exists { it > 10 } // Result: true + Some(7).exists { it > 10 } // Result: false + + val none: Option = None + none.exists { it > 10 } // Result: false +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-22.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-22.kt new file mode 100644 index 00000000000..9263fd76499 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-22.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption22 + +import arrow.core.Some +import arrow.core.None +import arrow.core.Option + +fun main() { + Some(12).exists { it > 10 } // Result: 12 + Some(7).exists { it > 10 } // Result: null + + val none: Option = None + none.exists { it > 10 } // Result: null +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-23.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-23.kt new file mode 100644 index 00000000000..dfcf262a5ae --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-23.kt @@ -0,0 +1,3 @@ +// This file was automatically generated from Option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOption23 + diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-01.kt new file mode 100644 index 00000000000..bd53693beba --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-01.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOptionComputations01 + +import arrow.core.computations.option + +suspend fun main() { + option { + ensure(true) + println("ensure(true) passes") + ensure(false) + 1 + } + .let(::println) +} +// println: "ensure(true) passes" +// res: None diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-02.kt new file mode 100644 index 00000000000..cc9f62c7d1b --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-option-computations-02.kt @@ -0,0 +1,17 @@ +// This file was automatically generated from option.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleOptionComputations02 + +import arrow.core.computations.option +import arrow.core.computations.ensureNotNull + +suspend fun main() { + option { + val x: Int? = 1 + ensureNotNull(x) + println(x) + ensureNotNull(null) + } + .let(::println) +} +// println: "1" +// res: None diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-result-computations-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-result-computations-01.kt new file mode 100644 index 00000000000..66f22e58f7f --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-result-computations-01.kt @@ -0,0 +1,23 @@ +// This file was automatically generated from result.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleResultComputations01 + +import arrow.core.* +import arrow.core.computations.result + +fun main() { + result { // We can safely use assertion based operation inside blocks + kotlin.require(false) { "Boom" } + } // Result.Failure(IllegalArgumentException("Boom")) + + result { + Result.failure(RuntimeException("Boom")) + .recover { 1 } + .bind() + } // Result.Success(1) + + result { + val x = Result.success(1).bind() + val y = Result.success(x + 1).bind() + x + y + } // Result.Success(3) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-01.kt new file mode 100644 index 00000000000..dcefb6f4865 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-01.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Semiring.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSemiring01 + +import arrow.typeclasses.Semiring + +fun main(args: Array) { + val result = + //sampleStart + Semiring.int().run { 1.combine(2) } + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-02.kt new file mode 100644 index 00000000000..b9bc9d80f41 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-02.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Semiring.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSemiring02 + +import arrow.typeclasses.Semiring + +fun main(args: Array) { + val result = + //sampleStart + Semiring.int().run { 2.combineMultiplicate(3) } + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-03.kt new file mode 100644 index 00000000000..663866438fd --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-03.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Semiring.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSemiring03 + +import arrow.typeclasses.Semiring + +fun main(args: Array) { + val result = + //sampleStart + Semiring.int().run { + 1 + 2 + } + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-04.kt new file mode 100644 index 00000000000..c96dc96f2a6 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-semiring-04.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Semiring.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSemiring04 + +import arrow.typeclasses.Semiring + +fun main(args: Array) { + val result = + //sampleStart + Semiring.int().run { + 2 * 3 + } + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-01.kt new file mode 100644 index 00000000000..12a2be59d90 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-01.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence01 + +import arrow.core.align + +fun main(args: Array) { + //sampleStart + val result = + sequenceOf("A", "B").align(sequenceOf(1, 2, 3)) { + "$it" + } + //sampleEnd + println(result.toList()) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-02.kt new file mode 100644 index 00000000000..9566c93918d --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-02.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence02 + +import arrow.core.align + +fun main(args: Array) { + //sampleStart + val result = + sequenceOf("A", "B").align(sequenceOf(1, 2, 3)) + //sampleEnd + println(result.toList()) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-03.kt new file mode 100644 index 00000000000..22d99f25487 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-03.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence03 + +import arrow.core.ifThen + +fun main(args: Array) { + //sampleStart + val result = + sequenceOf(1,2,3).ifThen(sequenceOf("empty")) { i -> + sequenceOf("$i, ${i + 1}") + } + //sampleEnd + println(result.toList()) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-04.kt new file mode 100644 index 00000000000..aa0ab0e390d --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-04.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence04 + +import arrow.core.interleave + +fun main(args: Array) { + //sampleStart + val tags = generateSequence { "#" }.take(10) + val result = + tags.interleave(sequenceOf("A", "B", "C")) + //sampleEnd + println(result.toList()) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-05.kt new file mode 100644 index 00000000000..025d052dd5a --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-05.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence05 + +import arrow.core.leftPadZip + +val left = sequenceOf(1, 2).leftPadZip(sequenceOf(3)) { l, r -> l?.plus(r) ?: r } // Result: [4] +val right = sequenceOf(1).leftPadZip(sequenceOf(3, 4)) { l, r -> l?.plus(r) ?: r } // Result: [4, 4] +val both = sequenceOf(1, 2).leftPadZip(sequenceOf(3, 4)) { l, r -> l?.plus(r) ?: r } // Result: [4, 6] + +fun main() { + println("left = $left") + println("right = $right") + println("both = $both") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-06.kt new file mode 100644 index 00000000000..bcff445249b --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-06.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence06 + +import arrow.core.leftPadZip + +val padRight = sequenceOf(1, 2).leftPadZip(sequenceOf("a")) // Result: [Pair(1, "a")] +val padLeft = sequenceOf(1).leftPadZip(sequenceOf("a", "b")) // Result: [Pair(1, "a"), Pair(null, "b")] +val noPadding = sequenceOf(1, 2).leftPadZip(sequenceOf("a", "b")) // Result: [Pair(1, "a"), Pair(2, "b")] + +fun main() { + println("padRight = $padRight") + println("padLeft = $padLeft") + println("noPadding = $noPadding") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-07.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-07.kt new file mode 100644 index 00000000000..d4f7e45722e --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-07.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence07 + +import arrow.core.padZip + +val padRight = sequenceOf(1, 2).padZip(sequenceOf("a")) // Result: [Pair(1, "a"), Pair(2, null)] +val padLeft = sequenceOf(1).padZip(sequenceOf("a", "b")) // Result: [Pair(1, "a"), Pair(null, "b")] +val noPadding = sequenceOf(1, 2).padZip(sequenceOf("a", "b")) // Result: [Pair(1, "a"), Pair(2, "b")] + +fun main() { + println("padRight = $padRight") + println("padLeft = $padLeft") + println("noPadding = $noPadding") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-08.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-08.kt new file mode 100644 index 00000000000..1b85b72cfbc --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-08.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence08 + +import arrow.core.padZip + +val padZipRight = sequenceOf(1, 2).padZip(sequenceOf(3)) { l, r -> (l?:0) + (r?:0) } // Result: [4, 2] +val padZipLeft = sequenceOf(1).padZip(sequenceOf(3, 4)) { l, r -> (l?:0) + (r?:0) } // Result: [4, 4] +val noPadding = sequenceOf(1, 2).padZip(sequenceOf(3, 4)) { l, r -> (l?:0) + (r?:0) } // Result: [4, 6] + +fun main() { + println("padZipRight = $padZipRight") + println("padZipLeft = $padZipLeft") + println("noPadding = $noPadding") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-09.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-09.kt new file mode 100644 index 00000000000..ae75fa6cd22 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-09.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence09 + +import arrow.core.rightPadZip + +val left = sequenceOf(1, 2).rightPadZip(sequenceOf(3)) { l, r -> l + (r?:0) } // Result: [4, 2] +val right = sequenceOf(1).rightPadZip(sequenceOf(3, 4)) { l, r -> l + (r?:0) } // Result: [4] +val both = sequenceOf(1, 2).rightPadZip(sequenceOf(3, 4)) { l, r -> l + (r?:0) } // Result: [4, 6] + +fun main() { + println("left = $left") + println("right = $right") + println("both = $both") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-10.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-10.kt new file mode 100644 index 00000000000..ecc6a38dab9 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-10.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence10 + +import arrow.core.rightPadZip + +val padRight = sequenceOf(1, 2).rightPadZip(sequenceOf("a")) // Result: [Pair(1, "a"), Pair(2, null)] +val padLeft = sequenceOf(1).rightPadZip(sequenceOf("a", "b")) // Result: [Pair(1, "a")] +val noPadding = sequenceOf(1, 2).rightPadZip(sequenceOf("a", "b")) // Result: [Pair(1, "a"), Pair(2, "b")] + +fun main() { + println("padRight = $padRight") + println("padLeft = $padLeft") + println("noPadding = $noPadding") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-11.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-11.kt new file mode 100644 index 00000000000..0e7ce3cdaaf --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-11.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence11 + +import arrow.core.split + +fun main(args: Array) { + //sampleStart + val result = sequenceOf("A", "B", "C").split() + //sampleEnd + result?.let { println("(${it.first.toList()}, ${it.second.toList()})") } +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-12.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-12.kt new file mode 100644 index 00000000000..3ad3a8684f2 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-12.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence12 + +import arrow.core.bothIor +import arrow.core.leftIor +import arrow.core.unalign + +fun main(args: Array) { + //sampleStart + val result = sequenceOf(("A" to 1).bothIor(), ("B" to 2).bothIor(), "C".leftIor()).unalign() + //sampleEnd + println("(${result.first.toList()}, ${result.second.toList()})") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-13.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-13.kt new file mode 100644 index 00000000000..5f18e813bc1 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-13.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence13 + +import arrow.core.leftIor +import arrow.core.unalign + +fun main(args: Array) { + //sampleStart + val result = sequenceOf(1, 2, 3).unalign { it.leftIor() } + //sampleEnd + println("(${result.first.toList()}, ${result.second.toList()})") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-14.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-14.kt new file mode 100644 index 00000000000..511d154024a --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-14.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence14 + +import arrow.core.unweave + +fun main(args: Array) { + //sampleStart + val result = sequenceOf(1,2,3).unweave { i -> sequenceOf("$i, ${i + 1}") } + //sampleEnd + println(result.toList()) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-15.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-15.kt new file mode 100644 index 00000000000..b5f5cf116ae --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-15.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence15 + +import arrow.core.unzip + +fun main(args: Array) { + //sampleStart + val result = sequenceOf("A" to 1, "B" to 2).unzip() + //sampleEnd + println("(${result.first.toList()}, ${result.second.toList()})") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-16.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-16.kt new file mode 100644 index 00000000000..4a8ab2e451d --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-sequence-16.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from Sequence.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleSequence16 + +import arrow.core.unzip + +fun main(args: Array) { + //sampleStart + val result = + sequenceOf("A:1", "B:2", "C:3").unzip { e -> + e.split(":").let { + it.first() to it.last() + } + } + //sampleEnd + println("(${result.first.toList()}, ${result.second.toList()})") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-01.kt new file mode 100644 index 00000000000..fc02b28723c --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-01.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from Validated.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleValidated01 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val f = Validated.lift(String::toUpperCase, Int::inc) + val res1 = f("test".invalid()) + val res2 = f(1.valid()) + //sampleEnd + println("res1: $res1") + println("res2: $res2") +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-02.kt new file mode 100644 index 00000000000..68849d20db2 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-02.kt @@ -0,0 +1,12 @@ +// This file was automatically generated from Validated.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleValidated02 + +import arrow.core.* + +fun main(args: Array) { + val result = + //sampleStart + "Hello World".valid().void() + //sampleEnd + println(result) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-03.kt new file mode 100644 index 00000000000..e5b5f1336cf --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-03.kt @@ -0,0 +1,9 @@ +// This file was automatically generated from Validated.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleValidated03 + +import arrow.core.Validated + +fun main() { + Validated.Valid(12).tapInvalid { println("flower") } // Result: Valid(12) + Validated.Invalid(12).tapInvalid { println("flower") } // Result: prints "flower" and returns: Invalid(12) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-04.kt new file mode 100644 index 00000000000..c13fa930955 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-04.kt @@ -0,0 +1,9 @@ +// This file was automatically generated from Validated.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleValidated04 + +import arrow.core.Validated + +fun main() { + Validated.Valid(12).tap { println("flower") } // Result: prints "flower" and returns: Valid(12) + Validated.Invalid(12).tap { println("flower") } // Result: Invalid(12) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-05.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-05.kt new file mode 100644 index 00000000000..af73083b797 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-05.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from Validated.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleValidated05 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val string: Validated = "Hello".valid() + val chars: Validated = + string.widen() + //sampleEnd + println(chars) +} diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-06.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-06.kt new file mode 100644 index 00000000000..4cf01a61751 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-validated-06.kt @@ -0,0 +1,11 @@ +// This file was automatically generated from Validated.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleValidated06 + +import arrow.core.Validated +import arrow.core.andThen + +fun main() { + Validated.Valid(5).andThen { Validated.Valid(10) } // Result: Valid(10) + Validated.Valid(5).andThen { Validated.Invalid(10) } // Result: Invalid(10) + Validated.Invalid(5).andThen { Validated.Valid(10) } // Result: Invalid(5) +} From e9cbf6e703f00b3477513174ddc50acd7349aed3 Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Wed, 17 Aug 2022 08:07:53 +0200 Subject: [PATCH 05/20] Fix error handlers signatures --- .../kotlin/arrow/core/continuations/Effect.kt | 3 --- .../arrow/core/continuations/ErrorHandlers.kt | 22 ++++++++++++------- .../kotlin/arrow/core/continuations/Shift.kt | 7 +++++- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt index 8864bd00f18..b1936e95c97 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt @@ -199,13 +199,10 @@ import kotlin.jvm.JvmMultifileClass * We define a `val failed` of type `Effect`, that represents a failed effect with value "failed". * * * ```kotlin * val failed: Effect = diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt index 5dfedf065e9..69e7284549d 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt @@ -11,32 +11,38 @@ import kotlin.jvm.JvmName @BuilderInference public infix fun Effect.catch(resolve: suspend Shift.(shifted: E) -> A): Effect = - effect { catch { e -> resolve(e) } } + effect { catch(resolve) } @BuilderInference public infix fun Effect.attempt(recover: suspend Shift.(throwable: Throwable) -> A): Effect = - effect { attempt({ invoke() }) { t -> recover(t) } } + effect { attempt(recover) } @BuilderInference @JvmName("attemptOrThrow") public inline infix fun Effect.attempt( crossinline recover: suspend Shift.(T) -> A, ): Effect = - effect { attempt({ invoke() }) { t: T -> recover(t) } } + effect { attempt { t: Throwable -> if (t is T) recover(t) else throw t } } -public fun Effect.attempt(): Effect> = - effect{ kotlin.runCatching { invoke() }.onFailure { it.nonFatalOrThrow() } } +public fun Effect.attempt(): Effect> = + effect { + try { + Result.success(invoke()) + } catch (e: Throwable) { + Result.failure(e.nonFatalOrThrow()) + } + } @BuilderInference public infix fun EagerEffect.catch(resolve: Shift.(shifted: E) -> A): EagerEffect = - eagerEffect { catch { e -> resolve(e) } } + eagerEffect { catch(resolve) } @BuilderInference public infix fun EagerEffect.attempt(recover: Shift.(throwable: Throwable) -> A): EagerEffect = - eagerEffect { attempt({ invoke() }) { t -> recover(t) } } + eagerEffect { attempt(recover) } @JvmName("attemptOrThrow") public inline infix fun EagerEffect.attempt( @BuilderInference crossinline recover: Shift.(T) -> A, ): EagerEffect = - eagerEffect { attempt({ invoke() }) { t: T -> recover(t) } } + eagerEffect { attempt { t: Throwable -> if (t is T) recover(t) else throw t } } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt index ee2bb6e2a39..19035079067 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt @@ -63,6 +63,11 @@ public interface Shift { @EffectDSL public suspend fun Effect.attempt( + @BuilderInference recover: suspend Shift.(Throwable) -> A, + ): A = fold({ recover(it) }, { shift(it) }, { it }) + + @EffectDSL + public fun EagerEffect.attempt( @BuilderInference recover: Shift.(Throwable) -> A, ): A = fold({ recover(it) }, { shift(it) }, { it }) } @@ -91,7 +96,7 @@ public inline fun Shift.attempt( public inline fun Shift.attempt( @BuilderInference action: Shift.() -> A, @BuilderInference recover: Shift.(T) -> A, -): A = attempt(action) { if (it is T) recover(it) else throw it } +): A = attempt(action) { t: Throwable -> if (t is T) recover(t) else throw t } @EffectDSL public inline fun Shift.ensure(condition: Boolean, shift: () -> R): Unit = From 942313a97679c3cb44293598fe3b87f0510be547 Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Wed, 17 Aug 2022 08:11:47 +0200 Subject: [PATCH 06/20] Add catch for Either --- .../arrow-core/src/commonMain/kotlin/arrow/core/Either.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt index c96a712caf6..4322e5ae2e4 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt @@ -3,6 +3,8 @@ package arrow.core import arrow.core.Either.Companion.resolve import arrow.core.Either.Left import arrow.core.Either.Right +import arrow.core.continuations.Shift +import arrow.core.continuations.either import arrow.typeclasses.Monoid import arrow.typeclasses.Semigroup import kotlin.experimental.ExperimentalTypeInference @@ -1229,6 +1231,12 @@ public sealed class Either { map { Unit } } +public inline fun Either.catch(block: Shift.(E) -> A): Either = + when (this) { + is Right -> this + is Left -> either { block(value) } + } + /** * Binds the given function across [Right]. * From b21b56b11fe3f0726fad9dc7f39def2f803d49ae Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Wed, 17 Aug 2022 08:44:20 +0200 Subject: [PATCH 07/20] Re-add full test specs --- .../commonMain/kotlin/arrow/core/Either.kt | 124 +----- .../arrow/core/continuations/Builders.kt | 6 +- .../arrow/core/continuations/ErrorHandlers.kt | 15 +- .../core/continuations/EagerEffectSpec.kt | 140 +++++-- .../arrow/core/continuations/EffectSpec.kt | 369 +++++++++--------- .../kotlin/examples/example-effect-04.kt | 3 - 6 files changed, 326 insertions(+), 331 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt index 4322e5ae2e4..2906703f445 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt @@ -5,6 +5,7 @@ import arrow.core.Either.Left import arrow.core.Either.Right import arrow.core.continuations.Shift import arrow.core.continuations.either +import arrow.core.continuations.ensure import arrow.typeclasses.Monoid import arrow.typeclasses.Semigroup import kotlin.experimental.ExperimentalTypeInference @@ -1485,22 +1486,13 @@ public inline fun Any?.rightIfNull(default: () -> A): Either = * This is like `flatMap` for the exception. */ public inline fun Either.handleErrorWith(f: (A) -> Either): Either = - when (this) { - is Left -> f(this.value) - is Right -> this - } + catch { f(it).bind() } public inline fun Either.handleError(f: (A) -> B): Either = - when (this) { - is Left -> f(value).right() - is Right -> this - } + catch { f(it) } public inline fun Either.redeem(fe: (A) -> C, fa: (B) -> C): Either = - when (this) { - is Left -> fe(value).right() - is Right -> map(fa) - } + either { fa(bind()) }.catch { fe(it) } public operator fun , B : Comparable> Either.compareTo(other: Either): Int = fold( @@ -1548,31 +1540,17 @@ public fun Either.leftWiden(): Either = this public fun Either.zip(fb: Either, f: (B, C) -> D): Either = - flatMap { b -> - fb.map { c -> f(b, c) } - } + either { f(bind(), fb.bind()) } public fun Either.zip(fb: Either): Either> = - flatMap { a -> - fb.map { b -> Pair(a, b) } - } + either { Pair(bind(), fb.bind()) } public inline fun Either.zip( c: Either, d: Either, map: (B, C, D) -> E ): Either = - zip( - c, - d, - Right.unit, - Right.unit, - Right.unit, - Right.unit, - Right.unit, - Right.unit, - Right.unit - ) { b, c, d, _, _, _, _, _, _, _ -> map(b, c, d) } + either { map(bind(), c.bind(), d.bind()) } public inline fun Either.zip( c: Either, @@ -1580,17 +1558,7 @@ public inline fun Either.zip( e: Either, map: (B, C, D, E) -> F ): Either = - zip( - c, - d, - e, - Right.unit, - Right.unit, - Right.unit, - Right.unit, - Right.unit, - Right.unit - ) { b, c, d, e, _, _, _, _, _, _ -> map(b, c, d, e) } + either { map(bind(), c.bind(), d.bind(), e.bind()) } public inline fun Either.zip( c: Either, @@ -1599,17 +1567,7 @@ public inline fun Either.zip( f: Either, map: (B, C, D, E, F) -> G ): Either = - zip( - c, - d, - e, - f, - Right.unit, - Right.unit, - Right.unit, - Right.unit, - Right.unit - ) { b, c, d, e, f, _, _, _, _, _ -> map(b, c, d, e, f) } + either { map(bind(), c.bind(), d.bind(), e.bind(), f.bind()) } public inline fun Either.zip( c: Either, @@ -1619,16 +1577,7 @@ public inline fun Either.zip( g: Either, map: (B, C, D, E, F, G) -> H ): Either = - zip(c, d, e, f, g, Right.unit, Right.unit, Right.unit, Right.unit) { b, c, d, e, f, g, _, _, _, _ -> - map( - b, - c, - d, - e, - f, - g - ) - } + either { map(bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind()) } public inline fun Either.zip( c: Either, @@ -1639,17 +1588,7 @@ public inline fun Either.zip( h: Either, map: (B, C, D, E, F, G, H) -> I ): Either = - zip(c, d, e, f, g, h, Right.unit, Right.unit, Right.unit) { b, c, d, e, f, g, h, _, _, _ -> - map( - b, - c, - d, - e, - f, - g, - h - ) - } + either { map(bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind(), h.bind()) } public inline fun Either.zip( c: Either, @@ -1661,7 +1600,7 @@ public inline fun Either.zip( i: Either, map: (B, C, D, E, F, G, H, I) -> J ): Either = - zip(c, d, e, f, g, h, i, Right.unit, Right.unit) { b, c, d, e, f, g, h, i, _, _ -> map(b, c, d, e, f, g, h, i) } + either { map(bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind(), h.bind(), i.bind()) } public inline fun Either.zip( c: Either, @@ -1674,7 +1613,7 @@ public inline fun Either.zip( j: Either, map: (B, C, D, E, F, G, H, I, J) -> K ): Either = - zip(c, d, e, f, g, h, i, j, Right.unit) { b, c, d, e, f, g, h, i, j, _ -> map(b, c, d, e, f, g, h, i, j) } + either { map(bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind(), h.bind(), i.bind(), j.bind()) } public inline fun Either.zip( c: Either, @@ -1687,28 +1626,9 @@ public inline fun Either.zip( j: Either, k: Either, map: (B, C, D, E, F, G, H, I, J, K) -> L -): Either = - flatMap { bb -> - c.flatMap { cc -> - d.flatMap { dd -> - e.flatMap { ee -> - f.flatMap { ff -> - g.flatMap { gg -> - h.flatMap { hh -> - i.flatMap { ii -> - j.flatMap { jj -> - k.map { kk -> - map(bb, cc, dd, ee, ff, gg, hh, ii, jj, kk) - } - } - } - } - } - } - } - } - } - } +): Either = either { + map(bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind(), h.bind(), i.bind(), j.bind(), k.bind()) +} public fun Either.replicate(n: Int, MB: Monoid): Either = if (n <= 0) MB.empty().right() @@ -1720,16 +1640,14 @@ public fun Either.replicate(n: Int, MB: Monoid): Either = } public inline fun Either.ensure(error: () -> A, predicate: (B) -> Boolean): Either = - when (this) { - is Right -> if (predicate(this.value)) this else error().left() - is Left -> this + either { + val b = bind() + ensure(predicate(b), error) + b } public inline fun Either.redeemWith(fa: (A) -> Either, fb: (B) -> Either): Either = - when (this) { - is Left -> fa(this.value) - is Right -> fb(this.value) - } + either { fold({ fa(it).bind() }, { fb(it).bind() }) } public fun Either>.sequence(): List> = traverse(::identity) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt index cb80b32a1b0..06abb84dc15 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Builders.kt @@ -22,13 +22,13 @@ import kotlin.jvm.JvmName public inline fun either(@BuilderInference block: Shift.() -> A): Either = fold({ block.invoke(this) }, { Either.Left(it) }, { Either.Right(it) }) -public inline fun nullable(@BuilderInference block: NullableShift.() -> A): A? = +public inline fun nullable(block: NullableShift.() -> A): A? = fold({ block(NullableShift(this)) }, { null }, ::identity) -public inline fun result(@BuilderInference action: ResultShift.() -> A): Result = +public inline fun result(action: ResultShift.() -> A): Result = fold({ action(ResultShift(this)) }, Result.Companion::failure, Result.Companion::success) -public inline fun option(@BuilderInference action: OptionShift.() -> A): Option = +public inline fun option(action: OptionShift.() -> A): Option = fold({ action(OptionShift(this)) }, ::identity, ::Some) public inline fun ior(semigroup: Semigroup, @BuilderInference action: IorShift.() -> A): Ior = diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt index 69e7284549d..caf04792607 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt @@ -9,18 +9,15 @@ import kotlin.experimental.ExperimentalTypeInference import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName -@BuilderInference -public infix fun Effect.catch(resolve: suspend Shift.(shifted: E) -> A): Effect = +public infix fun Effect.catch(@BuilderInference resolve: suspend Shift.(shifted: E) -> A): Effect = effect { catch(resolve) } -@BuilderInference -public infix fun Effect.attempt(recover: suspend Shift.(throwable: Throwable) -> A): Effect = +public infix fun Effect.attempt(@BuilderInference recover: suspend Shift.(throwable: Throwable) -> A): Effect = effect { attempt(recover) } -@BuilderInference @JvmName("attemptOrThrow") public inline infix fun Effect.attempt( - crossinline recover: suspend Shift.(T) -> A, + @BuilderInference crossinline recover: suspend Shift.(T) -> A, ): Effect = effect { attempt { t: Throwable -> if (t is T) recover(t) else throw t } } @@ -33,12 +30,10 @@ public fun Effect.attempt(): Effect> = } } -@BuilderInference -public infix fun EagerEffect.catch(resolve: Shift.(shifted: E) -> A): EagerEffect = +public infix fun EagerEffect.catch(@BuilderInference resolve: Shift.(shifted: E) -> A): EagerEffect = eagerEffect { catch(resolve) } -@BuilderInference -public infix fun EagerEffect.attempt(recover: Shift.(throwable: Throwable) -> A): EagerEffect = +public infix fun EagerEffect.attempt(@BuilderInference recover: Shift.(throwable: Throwable) -> A): EagerEffect = eagerEffect { attempt(recover) } @JvmName("attemptOrThrow") diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt index a73c477cd8f..51df17899a3 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt @@ -5,7 +5,9 @@ import arrow.core.identity import arrow.core.left import arrow.core.right import io.kotest.assertions.fail +import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.nulls.shouldNotBeNull import io.kotest.matchers.shouldBe import io.kotest.property.Arb import io.kotest.property.arbitrary.boolean @@ -19,7 +21,7 @@ import kotlinx.coroutines.CompletableDeferred class EagerEffectSpec : StringSpec({ "try/catch - can recover from shift" { checkAll(Arb.int(), Arb.string()) { i, s -> - eagerEffect { + eagerEffect { try { shift(s) } catch (e: Throwable) { @@ -28,13 +30,13 @@ class EagerEffectSpec : StringSpec({ }.fold({ fail("Should never come here") }, ::identity) shouldBe i } } - + "try/catch - finally works" { checkAll(Arb.string(), Arb.int()) { s, i -> val promise = CompletableDeferred() - eagerEffect { + eagerEffect { try { - shift(s) + shift(s) } finally { require(promise.complete(i)) } @@ -43,7 +45,7 @@ class EagerEffectSpec : StringSpec({ promise.await() shouldBe i } } - + "try/catch - First shift is ignored and second is returned" { checkAll(Arb.int(), Arb.string(), Arb.string()) { i, s, s2 -> eagerEffect { @@ -57,7 +59,7 @@ class EagerEffectSpec : StringSpec({ }.fold(::identity) { fail("Should never come here") } shouldBe s2 } } - + "attempt - catch" { checkAll(Arb.int(), Arb.long()) { i, l -> eagerEffect { @@ -70,7 +72,7 @@ class EagerEffectSpec : StringSpec({ }.runCont() shouldBe i } } - + "attempt - no catch" { checkAll(Arb.int(), Arb.long()) { i, l -> eagerEffect { @@ -83,16 +85,29 @@ class EagerEffectSpec : StringSpec({ }.runCont() shouldBe i } } - - "immediate values" { eagerEffect { 1 }.toEither().orNull() shouldBe 1 } - - "immediate short-circuit" { eagerEffect { shift("hello") }.runCont() shouldBe "hello" } - - "Rethrows immediate exceptions" { + + "attempt - shift from catch" { + checkAll(Arb.int(), Arb.long(), Arb.string()) { i, l, error -> + eagerEffect { + eagerEffect { + shift(l) + } catch { ll -> + ll shouldBe l + shift(error) + } + }.runCont() shouldBe error + } + } + + "values" { eagerEffect { 1 }.toEither().orNull() shouldBe 1 } + + "short-circuit" { eagerEffect { shift("hello") }.runCont() shouldBe "hello" } + + "Rethrows exceptions" { val e = RuntimeException("test") Either.catch { eagerEffect { throw e }.runCont() } shouldBe Either.Left(e) } - + "ensure null in eager either computation" { checkAll(Arb.boolean(), Arb.int(), Arb.string()) { predicate, success, shift -> either { @@ -101,10 +116,10 @@ class EagerEffectSpec : StringSpec({ } shouldBe if (predicate) success.right() else shift.left() } } - + "ensureNotNull in eager either computation" { fun square(i: Int): Int = i * i - + checkAll(Arb.int().orNull(), Arb.string()) { i: Int?, shift: String -> val res = either { @@ -116,20 +131,83 @@ class EagerEffectSpec : StringSpec({ res shouldBe expected } } - - "low-level use-case: distinguish between concurrency error and shift exception" { - val effect = eagerEffect { shift("Shift") } - val e = RuntimeException("test") - Either.catch { - eagerEffect { - try { - effect() - } catch (eagerShiftError: ShiftCancellationException) { - throw e - } catch (otherError: Throwable) { - fail("Should never come here") - } - }.runCont() - } shouldBe Either.Left(e) + + "catch - happy path" { + checkAll(Arb.string()) { str -> + eagerEffect { + str + }.catch { fail("It should never catch a success value") } + .runCont() shouldBe str + } + } + + "catch - error path and recover" { + checkAll(Arb.int(), Arb.string()) { int, fallback -> + eagerEffect { + shift(int) + fail("It should never reach this point") + }.catch { fallback } + .runCont() shouldBe fallback + } + } + + "catch - error path and re-shift" { + checkAll(Arb.int(), Arb.string()) { int, fallback -> + eagerEffect { + shift(int) + fail("It should never reach this point") + }.catch { shift(fallback) } + .runCont() shouldBe fallback + } + } + + "catch - error path and throw" { + checkAll(Arb.int(), Arb.string()) { int, msg -> + shouldThrow { + eagerEffect { + shift(int) + fail("It should never reach this point") + }.catch { throw RuntimeException(msg) } + .runCont() + }.message.shouldNotBeNull() shouldBe msg + } + } + + "attempt - happy path" { + checkAll(Arb.string()) { str -> + eagerEffect { + str + }.attempt { fail("It should never catch a success value") } + .runCont() shouldBe str + } + } + + "attempt - error path and recover" { + checkAll(Arb.string(), Arb.string()) { msg, fallback -> + eagerEffect { + throw RuntimeException(msg) + }.attempt { fallback } + .runCont() shouldBe fallback + } + } + + "attempt - error path and re-shift" { + checkAll(Arb.string(), Arb.int()) { msg, fallback -> + eagerEffect { + throw RuntimeException(msg) + }.attempt { shift(fallback) } + .runCont() shouldBe fallback + } + } + + "attempt - error path and throw" { + checkAll(Arb.string(), Arb.string()) { msg, msg2 -> + shouldThrow { + eagerEffect { + throw RuntimeException(msg) + }.attempt { throw RuntimeException(msg2) } + .runCont() + }.message.shouldNotBeNull() shouldBe msg2 + } } }) diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt index a7a3a6d774f..b1cd7d609bd 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt @@ -7,6 +7,7 @@ import arrow.core.right import io.kotest.assertions.fail import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.StringSpec +import io.kotest.matchers.nulls.shouldNotBeNull import io.kotest.matchers.shouldBe import io.kotest.property.Arb import io.kotest.property.arbitrary.arbitrary @@ -30,254 +31,181 @@ import kotlinx.coroutines.withContext class EffectSpec : StringSpec({ "try/catch - can recover from shift" { - checkAll(Arb.int(), Arb.string()) { i, s -> + checkAll(Arb.int().suspend(), Arb.string().suspend()) { i, s -> effect { try { - shift(s) + shift(s()) } catch (e: Throwable) { - i + i() } - }.fold({ fail("Should never come here") }, ::identity) shouldBe i + }.fold({ fail("Should never come here") }, ::identity) shouldBe i() } } - - "try/catch - can recover from shift suspended" { - checkAll(Arb.int(), Arb.string()) { i, s -> - effect { - try { - shift(s.suspend()) - } catch (e: Throwable) { - i - } - }.fold({ fail("Should never come here") }, ::identity) shouldBe i - } - } - + "try/catch - finally works" { - checkAll(Arb.string(), Arb.int()) { s, i -> - val promise = CompletableDeferred() - effect { - try { - shift(s) - } finally { - require(promise.complete(i)) - } - } - .fold(::identity) { fail("Should never come here") } shouldBe s - promise.await() shouldBe i - } - } - - "try/catch - finally works suspended" { - checkAll(Arb.string(), Arb.int()) { s, i -> + checkAll(Arb.string().suspend(), Arb.int().suspend()) { s, i -> val promise = CompletableDeferred() - effect { + effect { try { - shift(s.suspend()) + shift(s().suspend()) } finally { - require(promise.complete(i)) + require(promise.complete(i())) } } - .fold(::identity) { fail("Should never come here") } shouldBe s - promise.await() shouldBe i + .fold(::identity) { fail("Should never come here") } shouldBe s() + promise.await() shouldBe i() } } - + "try/catch - First shift is ignored and second is returned" { - checkAll(Arb.int(), Arb.string(), Arb.string()) { i, s, s2 -> + checkAll(Arb.int().suspend(), Arb.string().suspend(), Arb.string().suspend()) { i, s, s2 -> effect { - val x: Int = - try { - shift(s) - } catch (e: Throwable) { - i - } - shift(s2) - } - .fold(::identity) { fail("Should never come here") } shouldBe s2 - } - } - - "try/catch - First shift is ignored and second is returned suspended" { - checkAll(Arb.int(), Arb.string(), Arb.string()) { i, s, s2 -> - effect { - val x: Int = - try { - shift(s.suspend()) - } catch (e: Throwable) { - i - } - shift(s2.suspend()) + val x: Int = try { + shift(s()) + } catch (e: Throwable) { + i() + } + shift(s2()) } - .fold(::identity) { fail("Should never come here") } shouldBe s2 + .fold(::identity) { fail("Should never come here") } shouldBe s2() } } - + "attempt - catch" { - checkAll(Arb.int(), Arb.long()) { i, l -> + checkAll(Arb.int().suspend(), Arb.long().suspend()) { i, l -> effect { effect { - shift(l) + shift(l()) } catch { ll -> - ll shouldBe l - i + ll shouldBe l() + i() } - }.runCont() shouldBe i + }.runCont() shouldBe i() } } - + "attempt - no catch" { - checkAll(Arb.int(), Arb.long()) { i, l -> + checkAll(Arb.int().suspend(), Arb.long().suspend()) { i, l -> effect { effect { - i + i() } catch { ll -> - ll shouldBe l - i + 1 + ll shouldBe l() + i() + 1 } - }.runCont() shouldBe i + }.runCont() shouldBe i() } } - + "eagerEffect can be consumed within an Effect computation" { - checkAll(Arb.int(), Arb.int()) { a, b -> + checkAll(Arb.int(), Arb.int().suspend()) { a, b -> val eager: EagerEffect = eagerEffect { a } - + effect { + val bb = b() val aa = eager() - aa + b.suspend() - }.runCont() shouldBe (a + b) + aa + bb + }.runCont() shouldBe (a + b()) } } - + "eagerEffect shift short-circuits effect computation" { - checkAll(Arb.string(), Arb.int()) { a, b -> + checkAll(Arb.string(), Arb.int().suspend()) { a, b -> val eager: EagerEffect = eagerEffect { shift(a) } - + effect { + val bb = b() val aa = eager() - aa + b.suspend() + aa + bb }.runCont() shouldBe a } } - - "immediate values" { effect { 1 }.value() shouldBe 1 } - - "suspended value" { effect { 1.suspend() }.value() shouldBe 1 } - - "immediate short-circuit" { - effect { shift("hello") }.runCont() shouldBe "hello" - } - - "suspended short-circuit" { - effect { shift("hello".suspend()) }.runCont() shouldBe "hello" - } - - "Rethrows immediate exceptions" { - val e = RuntimeException("test") - Either.catch { effect { throw e }.runCont() } shouldBe Either.Left(e) + + "success" { + checkAll(Arb.int().suspend()) { i -> + effect { i() }.value() shouldBe i() + } } - - "Rethrows suspended exceptions" { - val e = RuntimeException("test") - Either.catch { effect { e.suspend() }.runCont() } shouldBe Either.Left(e) + + "short-circuit" { + checkAll(Arb.string().suspend()) { msg -> + effect { + shift(msg()) + }.runCont() shouldBe msg() + } } - - "Can short-circuit immediately from nested blocks" { - effect { - effect { shift("test") }.runCont() - fail("Should never reach this point") + + "Rethrows exceptions" { + checkAll(Arb.string().suspend()) { msg -> + shouldThrow { + effect { + throw RuntimeException(msg()) + }.toEither() + }.message shouldBe msg() } - .runCont() shouldBe "test" } - - "Can short-circuit suspended from nested blocks" { - effect { - effect { shift("test".suspend()) }.runCont() - fail("Should never reach this point") + + "Can short-circuit from nested blocks" { + checkAll(Arb.string().suspend()) { msg -> + effect { + effect { shift(msg()) }.runCont() + fail("Should never reach this point") + } + .runCont() shouldBe msg() } - .runCont() shouldBe "test" } - + "Can short-circuit immediately after suspending from nested blocks" { - effect { - effect { - 1L.suspend() - shift("test".suspend()) - } - .runCont() - fail("Should never reach this point") + checkAll(Arb.string().suspend()) { msg -> + effect { + effect { + 1L.suspend() + shift(msg()) + }.runCont() + fail("Should never reach this point") + }.runCont() shouldBe msg() } - .runCont() shouldBe "test" } - + "ensure null in either computation" { - checkAll(Arb.boolean(), Arb.int(), Arb.string()) { predicate, success, shift -> - either { - ensure(predicate) { shift } - success - } shouldBe if (predicate) success.right() else shift.left() + checkAll( + Arb.boolean().suspend(), + Arb.int().suspend(), + Arb.string().suspend() + ) { predicate, success, shift -> + either { + ensure(predicate()) { shift() } + success() + } shouldBe if (predicate()) success().right() else shift().left() } } - + "ensureNotNull in either computation" { fun square(i: Int): Int = i * i - - checkAll(Arb.int().orNull(), Arb.string()) { i: Int?, shift: String -> + + checkAll(Arb.int().orNull().suspend(), Arb.string().suspend()) { i, shift-> val res = either { - val ii = i - ensureNotNull(ii) { shift } + val ii = i() + ensureNotNull(ii) { shift() } square(ii) // Smart-cast by contract } - val expected = i?.let(::square)?.right() ?: shift.left() + val expected = i()?.let(::square)?.right() ?: shift().left() res shouldBe expected } } - - "low-level use-case: distinguish between concurrency error and shift exception" { - val effect = effect { shift("Shift") } - val e = RuntimeException("test") - Either.catch { - effect { - try { - effect() - } catch (shiftError: ShiftCancellationException) { - e.suspend() - } catch (otherError: Throwable) { - fail("Should never come here") - } - }.runCont() - } shouldBe Either.Left(e) - } - - "low-level use-case: eager shift exception within effect computations doesn't change shift exception" { - val effect = eagerEffect { shift("Shift") } - val e = RuntimeException("test") - Either.catch { - effect { - try { - effect() - } catch (eagerShiftError: ShiftCancellationException) { - e.suspend() - } catch (otherError: Throwable) { - fail("Should never come here") - } - }.runCont() - } shouldBe Either.Left(e) - } "#2760 - dispatching in nested Effect blocks does not make the nested Continuation to hang" { checkAll(Arb.string()) { msg -> - val failure: Effect = effect { + fun failure(): Effect = effect { withContext(Dispatchers.Default) {} shift(Failure(msg)) } effect { - failure() + failure().bind() 1 }.fold( recover = { it }, @@ -285,19 +213,19 @@ class EffectSpec : ) shouldBe Failure(msg) } } - + "#2779 - handleErrorWith does not make nested Continuations hang" { checkAll(Arb.string()) { error -> val failed: Effect = effect { withContext(Dispatchers.Default) {} shift(error) } - + val newError: Effect, Int> = failed.catch { str -> shift(str.reversed().toList()) } - + newError.toEither() shouldBe Either.Left(error.reversed().toList()) } } @@ -308,16 +236,16 @@ class EffectSpec : withContext(Dispatchers.Default) {} shift(error) } - + val newError: Effect, Int> = effect { failed.fold({ r -> effect, Int> { shift(r.reversed().toList()) - }.invoke() + }.bind() }, ::identity) } - + newError.toEither() shouldBe Either.Left(error.reversed().toList()) } } @@ -336,7 +264,7 @@ class EffectSpec : "Can shift from thrown exceptions" { checkAll(Arb.string().suspend(), Arb.string().suspend()) { msg, fallback -> - effect { + effect { effect { throw RuntimeException(msg()) }.fold( @@ -347,7 +275,7 @@ class EffectSpec : }.runCont() shouldBe fallback() } } - + "Can throw from thrown exceptions" { checkAll(Arb.string().suspend(), Arb.string().suspend()) { msg, fallback -> shouldThrow { @@ -361,6 +289,85 @@ class EffectSpec : }.message shouldBe fallback() } } + + "catch - happy path" { + checkAll(Arb.string().suspend()) { str -> + effect { + str() + }.catch { fail("It should never catch a success value") } + .runCont() shouldBe str() + } + } + + "catch - error path and recover" { + checkAll(Arb.int().suspend(), Arb.string().suspend()) { int, fallback -> + effect { + shift(int()) + fail("It should never reach this point") + }.catch { fallback() } + .runCont() shouldBe fallback() + } + } + + "catch - error path and re-shift" { + checkAll(Arb.int().suspend(), Arb.string().suspend()) { int, fallback -> + effect { + shift(int()) + fail("It should never reach this point") + }.catch { shift(fallback()) } + .runCont() shouldBe fallback() + } + } + + "catch - error path and throw" { + checkAll(Arb.int().suspend(), Arb.string().suspend()) { int, msg -> + shouldThrow { + effect { + shift(int()) + fail("It should never reach this point") + }.catch { throw RuntimeException(msg()) } + .runCont() + }.message.shouldNotBeNull() shouldBe msg() + } + } + + "attempt - happy path" { + checkAll(Arb.string().suspend()) { str -> + effect { + str() + }.attempt { fail("It should never catch a success value") } + .runCont() shouldBe str() + } + } + + "attempt - error path and recover" { + checkAll(Arb.string().suspend(), Arb.string().suspend()) { msg, fallback -> + effect { + throw RuntimeException(msg()) + }.attempt { fallback() } + .runCont() shouldBe fallback() + } + } + + "attempt - error path and re-shift" { + checkAll(Arb.string().suspend(), Arb.int().suspend()) { msg, fallback -> + effect { + throw RuntimeException(msg()) + }.attempt { shift(fallback()) } + .runCont() shouldBe fallback() + } + } + + "attempt - error path and throw" { + checkAll(Arb.string().suspend(), Arb.string().suspend()) { msg, msg2 -> + shouldThrow { + effect { + throw RuntimeException(msg()) + }.attempt { throw RuntimeException(msg2()) } + .runCont() + }.message.shouldNotBeNull() shouldBe msg2() + } + } }) private data class Failure(val msg: String) @@ -379,13 +386,13 @@ private fun Arb.suspend(): Arb A> = internal suspend fun Throwable.suspend(): Nothing = suspendCoroutineUninterceptedOrReturn { cont -> suspend { throw this } .startCoroutine(Continuation(Dispatchers.Default) { cont.intercepted().resumeWith(it) }) - + COROUTINE_SUSPENDED } internal suspend fun A.suspend(): A = suspendCoroutineUninterceptedOrReturn { cont -> suspend { this } .startCoroutine(Continuation(Dispatchers.Default) { cont.intercepted().resumeWith(it) }) - + COROUTINE_SUSPENDED } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt index 00971be0e26..43e405810d5 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt @@ -1,13 +1,10 @@ // This file was automatically generated from Effect.kt by Knit tool. Do not edit. package arrow.core.examples.exampleEffect04 -import arrow.core.Either import arrow.core.continuations.Effect import arrow.core.continuations.effect import arrow.core.continuations.catch import arrow.core.continuations.attempt -import arrow.core.continuations.toEither -import io.kotest.matchers.shouldBe val failed: Effect = effect { shift("failed") } From 4f0d629a0760bd584ebdd5e9ce367cceec609f01 Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Wed, 17 Aug 2022 09:21:36 +0200 Subject: [PATCH 08/20] Re-add some docs, and missing functions --- .../arrow/core/continuations/ErrorHandlers.kt | 68 +++++++++++++++++++ .../kotlin/arrow/core/continuations/Fold.kt | 34 +++++++--- .../arrow/core/continuations/Mappers.kt | 38 +++++------ .../examples/example-effect-error-01.kt | 14 ++++ .../examples/example-effect-error-02.kt | 14 ++++ .../examples/example-effect-error-03.kt | 16 +++++ 6 files changed, 156 insertions(+), 28 deletions(-) create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-01.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-02.kt create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-03.kt diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt index caf04792607..91a0e7e2827 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt @@ -9,18 +9,86 @@ import kotlin.experimental.ExperimentalTypeInference import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName +/** + * Catch the shifted value [E] of the `Effect`. + * You can either return a value a new value of [A], + * or short-circuit the effect by shifting with a value of [E], + * or raise an exception into [suspend]. + * + * ```kotlin + * import arrow.core.continuations.effect + * import arrow.core.continuations.catch + * + * object User + * object Error + * + * val error = effect { shift(Error) } // // Shift(error) + * + * val a = error.catch { error -> User } // Success(User) + * val b = error.catch { error -> shift("other-failure") } // Shift(other-failure) + * val c = error.catch { error -> throw RuntimeException("BOOM") } // Exception(BOOM) + * ``` + * + */ public infix fun Effect.catch(@BuilderInference resolve: suspend Shift.(shifted: E) -> A): Effect = effect { catch(resolve) } +/** + * Attempt to run the effect, and [recover] from any unexpected exceptions. + * You can either return a value a new value of [A], + * or short-circuit the effect by shifting with a value of [E], + * or raise an exception into [suspend]. + * + * ```kotlin + * import arrow.core.continuations.effect + * import arrow.core.continuations.attempt + * + * object User + * object Error + * + * val exception = effect { throw RuntimeException("BOOM") } // Exception(BOOM) + * + * val a = exception.attempt { error -> error.message?.length ?: -1 } // Success(5) + * val b = exception.attempt { shift(Error) } // Shift(error) + * val c = exception.attempt { throw RuntimeException("other-failure") } // Exception(other-failure) + * ``` + * + */ public infix fun Effect.attempt(@BuilderInference recover: suspend Shift.(throwable: Throwable) -> A): Effect = effect { attempt(recover) } +/** + * A version of [attempt] that refines the [Throwable] to [T]. + * This is useful for wrapping foreign code, such as database, network calls, etc. + * + * ```kotlin + * import arrow.core.continuations.effect + * import arrow.core.continuations.attempt + * + * object User + * object Error + * + * val x = effect { + * throw IllegalArgumentException("builder missed args") + * }.attempt { shift(Error) } + * ``` + * + * If you don't need an `error` value when wrapping your foreign code you can use `Nothing` to fill the type parameter. + * + * ```kotlin + * val y = effect { + * throw IllegalArgumentException("builder missed args") + * }.attempt { shift(Error) } + * ``` + * + */ @JvmName("attemptOrThrow") public inline infix fun Effect.attempt( @BuilderInference crossinline recover: suspend Shift.(T) -> A, ): Effect = effect { attempt { t: Throwable -> if (t is T) recover(t) else throw t } } +/** Runs the [Effect] and captures any [nonFatalOrThrow] exception into [Result]. */ public fun Effect.attempt(): Effect> = effect { try { diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt index a0cf008e02d..3718c15570e 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt @@ -10,16 +10,25 @@ import kotlin.experimental.ExperimentalTypeInference import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName +/** + * `invoke` the [Effect] and [fold] the result: + * - _success_ [transform] result of [A] to a value of [B]. + * - _shift_ [recover] from `shifted` value of [R] to a value of [B]. + * - _exception_ [error] from [Throwable] by transforming value into [B]. + * + * This method should never be wrapped in `try`/`catch` as it will not throw any unexpected errors, + * it will only result in [CancellationException], or fatal exceptions such as `OutOfMemoryError`. + */ public suspend fun Effect.fold( + error: suspend (error: Throwable) -> B, recover: suspend (shifted: R) -> B, transform: suspend (value: A) -> B, -): B = fold({ throw it }, recover, transform) +): B = fold({ invoke() }, { error(it) }, { recover(it) }, { transform(it) }) public suspend fun Effect.fold( - error: suspend (error: Throwable) -> B, recover: suspend (shifted: R) -> B, transform: suspend (value: A) -> B, -): B = fold({ invoke() }, { error(it) }, { recover(it) }, { transform(it) }) +): B = fold({ throw it }, recover, transform) public fun EagerEffect.fold(recover: (R) -> B, transform: (A) -> B): B = fold({ throw it }, recover, transform) @@ -48,20 +57,29 @@ public inline fun fold( return try { transform(program(shift)) } catch (e: ShiftCancellationException) { - @Suppress("UNCHECKED_CAST") - if (shift === e.shift) recover(e.shifted as R) else throw e + if (e.checkScope(shift)) recover(e.shifted()) else throw e } catch (e: Throwable) { error(e.nonFatalOrThrow()) } } +/** + * **AVOID USING THIS TYPE, it's meant for low-level cancellation code** When in need in low-level code, + * you can use this type to differentiate between a foreign [CancellationException] such as JobCancellationException, and the one from [Effect]. + */ public class ShiftCancellationException( + private val _shifted: Any?, + private val shift: Shift, +) : CancellationException("Shifted Continuation") { + @Suppress("UNCHECKED_CAST") @PublishedApi - internal val shifted: Any?, + internal fun shifted(): R = _shifted as R + @PublishedApi - internal val shift: Shift, -) : CancellationException("Shifted Continuation") + internal fun checkScope(other: Shift): Boolean = shift === other +} +/** Serves as both purposes of a scope-reference token, and a default implementation for Shift. */ @PublishedApi internal class DefaultShift : Shift { override fun shift(r: Any?): B = throw ShiftCancellationException(r, this) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt index b11d8fd9be7..9df1bb9e3b6 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Mappers.kt @@ -12,36 +12,34 @@ import arrow.core.Validated import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName +/** Run the [Effect] by returning [Either.Right] of [A], or [Either.Left] of [E]. */ public suspend fun Effect.toEither(): Either = either { invoke() } - public fun EagerEffect.toEither(): Either = either { invoke() } -public suspend fun Effect.toValidated(): Validated = - fold({ Validated.Invalid(it) }) { Validated.Valid(it) } - -public fun EagerEffect.toValidated(): Validated = - fold({ Validated.Invalid(it) }) { Validated.Valid(it) } - -public suspend fun Effect.toIor(): Ior = - fold({ Ior.Left(it) }) { Ior.Right(it) } +/** Run the [Effect] by returning [Validated.Valid] of [A], or [Validated.Invalid] of [E]. */ +public suspend fun Effect.toValidated(): Validated = fold({ Validated.Invalid(it) }) { Validated.Valid(it) } +public fun EagerEffect.toValidated(): Validated = fold({ Validated.Invalid(it) }) { Validated.Valid(it) } -public fun EagerEffect.toIor(): Ior = - fold({ Ior.Left(it) }) { Ior.Right(it) } +/** Run the [Effect] by returning [Ior.Right] of [A], or [Ior.Left] of [E]. */ +public suspend fun Effect.toIor(): Ior = fold({ Ior.Left(it) }) { Ior.Right(it) } +public fun EagerEffect.toIor(): Ior = fold({ Ior.Left(it) }) { Ior.Right(it) } +/** Run the [Effect] by returning [A], or `null` if shifted with [E]. */ public suspend fun Effect.orNull(): A? = fold({ _: E -> null }) { it } - public fun EagerEffect.orNull(): A? = fold({ _: E -> null }) { it } -public suspend fun Effect.toOption(): Option = option { invoke() } +/** Run the [Effect] by returning [Option] of [A], [orElse] run the fallback lambda and returning its result of [Option] of [A]. */ +public suspend fun Effect.toOption(orElse: suspend (E) -> Option): Option = fold(orElse) { Some(it) } +public fun EagerEffect.toOption(orElse: (E) -> Option): Option = fold(orElse) { Some(it) } +/** Run the [Effect] by returning [Option] of [A], or [None] if shifted with [None]. */ +public suspend fun Effect.toOption(): Option = option { invoke() } public fun EagerEffect.toOption(): Option = option { invoke() } -public suspend fun Effect.toResult(): Result = result { invoke() } +/** Run the [Effect] by returning [Result] of [A], [orElse] run the fallback lambda and returning its result of [Result] of [A]. */ +public suspend fun Effect.toResult(orElse: suspend (E) -> Result): Result = fold({ orElse(it) }, { Result.success(it) }) +public fun EagerEffect.toResult(orElse: (E) -> Result): Result = fold({ orElse(it) }, { Result.success(it) }) +/** Run the [Effect] by returning [Result] of [A], or [Result.Failure] if shifted with [Throwable]. */ +public suspend fun Effect.toResult(): Result = result { invoke() } public fun EagerEffect.toResult(): Result = result { invoke() } - -public suspend fun Effect.toOption(orElse: suspend (E) -> Option): Option = - fold(orElse) { Some(it) } - -public fun EagerEffect.toOption(orElse: (E) -> Option): Option = - fold(orElse) { Some(it) } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-01.kt new file mode 100644 index 00000000000..c8790b02c6b --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-01.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from ErrorHandlers.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffectError01 + +import arrow.core.continuations.effect +import arrow.core.continuations.catch + +object User +object Error + +val error = effect { shift(Error) } // // Shift(error) + +val a = error.catch { error -> User } // Success(User) +val b = error.catch { error -> shift("other-failure") } // Shift(other-failure) +val c = error.catch { error -> throw RuntimeException("BOOM") } // Exception(BOOM) diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-02.kt new file mode 100644 index 00000000000..e7fafac8216 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-02.kt @@ -0,0 +1,14 @@ +// This file was automatically generated from ErrorHandlers.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffectError02 + +import arrow.core.continuations.effect +import arrow.core.continuations.attempt + +object User +object Error + +val exception = effect { throw RuntimeException("BOOM") } // Exception(BOOM) + +val a = exception.attempt { error -> error.message?.length ?: -1 } // Success(5) +val b = exception.attempt { shift(Error) } // Shift(error) +val c = exception.attempt { throw RuntimeException("other-failure") } // Exception(other-failure) diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-03.kt new file mode 100644 index 00000000000..e7f65707717 --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-03.kt @@ -0,0 +1,16 @@ +// This file was automatically generated from ErrorHandlers.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEffectError03 + +import arrow.core.continuations.effect +import arrow.core.continuations.attempt + +object User +object Error + +val x = effect { + throw IllegalArgumentException("builder missed args") +}.attempt { shift(Error) } + +val y = effect { + throw IllegalArgumentException("builder missed args") +}.attempt { shift(Error) } From 18e5b54fd1946ca7ba7d2cc6717ba4a94a93cfa3 Mon Sep 17 00:00:00 2001 From: nomisRev Date: Wed, 17 Aug 2022 07:47:56 +0000 Subject: [PATCH 09/20] Update API files --- arrow-libs/core/arrow-core/api/arrow-core.api | 691 +++++++----------- 1 file changed, 245 insertions(+), 446 deletions(-) diff --git a/arrow-libs/core/arrow-core/api/arrow-core.api b/arrow-libs/core/arrow-core/api/arrow-core.api index 7ae8ccad9bc..02bf7974dd8 100644 --- a/arrow-libs/core/arrow-core/api/arrow-core.api +++ b/arrow-libs/core/arrow-core/api/arrow-core.api @@ -244,6 +244,7 @@ public final class arrow/core/EitherKt { public static final fun bisequenceNullable (Larrow/core/Either;)Larrow/core/Either; public static final fun bisequenceOption (Larrow/core/Either;)Larrow/core/Option; public static final fun bisequenceValidated (Larrow/core/Either;)Larrow/core/Validated; + public static final fun catch (Larrow/core/Either;Lkotlin/jvm/functions/Function2;)Larrow/core/Either; public static final fun combine (Larrow/core/Either;Larrow/typeclasses/Semigroup;Larrow/typeclasses/Semigroup;Larrow/core/Either;)Larrow/core/Either; public static final fun combineAll (Ljava/lang/Iterable;Larrow/typeclasses/Monoid;Larrow/typeclasses/Monoid;)Larrow/core/Either; public static final fun combineK (Larrow/core/Either;Larrow/core/Either;)Larrow/core/Either; @@ -2584,473 +2585,271 @@ public final class arrow/core/continuations/AtomicRefKt { public static final fun updateAndGet (Ljava/util/concurrent/atomic/AtomicReference;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; } -public final class arrow/core/continuations/Eager : arrow/core/continuations/ShiftCancellationException { - public fun (Larrow/core/continuations/Token;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)V - public final fun getRecover ()Lkotlin/jvm/functions/Function1; - public final fun getShifted ()Ljava/lang/Object; - public final fun getToken ()Larrow/core/continuations/Token; - public fun toString ()Ljava/lang/String; -} - -public abstract interface class arrow/core/continuations/EagerEffect { - public abstract fun attempt ()Larrow/core/continuations/EagerEffect; - public abstract fun flatMap (Lkotlin/jvm/functions/Function1;)Larrow/core/continuations/EagerEffect; - public abstract fun fold (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; - public abstract fun fold (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; - public abstract fun handleError (Lkotlin/jvm/functions/Function1;)Larrow/core/continuations/EagerEffect; - public abstract fun handleErrorWith (Lkotlin/jvm/functions/Function1;)Larrow/core/continuations/EagerEffect; - public abstract fun map (Lkotlin/jvm/functions/Function1;)Larrow/core/continuations/EagerEffect; - public abstract fun orNull ()Ljava/lang/Object; - public abstract fun redeem (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Larrow/core/continuations/EagerEffect; - public abstract fun redeemWith (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Larrow/core/continuations/EagerEffect; - public abstract fun toEither ()Larrow/core/Either; - public abstract fun toIor ()Larrow/core/Ior; - public abstract fun toOption (Lkotlin/jvm/functions/Function1;)Larrow/core/Option; - public abstract fun toValidated ()Larrow/core/Validated; -} - -public final class arrow/core/continuations/EagerEffect$DefaultImpls { - public static fun attempt (Larrow/core/continuations/EagerEffect;)Larrow/core/continuations/EagerEffect; - public static fun flatMap (Larrow/core/continuations/EagerEffect;Lkotlin/jvm/functions/Function1;)Larrow/core/continuations/EagerEffect; - public static fun fold (Larrow/core/continuations/EagerEffect;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; - public static fun handleError (Larrow/core/continuations/EagerEffect;Lkotlin/jvm/functions/Function1;)Larrow/core/continuations/EagerEffect; - public static fun handleErrorWith (Larrow/core/continuations/EagerEffect;Lkotlin/jvm/functions/Function1;)Larrow/core/continuations/EagerEffect; - public static fun map (Larrow/core/continuations/EagerEffect;Lkotlin/jvm/functions/Function1;)Larrow/core/continuations/EagerEffect; - public static fun orNull (Larrow/core/continuations/EagerEffect;)Ljava/lang/Object; - public static fun redeem (Larrow/core/continuations/EagerEffect;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Larrow/core/continuations/EagerEffect; - public static fun redeemWith (Larrow/core/continuations/EagerEffect;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Larrow/core/continuations/EagerEffect; - public static fun toEither (Larrow/core/continuations/EagerEffect;)Larrow/core/Either; - public static fun toIor (Larrow/core/continuations/EagerEffect;)Larrow/core/Ior; - public static fun toOption (Larrow/core/continuations/EagerEffect;Lkotlin/jvm/functions/Function1;)Larrow/core/Option; - public static fun toValidated (Larrow/core/continuations/EagerEffect;)Larrow/core/Validated; -} - -public final class arrow/core/continuations/EagerEffectKt { - public static final fun eagerEffect (Lkotlin/jvm/functions/Function2;)Larrow/core/continuations/EagerEffect; - public static final fun merge (Larrow/core/continuations/EagerEffect;)Ljava/lang/Object; -} - -public abstract interface class arrow/core/continuations/EagerEffectScope { - public abstract fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public abstract fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun shift (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/EagerEffectScope$DefaultImpls { - public static fun attempt (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind (Larrow/core/continuations/EagerEffectScope;Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind (Larrow/core/continuations/EagerEffectScope;Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind (Larrow/core/continuations/EagerEffectScope;Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind (Larrow/core/continuations/EagerEffectScope;Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun catch (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun ensure (Larrow/core/continuations/EagerEffectScope;ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/EagerEffectScopeKt { - public static final fun ensureNotNull (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public abstract interface class arrow/core/continuations/Effect { - public abstract fun attempt ()Larrow/core/continuations/Effect; - public abstract fun fold (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun fold (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun handleError (Lkotlin/jvm/functions/Function2;)Larrow/core/continuations/Effect; - public abstract fun handleErrorWith (Lkotlin/jvm/functions/Function2;)Larrow/core/continuations/Effect; - public abstract fun orNull (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun redeem (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Larrow/core/continuations/Effect; - public abstract fun redeemWith (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Larrow/core/continuations/Effect; - public abstract fun toEither (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun toIor (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun toOption (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun toValidated (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/Effect$DefaultImpls { - public static fun attempt (Larrow/core/continuations/Effect;)Larrow/core/continuations/Effect; - public static fun fold (Larrow/core/continuations/Effect;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun handleError (Larrow/core/continuations/Effect;Lkotlin/jvm/functions/Function2;)Larrow/core/continuations/Effect; - public static fun handleErrorWith (Larrow/core/continuations/Effect;Lkotlin/jvm/functions/Function2;)Larrow/core/continuations/Effect; - public static fun orNull (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun redeem (Larrow/core/continuations/Effect;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Larrow/core/continuations/Effect; - public static fun redeemWith (Larrow/core/continuations/Effect;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Larrow/core/continuations/Effect; - public static fun toEither (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun toIor (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun toOption (Larrow/core/continuations/Effect;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun toValidated (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; +public final class arrow/core/continuations/DefaultShift : arrow/core/continuations/Shift { + public fun ()V + public fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun bind (Larrow/core/Either;)Ljava/lang/Object; + public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun bind (Larrow/core/Validated;)Ljava/lang/Object; + public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun bind (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun invoke (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun shift (Ljava/lang/Object;)Ljava/lang/Object; +} + +public final class arrow/core/continuations/Effect { + public static final fun _fold (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static final fun _foldOrThrow (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static final fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function1; + public static final fun attempt (Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function2; + public static final fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;)Lkotlin/jvm/functions/Function2; + public static final fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function1; + public static final fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;)Lkotlin/jvm/functions/Function2; + public static final fun either (Lkotlin/jvm/functions/Function1;)Larrow/core/Either; + public static final fun fold (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static final fun fold (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static final fun fold (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun fold (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun ior (Larrow/typeclasses/Semigroup;Lkotlin/jvm/functions/Function1;)Larrow/core/Ior; + public static final fun nullable (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static final fun option (Lkotlin/jvm/functions/Function1;)Larrow/core/Option; + public static final fun orNull (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static final fun orNull (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun result (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static final fun toEither (Lkotlin/jvm/functions/Function1;)Larrow/core/Either; + public static final fun toEither (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun toIor (Lkotlin/jvm/functions/Function1;)Larrow/core/Ior; + public static final fun toIor (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun toOption (Lkotlin/jvm/functions/Function1;)Larrow/core/Option; + public static final fun toOption (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Larrow/core/Option; + public static final fun toOption (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun toOption (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun toResult (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static final fun toResult (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static final fun toResult (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun toResult (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun toValidated (Lkotlin/jvm/functions/Function1;)Larrow/core/Validated; + public static final fun toValidated (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; +} + +public abstract interface annotation class arrow/core/continuations/EffectDSL : java/lang/annotation/Annotation { } public final class arrow/core/continuations/EffectKt { - public static final fun effect (Lkotlin/jvm/functions/Function2;)Larrow/core/continuations/Effect; - public static final fun merge (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public abstract interface class arrow/core/continuations/EffectScope { - public abstract fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun bind (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun shift (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/EffectScope$DefaultImpls { - public static fun attempt (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind (Larrow/core/continuations/EffectScope;Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind (Larrow/core/continuations/EffectScope;Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind (Larrow/core/continuations/EffectScope;Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind (Larrow/core/continuations/EffectScope;Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind (Larrow/core/continuations/EffectScope;Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun catch (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun ensure (Larrow/core/continuations/EffectScope;ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/EffectScopeKt { - public static final fun ensureNotNull (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/FoldContinuation : arrow/core/continuations/Token, arrow/core/continuations/EffectScope, kotlin/coroutines/Continuation { - public field recover Lkotlin/jvm/functions/Function2; - public fun (Larrow/core/continuations/Token;Lkotlin/coroutines/CoroutineContext;Lkotlin/coroutines/Continuation;)V - public fun (Larrow/core/continuations/Token;Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)V - public fun (Lkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)V - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun eagerEffect (Lkotlin/jvm/functions/Function1;)Lkotlin/jvm/functions/Function1; + public static final fun effect (Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function2; +} + +public final class arrow/core/continuations/IorShift : arrow/core/continuations/Shift, arrow/typeclasses/Semigroup { + public fun (Larrow/typeclasses/Semigroup;Larrow/core/continuations/Shift;)V + public fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun bind (Larrow/core/Either;)Ljava/lang/Object; + public final fun bind (Larrow/core/Ior;)Ljava/lang/Object; + public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun bind (Larrow/core/Validated;)Ljava/lang/Object; + public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun bind (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun getContext ()Lkotlin/coroutines/CoroutineContext; - public final fun getRecover ()Lkotlin/jvm/functions/Function2; - public fun resumeWith (Ljava/lang/Object;)V - public final fun setRecover (Lkotlin/jvm/functions/Function2;)V - public fun shift (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/IorEagerEffectScope : arrow/core/continuations/EagerEffectScope, arrow/typeclasses/Semigroup { - public fun (Larrow/typeclasses/Semigroup;Larrow/core/continuations/EagerEffectScope;)V - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public final fun bind (Larrow/core/Ior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public fun combine (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getLeftState ()Ljava/util/concurrent/atomic/AtomicReference; + public final fun invoke (Lkotlin/jvm/functions/Function1;)Larrow/core/Ior; + public fun invoke (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun maybeCombine (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public final fun setLeftState (Ljava/util/concurrent/atomic/AtomicReference;)V - public fun shift (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/IorEffectScope : arrow/core/continuations/EffectScope, arrow/typeclasses/Semigroup { - public fun (Larrow/typeclasses/Semigroup;Larrow/core/continuations/EffectScope;)V - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public final fun bind (Larrow/core/Ior;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun shift (Ljava/lang/Object;)Ljava/lang/Object; +} + +public final class arrow/core/continuations/NullableShift : arrow/core/continuations/Shift { + public fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun attempt-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun attempt-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun bind (Larrow/core/Either;)Ljava/lang/Object; + public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun bind (Larrow/core/Validated;)Ljava/lang/Object; + public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun bind (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Larrow/core/Either;)Ljava/lang/Object; + public static final fun bind-impl (Larrow/core/continuations/Shift;Larrow/core/Option;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Larrow/core/Validated;)Ljava/lang/Object; + public static final fun bind-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final synthetic fun box-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/NullableShift; + public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun combine (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public final fun getLeftState ()Ljava/util/concurrent/atomic/AtomicReference; - public fun maybeCombine (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public final fun setLeftState (Ljava/util/concurrent/atomic/AtomicReference;)V - public fun shift (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/NullableEagerEffectScope : arrow/core/continuations/EagerEffectScope { - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun attempt-impl (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/Option;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun bind-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final synthetic fun box-impl (Larrow/core/continuations/EagerEffectScope;)Larrow/core/continuations/NullableEagerEffectScope; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun constructor-impl (Larrow/core/continuations/EagerEffectScope;)Larrow/core/continuations/EagerEffectScope; - public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun ensure-impl (Larrow/core/continuations/EagerEffectScope;ZLkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun ensure-impl (Larrow/core/continuations/EagerEffectScope;ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun constructor-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/Shift; + public static final fun ensure-impl (Larrow/core/continuations/Shift;Z)V + public static final fun ensureNotNull-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;)Ljava/lang/Object; public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;)Z - public static final fun equals-impl0 (Larrow/core/continuations/EagerEffectScope;Larrow/core/continuations/EagerEffectScope;)Z + public static fun equals-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;)Z + public static final fun equals-impl0 (Larrow/core/continuations/Shift;Larrow/core/continuations/Shift;)Z public fun hashCode ()I - public static fun hashCode-impl (Larrow/core/continuations/EagerEffectScope;)I - public synthetic fun shift (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun shift (Ljava/lang/Void;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun shift-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Void;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun hashCode-impl (Larrow/core/continuations/Shift;)I + public fun invoke (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun invoke-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static fun invoke-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public synthetic fun shift (Ljava/lang/Object;)Ljava/lang/Object; + public fun shift (Ljava/lang/Void;)Ljava/lang/Object; + public static fun shift-impl (Larrow/core/continuations/Shift;Ljava/lang/Void;)Ljava/lang/Object; public fun toString ()Ljava/lang/String; - public static fun toString-impl (Larrow/core/continuations/EagerEffectScope;)Ljava/lang/String; - public final synthetic fun unbox-impl ()Larrow/core/continuations/EagerEffectScope; -} - -public final class arrow/core/continuations/NullableEffectScope : arrow/core/continuations/EffectScope { - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun attempt-impl (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/Option;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun bind-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final synthetic fun box-impl (Larrow/core/continuations/EffectScope;)Larrow/core/continuations/NullableEffectScope; + public static fun toString-impl (Larrow/core/continuations/Shift;)Ljava/lang/String; + public final synthetic fun unbox-impl ()Larrow/core/continuations/Shift; +} + +public final class arrow/core/continuations/OptionShift : arrow/core/continuations/Shift { + public fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun attempt-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun attempt-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun bind (Larrow/core/Either;)Ljava/lang/Object; + public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun bind (Larrow/core/Validated;)Ljava/lang/Object; + public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun bind (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Larrow/core/Either;)Ljava/lang/Object; + public static final fun bind-impl (Larrow/core/continuations/Shift;Larrow/core/Option;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Larrow/core/Validated;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final synthetic fun box-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/OptionShift; + public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun constructor-impl (Larrow/core/continuations/EffectScope;)Larrow/core/continuations/EffectScope; - public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun ensure-impl (Larrow/core/continuations/EffectScope;ZLkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun ensure-impl (Larrow/core/continuations/EffectScope;ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun constructor-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/Shift; + public static final fun ensure-impl (Larrow/core/continuations/Shift;Z)V + public static final fun ensureNotNull-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;)Ljava/lang/Object; public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Object;)Z - public static final fun equals-impl0 (Larrow/core/continuations/EffectScope;Larrow/core/continuations/EffectScope;)Z + public static fun equals-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;)Z + public static final fun equals-impl0 (Larrow/core/continuations/Shift;Larrow/core/continuations/Shift;)Z public fun hashCode ()I - public static fun hashCode-impl (Larrow/core/continuations/EffectScope;)I - public synthetic fun shift (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun shift (Ljava/lang/Void;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun shift-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Void;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun hashCode-impl (Larrow/core/continuations/Shift;)I + public fun invoke (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun invoke-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static fun invoke-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun shift (Larrow/core/None;)Ljava/lang/Object; + public synthetic fun shift (Ljava/lang/Object;)Ljava/lang/Object; + public static fun shift-impl (Larrow/core/continuations/Shift;Larrow/core/None;)Ljava/lang/Object; public fun toString ()Ljava/lang/String; - public static fun toString-impl (Larrow/core/continuations/EffectScope;)Ljava/lang/String; - public final synthetic fun unbox-impl ()Larrow/core/continuations/EffectScope; -} - -public final class arrow/core/continuations/NullableKt { - public static final fun ensureNotNull-0Rsnnio (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun ensureNotNull-7s8y1X8 (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/OptionEagerEffectScope : arrow/core/continuations/EagerEffectScope { - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun attempt-impl (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/Option;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final synthetic fun box-impl (Larrow/core/continuations/EagerEffectScope;)Larrow/core/continuations/OptionEagerEffectScope; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun constructor-impl (Larrow/core/continuations/EagerEffectScope;)Larrow/core/continuations/EagerEffectScope; - public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun ensure-impl (Larrow/core/continuations/EagerEffectScope;ZLkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun ensure-impl (Larrow/core/continuations/EagerEffectScope;ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;)Z - public static final fun equals-impl0 (Larrow/core/continuations/EagerEffectScope;Larrow/core/continuations/EagerEffectScope;)Z - public fun hashCode ()I - public static fun hashCode-impl (Larrow/core/continuations/EagerEffectScope;)I - public fun shift (Larrow/core/None;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public synthetic fun shift (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun shift-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/None;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun toString ()Ljava/lang/String; - public static fun toString-impl (Larrow/core/continuations/EagerEffectScope;)Ljava/lang/String; - public final synthetic fun unbox-impl ()Larrow/core/continuations/EagerEffectScope; -} - -public final class arrow/core/continuations/OptionEffectScope : arrow/core/continuations/EffectScope { - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun attempt-impl (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/Option;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final synthetic fun box-impl (Larrow/core/continuations/EffectScope;)Larrow/core/continuations/OptionEffectScope; + public static fun toString-impl (Larrow/core/continuations/Shift;)Ljava/lang/String; + public final synthetic fun unbox-impl ()Larrow/core/continuations/Shift; +} + +public final class arrow/core/continuations/ResultShift : arrow/core/continuations/Shift { + public fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun attempt-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun attempt-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun bind (Larrow/core/Either;)Ljava/lang/Object; + public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun bind (Larrow/core/Validated;)Ljava/lang/Object; + public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun bind (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Larrow/core/Either;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Larrow/core/Validated;)Ljava/lang/Object; + public static final fun bind-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final synthetic fun box-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/ResultShift; + public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun constructor-impl (Larrow/core/continuations/EffectScope;)Larrow/core/continuations/EffectScope; - public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun ensure-impl (Larrow/core/continuations/EffectScope;ZLkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun ensure-impl (Larrow/core/continuations/EffectScope;ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Object;)Z - public static final fun equals-impl0 (Larrow/core/continuations/EffectScope;Larrow/core/continuations/EffectScope;)Z - public fun hashCode ()I - public static fun hashCode-impl (Larrow/core/continuations/EffectScope;)I - public fun shift (Larrow/core/None;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public synthetic fun shift (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun shift-impl (Larrow/core/continuations/EffectScope;Larrow/core/None;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun toString ()Ljava/lang/String; - public static fun toString-impl (Larrow/core/continuations/EffectScope;)Ljava/lang/String; - public final synthetic fun unbox-impl ()Larrow/core/continuations/EffectScope; -} - -public final class arrow/core/continuations/OptionKt { - public static final fun ensureNotNull-09sQPHg (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun ensureNotNull-dxZa7OQ (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun toOption (Larrow/core/continuations/EagerEffect;)Larrow/core/Option; - public static final fun toOption (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/ResultEagerEffectScope : arrow/core/continuations/EagerEffectScope { - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun attempt-impl (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun bind-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final synthetic fun box-impl (Larrow/core/continuations/EagerEffectScope;)Larrow/core/continuations/ResultEagerEffectScope; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/EagerEffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun constructor-impl (Larrow/core/continuations/EagerEffectScope;)Larrow/core/continuations/EagerEffectScope; - public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun ensure-impl (Larrow/core/continuations/EagerEffectScope;ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun constructor-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/Shift; public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Object;)Z - public static final fun equals-impl0 (Larrow/core/continuations/EagerEffectScope;Larrow/core/continuations/EagerEffectScope;)Z + public static fun equals-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;)Z + public static final fun equals-impl0 (Larrow/core/continuations/Shift;Larrow/core/continuations/Shift;)Z public fun hashCode ()I - public static fun hashCode-impl (Larrow/core/continuations/EagerEffectScope;)I - public synthetic fun shift (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun shift (Ljava/lang/Throwable;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun shift-impl (Larrow/core/continuations/EagerEffectScope;Ljava/lang/Throwable;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun hashCode-impl (Larrow/core/continuations/Shift;)I + public fun invoke (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun invoke-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static fun invoke-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public synthetic fun shift (Ljava/lang/Object;)Ljava/lang/Object; + public fun shift (Ljava/lang/Throwable;)Ljava/lang/Object; + public static fun shift-impl (Larrow/core/continuations/Shift;Ljava/lang/Throwable;)Ljava/lang/Object; public fun toString ()Ljava/lang/String; - public static fun toString-impl (Larrow/core/continuations/EagerEffectScope;)Ljava/lang/String; - public final synthetic fun unbox-impl ()Larrow/core/continuations/EagerEffectScope; -} - -public final class arrow/core/continuations/ResultEffectScope : arrow/core/continuations/EffectScope { - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun attempt-impl (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/Either;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/Option;Lkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/Validated;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/continuations/EagerEffect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun bind-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun bind-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final synthetic fun box-impl (Larrow/core/continuations/EffectScope;)Larrow/core/continuations/ResultEffectScope; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/EffectScope;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun constructor-impl (Larrow/core/continuations/EffectScope;)Larrow/core/continuations/EffectScope; - public fun ensure (ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun ensure-impl (Larrow/core/continuations/EffectScope;ZLkotlin/jvm/functions/Function0;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun equals (Ljava/lang/Object;)Z - public static fun equals-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Object;)Z - public static final fun equals-impl0 (Larrow/core/continuations/EffectScope;Larrow/core/continuations/EffectScope;)Z - public fun hashCode ()I - public static fun hashCode-impl (Larrow/core/continuations/EffectScope;)I - public synthetic fun shift (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun shift (Ljava/lang/Throwable;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun shift-impl (Larrow/core/continuations/EffectScope;Ljava/lang/Throwable;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun toString ()Ljava/lang/String; - public static fun toString-impl (Larrow/core/continuations/EffectScope;)Ljava/lang/String; - public final synthetic fun unbox-impl ()Larrow/core/continuations/EffectScope; -} - -public final class arrow/core/continuations/ResultKt { - public static final fun toResult (Larrow/core/continuations/EagerEffect;)Ljava/lang/Object; - public static final fun toResult (Larrow/core/continuations/Effect;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public abstract class arrow/core/continuations/ShiftCancellationException : java/util/concurrent/CancellationException { -} - -public final class arrow/core/continuations/Suspend : arrow/core/continuations/ShiftCancellationException { - public fun (Larrow/core/continuations/Token;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)V - public final fun getRecover ()Lkotlin/jvm/functions/Function2; - public final fun getShifted ()Ljava/lang/Object; - public final fun getToken ()Larrow/core/continuations/Token; - public fun toString ()Ljava/lang/String; -} - -public class arrow/core/continuations/Token { - public fun ()V - public fun toString ()Ljava/lang/String; -} - -public final class arrow/core/continuations/either { - public static final field INSTANCE Larrow/core/continuations/either; - public final fun eager (Lkotlin/jvm/functions/Function2;)Larrow/core/Either; - public final fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/ior { - public static final field INSTANCE Larrow/core/continuations/ior; - public final fun eager (Larrow/typeclasses/Semigroup;Lkotlin/jvm/functions/Function2;)Larrow/core/Ior; - public final fun invoke (Larrow/typeclasses/Semigroup;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/nullable { - public static final field INSTANCE Larrow/core/continuations/nullable; - public final fun eager (Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public final fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/option { - public static final field INSTANCE Larrow/core/continuations/option; - public final fun eager (Lkotlin/jvm/functions/Function2;)Larrow/core/Option; - public final fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; -} - -public final class arrow/core/continuations/result { - public static final field INSTANCE Larrow/core/continuations/result; - public final fun eager-IoAF18A (Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public final fun invoke-gIAlu-s (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun toString-impl (Larrow/core/continuations/Shift;)Ljava/lang/String; + public final synthetic fun unbox-impl ()Larrow/core/continuations/Shift; +} + +public abstract interface class arrow/core/continuations/Shift { + public abstract fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public abstract fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun bind (Larrow/core/Either;)Ljava/lang/Object; + public abstract fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public abstract fun bind (Larrow/core/Validated;)Ljava/lang/Object; + public abstract fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public abstract fun bind (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public abstract fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public abstract fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun invoke (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public abstract fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun shift (Ljava/lang/Object;)Ljava/lang/Object; +} + +public final class arrow/core/continuations/Shift$DefaultImpls { + public static fun attempt (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun attempt (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun bind (Larrow/core/continuations/Shift;Larrow/core/Either;)Ljava/lang/Object; + public static fun bind (Larrow/core/continuations/Shift;Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun bind (Larrow/core/continuations/Shift;Larrow/core/Validated;)Ljava/lang/Object; + public static fun bind (Larrow/core/continuations/Shift;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static fun bind (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static fun bind (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun invoke (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static fun invoke (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; +} + +public final class arrow/core/continuations/ShiftCancellationException : java/util/concurrent/CancellationException { + public fun (Ljava/lang/Object;Larrow/core/continuations/Shift;)V + public final fun checkScope (Larrow/core/continuations/Shift;)Z + public final fun shifted ()Ljava/lang/Object; +} + +public final class arrow/core/continuations/ShiftKt { + public static final fun attempt (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static final fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static final fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static final fun ensure (Larrow/core/continuations/Shift;ZLkotlin/jvm/functions/Function0;)V + public static final fun ensureNotNull (Larrow/core/continuations/Shift;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object; } public abstract interface class arrow/typeclasses/Monoid : arrow/typeclasses/Semigroup { From a5d40c443ec7b67cd434f6c709d30dc703f5eb0f Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Wed, 17 Aug 2022 09:57:38 +0200 Subject: [PATCH 10/20] Retrigger CI with updated API files From 83530826173a6238eb4603b503c61a656442f475 Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Tue, 30 Aug 2022 12:02:11 +0200 Subject: [PATCH 11/20] Rename catch to recover --- .../kotlin/arrow/core/continuations/Effect.kt | 32 +++++++++---------- .../arrow/core/continuations/ErrorHandlers.kt | 16 +++++----- .../kotlin/arrow/core/continuations/Shift.kt | 14 ++++---- .../core/continuations/EagerEffectSpec.kt | 14 ++++---- .../arrow/core/continuations/EffectSpec.kt | 14 ++++---- .../kotlin/examples/example-effect-04.kt | 12 +++---- .../examples/example-effect-error-01.kt | 8 ++--- 7 files changed, 55 insertions(+), 55 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt index b1936e95c97..4badd790e55 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt @@ -16,7 +16,7 @@ import kotlin.jvm.JvmMultifileClass * [Writing a program with Effect](#writing-a-program-with-effect) * [Handling errors](#handling-errors) - * [catch](#catch) + * [recover](#recover) * [attempt](#attempt) * [Structured Concurrency](#structured-concurrency) * [Arrow Fx Coroutines](#arrow-fx-coroutines) @@ -186,12 +186,12 @@ import kotlin.jvm.JvmMultifileClass * An Effect has 2 error channels: `Throwable` and `R` * There are two separate handlers to transform either of the error channels. * - * - `catch` to handle, and transform any error of type `R`. + * - `recover` to handle, and transform any error of type `R`. * - `attempt` to handle, and transform and error of type `Throwable`. * - * ### catch + * ### recover * - * `catch` handles the error of type `R`, + * `recover` handles the error of type `R`, * by providing a new value of type `A`, raising a different error of type `E`, or throwing an exception. * * Let's take a look at some examples: @@ -201,7 +201,7 @@ import kotlin.jvm.JvmMultifileClass * * ```kotlin @@ -209,14 +209,14 @@ import kotlin.jvm.JvmMultifileClass * effect { shift("failed") } * ``` * - * We can `catch` the failure, and resolve it by providing a default value of `-1` or the length of the `error: String`. + * We can `recover` the failure, and resolve it by providing a default value of `-1` or the length of the `error: String`. * * ```kotlin * val default: Effect = - * failed.catch { -1 } + * failed.recover { -1 } * * val resolved: Effect = - * failed.catch { it.length } + * failed.recover { it.length } * ``` * * As you can see the resulting `error` is now of type `Nothing`, since we did not raise any new errors. @@ -229,28 +229,28 @@ import kotlin.jvm.JvmMultifileClass * val resolved2: Effect = resolved * ``` * - * `catch` also allows us to _change_ the error type when we resolve the error of type `R`. + * `recover` also allows us to _change_ the error type when we resolve the error of type `R`. * Below we handle our error of `String` and turn it into `List` using `reversed().toList()`. * This is a powerful operation, since it allows us to transform our error types across boundaries or layers. * * ```kotlin * val newError: Effect, Int> = - * failed.catch { str -> + * failed.recover { str -> * shift(str.reversed().toList()) * } * ``` * - * Finally, since `catch` supports `suspend` we can safely call other `suspend` code and throw `Throwable` into the `suspend` system. + * Finally, since `recover` supports `suspend` we can safely call other `suspend` code and throw `Throwable` into the `suspend` system. * This is typically undesired, since you should prefer lifting `Throwable` into typed values of `R` to make them compile-time tracked. * * ```kotlin * val newException: Effect = - * failed.catch { str -> throw RuntimeException(str) } + * failed.recover { str -> throw RuntimeException(str) } * ``` * * ### attempt * - * `attempt` gives us the same powers as `catch`, but instead of resolving `R` we're recovering from any unexpected `Throwable`. + * `attempt` gives us the same powers as `recover`, but instead of resolving `R` we're recovering from any unexpected `Throwable`. * Unexpected, because the expectation is that all `Throwable` get turned into `R` unless it's a fatal/unexpected. * This operator is useful when you need to work/wrap foreign code, especially Java SDKs or any code that is heavily based on exceptions. * @@ -273,13 +273,13 @@ import kotlin.jvm.JvmMultifileClass * foreign.attempt { it.message?.length ?: -1 } * ``` * - * A big difference with `catch` is that `attempt` **cannot** change the error type of `R` because it doesn't resolve it, so it stays unchanged. - * You can however compose `catch`, and `attempt` to resolve the error type **and** recover the exception. + * A big difference with `recover` is that `attempt` **cannot** change the error type of `R` because it doesn't resolve it, so it stays unchanged. + * You can however compose `recover`, and `attempt` to resolve the error type **and** recover the exception. * * ```kotlin * val default4: Effect = * foreign - * .catch { -1 } + * .recover { -1 } * .attempt { -2 } * ``` * diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt index 91a0e7e2827..ab6a035cc6b 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt @@ -17,21 +17,21 @@ import kotlin.jvm.JvmName * * ```kotlin * import arrow.core.continuations.effect - * import arrow.core.continuations.catch + * import arrow.core.continuations.recover * * object User * object Error * * val error = effect { shift(Error) } // // Shift(error) * - * val a = error.catch { error -> User } // Success(User) - * val b = error.catch { error -> shift("other-failure") } // Shift(other-failure) - * val c = error.catch { error -> throw RuntimeException("BOOM") } // Exception(BOOM) + * val a = error.recover { error -> User } // Success(User) + * val b = error.recover { error -> shift("other-failure") } // Shift(other-failure) + * val c = error.recover { error -> throw RuntimeException("BOOM") } // Exception(BOOM) * ``` * */ -public infix fun Effect.catch(@BuilderInference resolve: suspend Shift.(shifted: E) -> A): Effect = - effect { catch(resolve) } +public infix fun Effect.recover(@BuilderInference resolve: suspend Shift.(shifted: E) -> A): Effect = + effect { recover(resolve) } /** * Attempt to run the effect, and [recover] from any unexpected exceptions. @@ -98,8 +98,8 @@ public fun Effect.attempt(): Effect> = } } -public infix fun EagerEffect.catch(@BuilderInference resolve: Shift.(shifted: E) -> A): EagerEffect = - eagerEffect { catch(resolve) } +public infix fun EagerEffect.recover(@BuilderInference resolve: Shift.(shifted: E) -> A): EagerEffect = + eagerEffect { recover(resolve) } public infix fun EagerEffect.attempt(@BuilderInference recover: Shift.(throwable: Throwable) -> A): EagerEffect = eagerEffect { attempt(recover) } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt index 19035079067..c97854c5030 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt @@ -47,15 +47,15 @@ public interface Shift { fold(::identity) { throwable -> shift(transform(throwable)) } @EffectDSL - public suspend infix fun Effect.catch(@BuilderInference resolve: suspend Shift.(E) -> A): A = - catch({ invoke() }) { resolve(it) } + public suspend infix fun Effect.recover(@BuilderInference resolve: suspend Shift.(E) -> A): A = + recover({ invoke() }) { resolve(it) } @EffectDSL - public infix fun EagerEffect.catch(@BuilderInference resolve: Shift.(E) -> A): A = - catch({ invoke() }, resolve) + public infix fun EagerEffect.recover(@BuilderInference resolve: Shift.(E) -> A): A = + recover({ invoke() }, resolve) @EffectDSL - public suspend fun Effect.catch( + public suspend fun Effect.recover( @BuilderInference action: suspend Shift.() -> A, @BuilderInference resolve: suspend Shift.(E) -> A, @BuilderInference recover: suspend Shift.(Throwable) -> A, @@ -73,13 +73,13 @@ public interface Shift { } @EffectDSL -public inline fun Shift.catch( +public inline fun Shift.recover( @BuilderInference action: Shift.() -> A, @BuilderInference resolve: Shift.(E) -> A ): A = fold({ action(this) }, { throw it }, { resolve(it) }, { it }) @EffectDSL -public inline fun Shift.catch( +public inline fun Shift.recover( @BuilderInference action: Shift.() -> A, @BuilderInference resolve: Shift.(E) -> A, @BuilderInference recover: Shift.(Throwable) -> A, diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt index 51df17899a3..5fccd072025 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt @@ -65,7 +65,7 @@ class EagerEffectSpec : StringSpec({ eagerEffect { eagerEffect { shift(l) - } catch { ll -> + } recover { ll -> ll shouldBe l i } @@ -78,7 +78,7 @@ class EagerEffectSpec : StringSpec({ eagerEffect { eagerEffect { i - } catch { ll -> + } recover { ll -> ll shouldBe l i + 1 } @@ -91,7 +91,7 @@ class EagerEffectSpec : StringSpec({ eagerEffect { eagerEffect { shift(l) - } catch { ll -> + } recover { ll -> ll shouldBe l shift(error) } @@ -136,7 +136,7 @@ class EagerEffectSpec : StringSpec({ checkAll(Arb.string()) { str -> eagerEffect { str - }.catch { fail("It should never catch a success value") } + }.recover { fail("It should never catch a success value") } .runCont() shouldBe str } } @@ -146,7 +146,7 @@ class EagerEffectSpec : StringSpec({ eagerEffect { shift(int) fail("It should never reach this point") - }.catch { fallback } + }.recover { fallback } .runCont() shouldBe fallback } } @@ -156,7 +156,7 @@ class EagerEffectSpec : StringSpec({ eagerEffect { shift(int) fail("It should never reach this point") - }.catch { shift(fallback) } + }.recover { shift(fallback) } .runCont() shouldBe fallback } } @@ -167,7 +167,7 @@ class EagerEffectSpec : StringSpec({ eagerEffect { shift(int) fail("It should never reach this point") - }.catch { throw RuntimeException(msg) } + }.recover { throw RuntimeException(msg) } .runCont() }.message.shouldNotBeNull() shouldBe msg } diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt index b1cd7d609bd..e8ad98239f6 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt @@ -76,7 +76,7 @@ class EffectSpec : effect { effect { shift(l()) - } catch { ll -> + } recover { ll -> ll shouldBe l() i() } @@ -89,7 +89,7 @@ class EffectSpec : effect { effect { i() - } catch { ll -> + } recover { ll -> ll shouldBe l() i() + 1 } @@ -222,7 +222,7 @@ class EffectSpec : } val newError: Effect, Int> = - failed.catch { str -> + failed.recover { str -> shift(str.reversed().toList()) } @@ -294,7 +294,7 @@ class EffectSpec : checkAll(Arb.string().suspend()) { str -> effect { str() - }.catch { fail("It should never catch a success value") } + }.recover { fail("It should never catch a success value") } .runCont() shouldBe str() } } @@ -304,7 +304,7 @@ class EffectSpec : effect { shift(int()) fail("It should never reach this point") - }.catch { fallback() } + }.recover { fallback() } .runCont() shouldBe fallback() } } @@ -314,7 +314,7 @@ class EffectSpec : effect { shift(int()) fail("It should never reach this point") - }.catch { shift(fallback()) } + }.recover { shift(fallback()) } .runCont() shouldBe fallback() } } @@ -325,7 +325,7 @@ class EffectSpec : effect { shift(int()) fail("It should never reach this point") - }.catch { throw RuntimeException(msg()) } + }.recover { throw RuntimeException(msg()) } .runCont() }.message.shouldNotBeNull() shouldBe msg() } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt index 43e405810d5..b2064406184 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt @@ -3,28 +3,28 @@ package arrow.core.examples.exampleEffect04 import arrow.core.continuations.Effect import arrow.core.continuations.effect -import arrow.core.continuations.catch +import arrow.core.continuations.recover import arrow.core.continuations.attempt val failed: Effect = effect { shift("failed") } val default: Effect = - failed.catch { -1 } + failed.recover { -1 } val resolved: Effect = - failed.catch { it.length } + failed.recover { it.length } val default2: Effect = default val resolved2: Effect = resolved val newError: Effect, Int> = - failed.catch { str -> + failed.recover { str -> shift(str.reversed().toList()) } val newException: Effect = - failed.catch { str -> throw RuntimeException(str) } + failed.recover { str -> throw RuntimeException(str) } val foreign = effect { throw RuntimeException("BOOM!") @@ -38,7 +38,7 @@ val resolved3: Effect = val default4: Effect = foreign - .catch { -1 } + .recover { -1 } .attempt { -2 } val default5: Effect = diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-01.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-01.kt index c8790b02c6b..47297a7f7b4 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-01.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-01.kt @@ -2,13 +2,13 @@ package arrow.core.examples.exampleEffectError01 import arrow.core.continuations.effect -import arrow.core.continuations.catch +import arrow.core.continuations.recover object User object Error val error = effect { shift(Error) } // // Shift(error) -val a = error.catch { error -> User } // Success(User) -val b = error.catch { error -> shift("other-failure") } // Shift(other-failure) -val c = error.catch { error -> throw RuntimeException("BOOM") } // Exception(BOOM) +val a = error.recover { error -> User } // Success(User) +val b = error.recover { error -> shift("other-failure") } // Shift(other-failure) +val c = error.recover { error -> throw RuntimeException("BOOM") } // Exception(BOOM) From 32ce2051f7624c20f7a77658cce6eda46d022857 Mon Sep 17 00:00:00 2001 From: nomisRev Date: Tue, 30 Aug 2022 10:06:26 +0000 Subject: [PATCH 12/20] Update API files --- arrow-libs/core/arrow-core/api/arrow-core.api | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/arrow-libs/core/arrow-core/api/arrow-core.api b/arrow-libs/core/arrow-core/api/arrow-core.api index 02bf7974dd8..7ff470515ed 100644 --- a/arrow-libs/core/arrow-core/api/arrow-core.api +++ b/arrow-libs/core/arrow-core/api/arrow-core.api @@ -2595,11 +2595,11 @@ public final class arrow/core/continuations/DefaultShift : arrow/core/continuati public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public fun bind (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun invoke (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun shift (Ljava/lang/Object;)Ljava/lang/Object; } @@ -2609,8 +2609,6 @@ public final class arrow/core/continuations/Effect { public static final fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function1; public static final fun attempt (Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function2; public static final fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;)Lkotlin/jvm/functions/Function2; - public static final fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function1; - public static final fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;)Lkotlin/jvm/functions/Function2; public static final fun either (Lkotlin/jvm/functions/Function1;)Larrow/core/Either; public static final fun fold (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static final fun fold (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; @@ -2621,6 +2619,8 @@ public final class arrow/core/continuations/Effect { public static final fun option (Lkotlin/jvm/functions/Function1;)Larrow/core/Option; public static final fun orNull (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static final fun orNull (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun recover (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function1; + public static final fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;)Lkotlin/jvm/functions/Function2; public static final fun result (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static final fun toEither (Lkotlin/jvm/functions/Function1;)Larrow/core/Either; public static final fun toEither (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; @@ -2657,9 +2657,6 @@ public final class arrow/core/continuations/IorShift : arrow/core/continuations/ public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public fun bind (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun combine (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public final fun getLeftState ()Ljava/util/concurrent/atomic/AtomicReference; public final fun invoke (Lkotlin/jvm/functions/Function1;)Larrow/core/Ior; @@ -2667,6 +2664,9 @@ public final class arrow/core/continuations/IorShift : arrow/core/continuations/ public fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun maybeCombine (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun setLeftState (Ljava/util/concurrent/atomic/AtomicReference;)V public fun shift (Ljava/lang/Object;)Ljava/lang/Object; } @@ -2691,12 +2691,6 @@ public final class arrow/core/continuations/NullableShift : arrow/core/continuat public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final synthetic fun box-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/NullableShift; - public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun constructor-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/Shift; public static final fun ensure-impl (Larrow/core/continuations/Shift;Z)V public static final fun ensureNotNull-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;)Ljava/lang/Object; @@ -2709,6 +2703,12 @@ public final class arrow/core/continuations/NullableShift : arrow/core/continuat public fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun invoke-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun invoke-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun recover-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun recover-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun recover-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public synthetic fun shift (Ljava/lang/Object;)Ljava/lang/Object; public fun shift (Ljava/lang/Void;)Ljava/lang/Object; public static fun shift-impl (Larrow/core/continuations/Shift;Ljava/lang/Void;)Ljava/lang/Object; @@ -2736,12 +2736,6 @@ public final class arrow/core/continuations/OptionShift : arrow/core/continuatio public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final synthetic fun box-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/OptionShift; - public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun constructor-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/Shift; public static final fun ensure-impl (Larrow/core/continuations/Shift;Z)V public static final fun ensureNotNull-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;)Ljava/lang/Object; @@ -2754,6 +2748,12 @@ public final class arrow/core/continuations/OptionShift : arrow/core/continuatio public fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun invoke-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun invoke-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun recover-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun recover-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun recover-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun shift (Larrow/core/None;)Ljava/lang/Object; public synthetic fun shift (Ljava/lang/Object;)Ljava/lang/Object; public static fun shift-impl (Larrow/core/continuations/Shift;Larrow/core/None;)Ljava/lang/Object; @@ -2781,12 +2781,6 @@ public final class arrow/core/continuations/ResultShift : arrow/core/continuatio public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final synthetic fun box-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/ResultShift; - public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun constructor-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/Shift; public fun equals (Ljava/lang/Object;)Z public static fun equals-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;)Z @@ -2797,6 +2791,12 @@ public final class arrow/core/continuations/ResultShift : arrow/core/continuatio public fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun invoke-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun invoke-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun recover-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun recover-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun recover-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public synthetic fun shift (Ljava/lang/Object;)Ljava/lang/Object; public fun shift (Ljava/lang/Throwable;)Ljava/lang/Object; public static fun shift-impl (Larrow/core/continuations/Shift;Ljava/lang/Throwable;)Ljava/lang/Object; @@ -2814,11 +2814,11 @@ public abstract interface class arrow/core/continuations/Shift { public abstract fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun bind (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public abstract fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public abstract fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun invoke (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun recover (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public abstract fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun shift (Ljava/lang/Object;)Ljava/lang/Object; } @@ -2831,11 +2831,11 @@ public final class arrow/core/continuations/Shift$DefaultImpls { public static fun bind (Larrow/core/continuations/Shift;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun invoke (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun invoke (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun recover (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun recover (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun recover (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } public final class arrow/core/continuations/ShiftCancellationException : java/util/concurrent/CancellationException { @@ -2846,10 +2846,10 @@ public final class arrow/core/continuations/ShiftCancellationException : java/ut public final class arrow/core/continuations/ShiftKt { public static final fun attempt (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static final fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static final fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public static final fun ensure (Larrow/core/continuations/Shift;ZLkotlin/jvm/functions/Function0;)V public static final fun ensureNotNull (Larrow/core/continuations/Shift;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object; + public static final fun recover (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static final fun recover (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; } public abstract interface class arrow/typeclasses/Monoid : arrow/typeclasses/Semigroup { From 5ccf0abb50779aedc097a00f47aeec4ae594543f Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Tue, 30 Aug 2022 12:32:30 +0200 Subject: [PATCH 13/20] Refactor attempt to catch, update knit --- .../kotlin/arrow/core/continuations/Effect.kt | 34 ++++++++-------- .../arrow/core/continuations/ErrorHandlers.kt | 40 +++++++++---------- .../kotlin/arrow/core/continuations/Shift.kt | 12 +++--- .../core/continuations/EagerEffectSpec.kt | 8 ++-- .../arrow/core/continuations/EffectSpec.kt | 8 ++-- .../kotlin/examples/example-effect-04.kt | 14 +++---- .../examples/example-effect-error-02.kt | 8 ++-- .../examples/example-effect-error-03.kt | 6 +-- 8 files changed, 65 insertions(+), 65 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt index 4badd790e55..18f1f15c791 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Effect.kt @@ -17,7 +17,7 @@ import kotlin.jvm.JvmMultifileClass * [Writing a program with Effect](#writing-a-program-with-effect) * [Handling errors](#handling-errors) * [recover](#recover) - * [attempt](#attempt) + * [catch](#catch) * [Structured Concurrency](#structured-concurrency) * [Arrow Fx Coroutines](#arrow-fx-coroutines) * [parZip](#parzip) @@ -187,7 +187,7 @@ import kotlin.jvm.JvmMultifileClass * There are two separate handlers to transform either of the error channels. * * - `recover` to handle, and transform any error of type `R`. - * - `attempt` to handle, and transform and error of type `Throwable`. + * - `catch` to handle, and transform and error of type `Throwable`. * * ### recover * @@ -202,7 +202,7 @@ import kotlin.jvm.JvmMultifileClass * import arrow.core.continuations.Effect * import arrow.core.continuations.effect * import arrow.core.continuations.recover - * import arrow.core.continuations.attempt + * import arrow.core.continuations.catch * --> * ```kotlin * val failed: Effect = @@ -248,9 +248,9 @@ import kotlin.jvm.JvmMultifileClass * failed.recover { str -> throw RuntimeException(str) } * ``` * - * ### attempt + * ### catch * - * `attempt` gives us the same powers as `recover`, but instead of resolving `R` we're recovering from any unexpected `Throwable`. + * `catch` gives us the same powers as `recover`, but instead of resolving `R` we're recovering from any unexpected `Throwable`. * Unexpected, because the expectation is that all `Throwable` get turned into `R` unless it's a fatal/unexpected. * This operator is useful when you need to work/wrap foreign code, especially Java SDKs or any code that is heavily based on exceptions. * @@ -262,40 +262,40 @@ import kotlin.jvm.JvmMultifileClass * } * ``` * - * We can `attempt` to run the effect recovering from any exception, + * We can `catch` to run the effect recovering from any exception, * and recover it by providing a default value of `-1` or the length of the [Throwable.message]. * * ```kotlin * val default3: Effect = - * foreign.attempt { -1 } + * foreign.catch { -1 } * * val resolved3: Effect = - * foreign.attempt { it.message?.length ?: -1 } + * foreign.catch { it.message?.length ?: -1 } * ``` * - * A big difference with `recover` is that `attempt` **cannot** change the error type of `R` because it doesn't resolve it, so it stays unchanged. - * You can however compose `recover`, and `attempt` to resolve the error type **and** recover the exception. + * A big difference with `recover` is that `catch` **cannot** change the error type of `R` because it doesn't resolve it, so it stays unchanged. + * You can however compose `recover`, and `v` to resolve the error type **and** recover the exception. * * ```kotlin * val default4: Effect = * foreign * .recover { -1 } - * .attempt { -2 } + * .catch { -2 } * ``` * - * `attempt` however offers an overload that can _refine the exception_. + * `catch` however offers an overload that can _refine the exception_. * Let's say you're wrapping some database interactions that might throw `java.sql.SqlException`, or `org.postgresql.util.PSQLException`, - * then you might only be interested in those exceptions and not `Throwable`. `attempt` allows you to install multiple handlers for specific exceptions. + * then you might only be interested in those exceptions and not `Throwable`. `catch` allows you to install multiple handlers for specific exceptions. * If the desired exception is not matched, then it stays in the `suspend` exception channel and will be thrown or recovered at a later point. * * ```kotlin * val default5: Effect = * foreign - * .attempt { ex: RuntimeException -> -1 } - * .attempt { ex: java.sql.SQLException -> -2 } + * .catch { ex: RuntimeException -> -1 } + * .catch { ex: java.sql.SQLException -> -2 } * ``` * - * Finally, since `attempt` also supports `suspend` we can safely call other `suspend` code and throw `Throwable` into the `suspend` system. + * Finally, since `catch` also supports `suspend` we can safely call other `suspend` code and throw `Throwable` into the `suspend` system. * This can be useful if refinement of exceptions is not sufficient, for example in the case of `org.postgresql.util.PSQLException` you might want to * check the `SQLState` to check for a `foreign key violation` and rethrow the exception if not matched. * @@ -303,7 +303,7 @@ import kotlin.jvm.JvmMultifileClass * suspend fun java.sql.SQLException.isForeignKeyViolation(): Boolean = true * * val rethrown: Effect = - * failed.attempt { ex: java.sql.SQLException -> + * failed.catch { ex: java.sql.SQLException -> * if(ex.isForeignKeyViolation()) shift("foreign key violation") * else throw ex * } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt index ab6a035cc6b..4368a7a1f2b 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt @@ -34,43 +34,43 @@ public infix fun Effect.recover(@BuilderInference resolve: susp effect { recover(resolve) } /** - * Attempt to run the effect, and [recover] from any unexpected exceptions. + * Catch any unexpected exceptions, and [resolve] them. * You can either return a value a new value of [A], * or short-circuit the effect by shifting with a value of [E], * or raise an exception into [suspend]. * * ```kotlin * import arrow.core.continuations.effect - * import arrow.core.continuations.attempt + * import arrow.core.continuations.catch * * object User * object Error * * val exception = effect { throw RuntimeException("BOOM") } // Exception(BOOM) * - * val a = exception.attempt { error -> error.message?.length ?: -1 } // Success(5) - * val b = exception.attempt { shift(Error) } // Shift(error) - * val c = exception.attempt { throw RuntimeException("other-failure") } // Exception(other-failure) + * val a = exception.catch { error -> error.message?.length ?: -1 } // Success(5) + * val b = exception.catch { shift(Error) } // Shift(error) + * val c = exception.catch { throw RuntimeException("other-failure") } // Exception(other-failure) * ``` * */ -public infix fun Effect.attempt(@BuilderInference recover: suspend Shift.(throwable: Throwable) -> A): Effect = - effect { attempt(recover) } +public infix fun Effect.catch(@BuilderInference resolve: suspend Shift.(throwable: Throwable) -> A): Effect = + effect { catch(resolve) } /** - * A version of [attempt] that refines the [Throwable] to [T]. + * A version of [catch] that refines the [Throwable] to [T]. * This is useful for wrapping foreign code, such as database, network calls, etc. * * ```kotlin * import arrow.core.continuations.effect - * import arrow.core.continuations.attempt + * import arrow.core.continuations.catch * * object User * object Error * * val x = effect { * throw IllegalArgumentException("builder missed args") - * }.attempt { shift(Error) } + * }.catch { shift(Error) } * ``` * * If you don't need an `error` value when wrapping your foreign code you can use `Nothing` to fill the type parameter. @@ -78,18 +78,18 @@ public infix fun Effect.attempt(@BuilderInference recover: suspend * ```kotlin * val y = effect { * throw IllegalArgumentException("builder missed args") - * }.attempt { shift(Error) } + * }.catch { shift(Error) } * ``` * */ -@JvmName("attemptOrThrow") -public inline infix fun Effect.attempt( +@JvmName("catchOrThrow") +public inline infix fun Effect.catch( @BuilderInference crossinline recover: suspend Shift.(T) -> A, ): Effect = - effect { attempt { t: Throwable -> if (t is T) recover(t) else throw t } } + effect { catch { t: Throwable -> if (t is T) recover(t) else throw t } } /** Runs the [Effect] and captures any [nonFatalOrThrow] exception into [Result]. */ -public fun Effect.attempt(): Effect> = +public fun Effect.catch(): Effect> = effect { try { Result.success(invoke()) @@ -101,11 +101,11 @@ public fun Effect.attempt(): Effect> = public infix fun EagerEffect.recover(@BuilderInference resolve: Shift.(shifted: E) -> A): EagerEffect = eagerEffect { recover(resolve) } -public infix fun EagerEffect.attempt(@BuilderInference recover: Shift.(throwable: Throwable) -> A): EagerEffect = - eagerEffect { attempt(recover) } +public infix fun EagerEffect.catch(@BuilderInference recover: Shift.(throwable: Throwable) -> A): EagerEffect = + eagerEffect { catch(recover) } -@JvmName("attemptOrThrow") -public inline infix fun EagerEffect.attempt( +@JvmName("catchOrThrow") +public inline infix fun EagerEffect.catch( @BuilderInference crossinline recover: Shift.(T) -> A, ): EagerEffect = - eagerEffect { attempt { t: Throwable -> if (t is T) recover(t) else throw t } } + eagerEffect { catch { t: Throwable -> if (t is T) recover(t) else throw t } } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt index c97854c5030..a906d90b9fc 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt @@ -62,12 +62,12 @@ public interface Shift { ): A = fold({ action(this) }, { recover(it) }, { resolve(it) }, { it }) @EffectDSL - public suspend fun Effect.attempt( + public suspend fun Effect.catch( @BuilderInference recover: suspend Shift.(Throwable) -> A, ): A = fold({ recover(it) }, { shift(it) }, { it }) @EffectDSL - public fun EagerEffect.attempt( + public fun EagerEffect.catch( @BuilderInference recover: Shift.(Throwable) -> A, ): A = fold({ recover(it) }, { shift(it) }, { it }) } @@ -86,17 +86,17 @@ public inline fun Shift.recover( ): A = fold({ action(this) }, { recover(it) }, { resolve(it) }, { it }) @EffectDSL -public inline fun Shift.attempt( +public inline fun Shift.catch( @BuilderInference action: Shift.() -> A, @BuilderInference recover: Shift.(Throwable) -> A, ): A = fold({ action(this) }, { recover(it) }, { shift(it) }, { it }) @EffectDSL -@JvmName("attemptOrThrow") -public inline fun Shift.attempt( +@JvmName("catchOrThrow") +public inline fun Shift.catch( @BuilderInference action: Shift.() -> A, @BuilderInference recover: Shift.(T) -> A, -): A = attempt(action) { t: Throwable -> if (t is T) recover(t) else throw t } +): A = catch(action) { t: Throwable -> if (t is T) recover(t) else throw t } @EffectDSL public inline fun Shift.ensure(condition: Boolean, shift: () -> R): Unit = diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt index 5fccd072025..9dcee7f3610 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EagerEffectSpec.kt @@ -177,7 +177,7 @@ class EagerEffectSpec : StringSpec({ checkAll(Arb.string()) { str -> eagerEffect { str - }.attempt { fail("It should never catch a success value") } + }.catch { fail("It should never catch a success value") } .runCont() shouldBe str } } @@ -186,7 +186,7 @@ class EagerEffectSpec : StringSpec({ checkAll(Arb.string(), Arb.string()) { msg, fallback -> eagerEffect { throw RuntimeException(msg) - }.attempt { fallback } + }.catch { fallback } .runCont() shouldBe fallback } } @@ -195,7 +195,7 @@ class EagerEffectSpec : StringSpec({ checkAll(Arb.string(), Arb.int()) { msg, fallback -> eagerEffect { throw RuntimeException(msg) - }.attempt { shift(fallback) } + }.catch { shift(fallback) } .runCont() shouldBe fallback } } @@ -205,7 +205,7 @@ class EagerEffectSpec : StringSpec({ shouldThrow { eagerEffect { throw RuntimeException(msg) - }.attempt { throw RuntimeException(msg2) } + }.catch { throw RuntimeException(msg2) } .runCont() }.message.shouldNotBeNull() shouldBe msg2 } diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt index e8ad98239f6..79679cde04c 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/continuations/EffectSpec.kt @@ -335,7 +335,7 @@ class EffectSpec : checkAll(Arb.string().suspend()) { str -> effect { str() - }.attempt { fail("It should never catch a success value") } + }.catch { fail("It should never catch a success value") } .runCont() shouldBe str() } } @@ -344,7 +344,7 @@ class EffectSpec : checkAll(Arb.string().suspend(), Arb.string().suspend()) { msg, fallback -> effect { throw RuntimeException(msg()) - }.attempt { fallback() } + }.catch { fallback() } .runCont() shouldBe fallback() } } @@ -353,7 +353,7 @@ class EffectSpec : checkAll(Arb.string().suspend(), Arb.int().suspend()) { msg, fallback -> effect { throw RuntimeException(msg()) - }.attempt { shift(fallback()) } + }.catch { shift(fallback()) } .runCont() shouldBe fallback() } } @@ -363,7 +363,7 @@ class EffectSpec : shouldThrow { effect { throw RuntimeException(msg()) - }.attempt { throw RuntimeException(msg2()) } + }.catch { throw RuntimeException(msg2()) } .runCont() }.message.shouldNotBeNull() shouldBe msg2() } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt index b2064406184..add17cff915 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-04.kt @@ -4,7 +4,7 @@ package arrow.core.examples.exampleEffect04 import arrow.core.continuations.Effect import arrow.core.continuations.effect import arrow.core.continuations.recover -import arrow.core.continuations.attempt +import arrow.core.continuations.catch val failed: Effect = effect { shift("failed") } @@ -31,25 +31,25 @@ val foreign = effect { } val default3: Effect = - foreign.attempt { -1 } + foreign.catch { -1 } val resolved3: Effect = - foreign.attempt { it.message?.length ?: -1 } + foreign.catch { it.message?.length ?: -1 } val default4: Effect = foreign .recover { -1 } - .attempt { -2 } + .catch { -2 } val default5: Effect = foreign - .attempt { ex: RuntimeException -> -1 } - .attempt { ex: java.sql.SQLException -> -2 } + .catch { ex: RuntimeException -> -1 } + .catch { ex: java.sql.SQLException -> -2 } suspend fun java.sql.SQLException.isForeignKeyViolation(): Boolean = true val rethrown: Effect = - failed.attempt { ex: java.sql.SQLException -> + failed.catch { ex: java.sql.SQLException -> if(ex.isForeignKeyViolation()) shift("foreign key violation") else throw ex } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-02.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-02.kt index e7fafac8216..ae04eef943f 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-02.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-02.kt @@ -2,13 +2,13 @@ package arrow.core.examples.exampleEffectError02 import arrow.core.continuations.effect -import arrow.core.continuations.attempt +import arrow.core.continuations.catch object User object Error val exception = effect { throw RuntimeException("BOOM") } // Exception(BOOM) -val a = exception.attempt { error -> error.message?.length ?: -1 } // Success(5) -val b = exception.attempt { shift(Error) } // Shift(error) -val c = exception.attempt { throw RuntimeException("other-failure") } // Exception(other-failure) +val a = exception.catch { error -> error.message?.length ?: -1 } // Success(5) +val b = exception.catch { shift(Error) } // Shift(error) +val c = exception.catch { throw RuntimeException("other-failure") } // Exception(other-failure) diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-03.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-03.kt index e7f65707717..d1f053d3573 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-03.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-effect-error-03.kt @@ -2,15 +2,15 @@ package arrow.core.examples.exampleEffectError03 import arrow.core.continuations.effect -import arrow.core.continuations.attempt +import arrow.core.continuations.catch object User object Error val x = effect { throw IllegalArgumentException("builder missed args") -}.attempt { shift(Error) } +}.catch { shift(Error) } val y = effect { throw IllegalArgumentException("builder missed args") -}.attempt { shift(Error) } +}.catch { shift(Error) } From 424820dd17043c1b8bdf1b781a8d504e4d1b1cd0 Mon Sep 17 00:00:00 2001 From: nomisRev Date: Tue, 30 Aug 2022 10:36:14 +0000 Subject: [PATCH 14/20] Update API files --- arrow-libs/core/arrow-core/api/arrow-core.api | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/arrow-libs/core/arrow-core/api/arrow-core.api b/arrow-libs/core/arrow-core/api/arrow-core.api index 7ff470515ed..68204097621 100644 --- a/arrow-libs/core/arrow-core/api/arrow-core.api +++ b/arrow-libs/core/arrow-core/api/arrow-core.api @@ -2587,14 +2587,14 @@ public final class arrow/core/continuations/AtomicRefKt { public final class arrow/core/continuations/DefaultShift : arrow/core/continuations/Shift { public fun ()V - public fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Either;)Ljava/lang/Object; public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public fun bind (Larrow/core/Validated;)Ljava/lang/Object; public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public fun bind (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun invoke (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun recover (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; @@ -2606,9 +2606,9 @@ public final class arrow/core/continuations/DefaultShift : arrow/core/continuati public final class arrow/core/continuations/Effect { public static final fun _fold (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static final fun _foldOrThrow (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; - public static final fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function1; - public static final fun attempt (Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function2; - public static final fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;)Lkotlin/jvm/functions/Function2; + public static final fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function1; + public static final fun catch (Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function2; + public static final fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;)Lkotlin/jvm/functions/Function2; public static final fun either (Lkotlin/jvm/functions/Function1;)Larrow/core/Either; public static final fun fold (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static final fun fold (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; @@ -2648,8 +2648,6 @@ public final class arrow/core/continuations/EffectKt { public final class arrow/core/continuations/IorShift : arrow/core/continuations/Shift, arrow/typeclasses/Semigroup { public fun (Larrow/typeclasses/Semigroup;Larrow/core/continuations/Shift;)V - public fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Either;)Ljava/lang/Object; public final fun bind (Larrow/core/Ior;)Ljava/lang/Object; public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; @@ -2657,6 +2655,8 @@ public final class arrow/core/continuations/IorShift : arrow/core/continuations/ public fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public fun bind (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun combine (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public final fun getLeftState ()Ljava/util/concurrent/atomic/AtomicReference; public final fun invoke (Lkotlin/jvm/functions/Function1;)Larrow/core/Ior; @@ -2672,10 +2672,6 @@ public final class arrow/core/continuations/IorShift : arrow/core/continuations/ } public final class arrow/core/continuations/NullableShift : arrow/core/continuations/Shift { - public fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun attempt-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun attempt-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Either;)Ljava/lang/Object; public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public fun bind (Larrow/core/Validated;)Ljava/lang/Object; @@ -2691,6 +2687,10 @@ public final class arrow/core/continuations/NullableShift : arrow/core/continuat public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final synthetic fun box-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/NullableShift; + public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun constructor-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/Shift; public static final fun ensure-impl (Larrow/core/continuations/Shift;Z)V public static final fun ensureNotNull-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;)Ljava/lang/Object; @@ -2718,10 +2718,6 @@ public final class arrow/core/continuations/NullableShift : arrow/core/continuat } public final class arrow/core/continuations/OptionShift : arrow/core/continuations/Shift { - public fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun attempt-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun attempt-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Either;)Ljava/lang/Object; public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public fun bind (Larrow/core/Validated;)Ljava/lang/Object; @@ -2736,6 +2732,10 @@ public final class arrow/core/continuations/OptionShift : arrow/core/continuatio public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final synthetic fun box-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/OptionShift; + public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun constructor-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/Shift; public static final fun ensure-impl (Larrow/core/continuations/Shift;Z)V public static final fun ensureNotNull-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;)Ljava/lang/Object; @@ -2763,10 +2763,6 @@ public final class arrow/core/continuations/OptionShift : arrow/core/continuatio } public final class arrow/core/continuations/ResultShift : arrow/core/continuations/Shift { - public fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static fun attempt-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun attempt-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun bind (Larrow/core/Either;)Ljava/lang/Object; public fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public fun bind (Larrow/core/Validated;)Ljava/lang/Object; @@ -2781,6 +2777,10 @@ public final class arrow/core/continuations/ResultShift : arrow/core/continuatio public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun bind-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final synthetic fun box-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/ResultShift; + public fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun catch-impl (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun constructor-impl (Larrow/core/continuations/Shift;)Larrow/core/continuations/Shift; public fun equals (Ljava/lang/Object;)Z public static fun equals-impl (Larrow/core/continuations/Shift;Ljava/lang/Object;)Z @@ -2806,14 +2806,14 @@ public final class arrow/core/continuations/ResultShift : arrow/core/continuatio } public abstract interface class arrow/core/continuations/Shift { - public abstract fun attempt (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public abstract fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun bind (Larrow/core/Either;)Ljava/lang/Object; public abstract fun bind (Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public abstract fun bind (Larrow/core/Validated;)Ljava/lang/Object; public abstract fun bind (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun bind (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun bind (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public abstract fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public abstract fun catch (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun invoke (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public abstract fun recover (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; @@ -2823,14 +2823,14 @@ public abstract interface class arrow/core/continuations/Shift { } public final class arrow/core/continuations/Shift$DefaultImpls { - public static fun attempt (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; - public static fun attempt (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/Shift;Larrow/core/Either;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/Shift;Larrow/core/Option;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/Shift;Larrow/core/Validated;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/Shift;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun bind (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun invoke (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun invoke (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static fun recover (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; @@ -2845,7 +2845,7 @@ public final class arrow/core/continuations/ShiftCancellationException : java/ut } public final class arrow/core/continuations/ShiftKt { - public static final fun attempt (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; + public static final fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public static final fun ensure (Larrow/core/continuations/Shift;ZLkotlin/jvm/functions/Function0;)V public static final fun ensureNotNull (Larrow/core/continuations/Shift;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object; public static final fun recover (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; From 627747c858474034710b52ba050fd4633069cdc2 Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Tue, 30 Aug 2022 13:17:08 +0200 Subject: [PATCH 15/20] Retrigger CI From 04c4e0ada134f6413002e09a19547bc516ec431f Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Tue, 30 Aug 2022 14:23:38 +0200 Subject: [PATCH 16/20] Rename Either.catch to recover, and add docs --- .../commonMain/kotlin/arrow/core/Either.kt | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt index 2906703f445..93acb6c03e3 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt @@ -1232,10 +1232,30 @@ public sealed class Either { map { Unit } } -public inline fun Either.catch(block: Shift.(E) -> A): Either = +/** + * Recover from [E] when encountering [Left]. + * You can either return a new value of [A], or short-circuit by shifting with a value of [E2]. + * + * ```kotlin + * import arrow.core.Either + * import arrow.core.left + * import arrow.core.recover + * + * object User + * object Error + * + * val error: Either = Error.left() + * + * val a: Either = error.recover { error -> User } // Either.Right(User) + * val b: Either = error.recover { error -> shift("other-failure") } // Either.Left(other-failure) + * val c: Either = error.recover { error -> User } // Either.Right(User) + * ``` + * + */ +public inline fun Either.recover(recover: Shift.(E) -> A): Either = when (this) { is Right -> this - is Left -> either { block(value) } + is Left -> either { recover(value) } } /** @@ -1486,13 +1506,13 @@ public inline fun Any?.rightIfNull(default: () -> A): Either = * This is like `flatMap` for the exception. */ public inline fun Either.handleErrorWith(f: (A) -> Either): Either = - catch { f(it).bind() } + recover { f(it).bind() } public inline fun Either.handleError(f: (A) -> B): Either = - catch { f(it) } + recover { f(it) } public inline fun Either.redeem(fe: (A) -> C, fa: (B) -> C): Either = - either { fa(bind()) }.catch { fe(it) } + either { fa(bind()) }.recover { fe(it) } public operator fun , B : Comparable> Either.compareTo(other: Either): Int = fold( From 2f8ef0e518b275e7a1e4f532103bcc9b83040ec0 Mon Sep 17 00:00:00 2001 From: nomisRev Date: Tue, 30 Aug 2022 12:27:12 +0000 Subject: [PATCH 17/20] Update API files --- arrow-libs/core/arrow-core/api/arrow-core.api | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arrow-libs/core/arrow-core/api/arrow-core.api b/arrow-libs/core/arrow-core/api/arrow-core.api index 68204097621..752d9746f66 100644 --- a/arrow-libs/core/arrow-core/api/arrow-core.api +++ b/arrow-libs/core/arrow-core/api/arrow-core.api @@ -244,7 +244,6 @@ public final class arrow/core/EitherKt { public static final fun bisequenceNullable (Larrow/core/Either;)Larrow/core/Either; public static final fun bisequenceOption (Larrow/core/Either;)Larrow/core/Option; public static final fun bisequenceValidated (Larrow/core/Either;)Larrow/core/Validated; - public static final fun catch (Larrow/core/Either;Lkotlin/jvm/functions/Function2;)Larrow/core/Either; public static final fun combine (Larrow/core/Either;Larrow/typeclasses/Semigroup;Larrow/typeclasses/Semigroup;Larrow/core/Either;)Larrow/core/Either; public static final fun combineAll (Ljava/lang/Iterable;Larrow/typeclasses/Monoid;Larrow/typeclasses/Monoid;)Larrow/core/Either; public static final fun combineK (Larrow/core/Either;Larrow/core/Either;)Larrow/core/Either; @@ -264,6 +263,7 @@ public final class arrow/core/EitherKt { public static final fun leftWiden (Larrow/core/Either;)Larrow/core/Either; public static final fun merge (Larrow/core/Either;)Ljava/lang/Object; public static final fun orNull (Larrow/core/Either;)Ljava/lang/Object; + public static final fun recover (Larrow/core/Either;Lkotlin/jvm/functions/Function2;)Larrow/core/Either; public static final fun redeem (Larrow/core/Either;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Larrow/core/Either; public static final fun redeemWith (Larrow/core/Either;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Larrow/core/Either; public static final fun replicate (Larrow/core/Either;ILarrow/typeclasses/Monoid;)Larrow/core/Either; From 60edb5c92af4bcd37e5381871218f7a5a5ee3bef Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Wed, 31 Aug 2022 13:35:30 +0200 Subject: [PATCH 18/20] Rename JvmName catchOrThrow to catchReified --- .../kotlin/arrow/core/continuations/ErrorHandlers.kt | 4 ++-- .../src/commonMain/kotlin/arrow/core/continuations/Shift.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt index 4368a7a1f2b..9493b0aa8dd 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/ErrorHandlers.kt @@ -82,7 +82,7 @@ public infix fun Effect.catch(@BuilderInference resolve: suspend Sh * ``` * */ -@JvmName("catchOrThrow") +@JvmName("catchReified") public inline infix fun Effect.catch( @BuilderInference crossinline recover: suspend Shift.(T) -> A, ): Effect = @@ -104,7 +104,7 @@ public infix fun EagerEffect.recover(@BuilderInference resolve: public infix fun EagerEffect.catch(@BuilderInference recover: Shift.(throwable: Throwable) -> A): EagerEffect = eagerEffect { catch(recover) } -@JvmName("catchOrThrow") +@JvmName("catchReified") public inline infix fun EagerEffect.catch( @BuilderInference crossinline recover: Shift.(T) -> A, ): EagerEffect = diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt index a906d90b9fc..1e13f47c1c6 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Shift.kt @@ -92,7 +92,7 @@ public inline fun Shift.catch( ): A = fold({ action(this) }, { recover(it) }, { shift(it) }, { it }) @EffectDSL -@JvmName("catchOrThrow") +@JvmName("catchReified") public inline fun Shift.catch( @BuilderInference action: Shift.() -> A, @BuilderInference recover: Shift.(T) -> A, From c1e7eeda883826c3bfd82d12b48581d0adf07012 Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Wed, 31 Aug 2022 13:42:29 +0200 Subject: [PATCH 19/20] apiDump & knit --- .../commonMain/kotlin/arrow/core/Either.kt | 20 +++++++++---------- .../kotlin/examples/example-either-45.kt | 20 +++++++++++-------- .../kotlin/examples/example-either-46.kt | 5 +++-- .../kotlin/examples/example-either-47.kt | 5 ++--- .../kotlin/examples/example-either-48.kt | 13 +++++------- .../kotlin/examples/example-either-49.kt | 19 ++++++++---------- .../kotlin/examples/example-either-50.kt | 18 +++++++++++------ .../kotlin/examples/example-either-51.kt | 11 +++++----- .../kotlin/examples/example-either-52.kt | 9 ++++++--- .../kotlin/examples/example-either-53.kt | 12 ++++------- .../kotlin/examples/example-either-54.kt | 13 ++++++++++++ 11 files changed, 80 insertions(+), 65 deletions(-) create mode 100644 arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-54.kt diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt index 93acb6c03e3..3d479b8ede2 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt @@ -1250,7 +1250,7 @@ public sealed class Either { * val b: Either = error.recover { error -> shift("other-failure") } // Either.Left(other-failure) * val c: Either = error.recover { error -> User } // Either.Right(User) * ``` - * + * */ public inline fun Either.recover(recover: Shift.(E) -> A): Either = when (this) { @@ -1286,7 +1286,7 @@ public fun Either>.flatten(): Either = * Left(12).getOrElse { 17 } // Result: 17 * } * ``` - * + * */ public inline fun Either<*, B>.getOrElse(default: () -> B): B = fold({ default() }, ::identity) @@ -1304,7 +1304,7 @@ public inline fun Either<*, B>.getOrElse(default: () -> B): B = * Left(12).orNull() // Result: null * } * ``` - * + * */ @Deprecated("Duplicated API. Please use Either's member function orNull. This will be removed towards Arrow 2.0", ReplaceWith("orNull()")) public fun Either<*, B>.orNull(): B? = @@ -1325,7 +1325,7 @@ public fun Either<*, B>.orNull(): B? = * Left(12).getOrHandle { it + 5 } // Result: 17 * } * ``` - * + * */ public inline fun Either.getOrHandle(default: (A) -> B): B = fold({ default(it) }, ::identity) @@ -1353,7 +1353,7 @@ public inline fun Either.getOrHandle(default: (A) -> B): B = * left.filterOrElse({ it > 10 }, { -1 }) // Result: Left(12) * } * ``` - * + * */ public inline fun Either.filterOrElse(predicate: (B) -> Boolean, default: () -> A): Either = flatMap { if (predicate(it)) Right(it) else Left(default()) } @@ -1386,7 +1386,7 @@ public inline fun Either.filterOrElse(predicate: (B) -> Boolean, de * //sampleEnd * } * ``` - * + * */ public inline fun Either.filterOrOther(predicate: (B) -> Boolean, default: (B) -> A): Either = flatMap { @@ -1408,7 +1408,7 @@ public inline fun Either.filterOrOther(predicate: (B) -> Boolean, d * Left(12).merge() // Result: 12 * } * ``` - * + * */ public inline fun Either.merge(): A = fold(::identity, ::identity) @@ -1433,7 +1433,7 @@ public inline fun Either.merge(): A = * Left(12).leftIfNull({ -1 }) // Result: Left(12) * } * ``` - * + * */ public inline fun Either.leftIfNull(default: () -> A): Either = flatMap { it.rightIfNotNull { default() } } @@ -1485,7 +1485,7 @@ public fun A.right(): Either = Right(this) * null.rightIfNotNull { "left" } // Left(a="left") * } * ``` - * + * */ public inline fun B?.rightIfNotNull(default: () -> A): Either = when (this) { null -> Left(default()) @@ -1551,7 +1551,7 @@ public fun Iterable>.combineAll(MA: Monoid, MB: Monoid * println(chars) * } * ``` - * + * */ public fun Either.widen(): Either = this diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-45.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-45.kt index df6411d8a14..2b56f3c4506 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-45.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-45.kt @@ -1,11 +1,15 @@ // This file was automatically generated from Either.kt by Knit tool. Do not edit. package arrow.core.examples.exampleEither45 -import arrow.core.Either.Right -import arrow.core.Either.Left -import arrow.core.getOrElse - -fun main() { - Right(12).getOrElse { 17 } // Result: 12 - Left(12).getOrElse { 17 } // Result: 17 -} +import arrow.core.Either +import arrow.core.left +import arrow.core.recover + +object User +object Error + +val error: Either = Error.left() + +val a: Either = error.recover { error -> User } // Either.Right(User) +val b: Either = error.recover { error -> shift("other-failure") } // Either.Left(other-failure) +val c: Either = error.recover { error -> User } // Either.Right(User) diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-46.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-46.kt index e0b22d93443..0de140e5df0 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-46.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-46.kt @@ -3,8 +3,9 @@ package arrow.core.examples.exampleEither46 import arrow.core.Either.Right import arrow.core.Either.Left +import arrow.core.getOrElse fun main() { - Right(12).orNull() // Result: 12 - Left(12).orNull() // Result: null + Right(12).getOrElse { 17 } // Result: 12 + Left(12).getOrElse { 17 } // Result: 17 } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-47.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-47.kt index 7e170618794..b55ad404f8c 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-47.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-47.kt @@ -3,9 +3,8 @@ package arrow.core.examples.exampleEither47 import arrow.core.Either.Right import arrow.core.Either.Left -import arrow.core.getOrHandle fun main() { - Right(12).getOrHandle { 17 } // Result: 12 - Left(12).getOrHandle { it + 5 } // Result: 17 + Right(12).orNull() // Result: 12 + Left(12).orNull() // Result: null } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-48.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-48.kt index 6d282c44373..8ee7acceda5 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-48.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-48.kt @@ -1,14 +1,11 @@ // This file was automatically generated from Either.kt by Knit tool. Do not edit. package arrow.core.examples.exampleEither48 -import arrow.core.Either.* -import arrow.core.Either -import arrow.core.filterOrElse +import arrow.core.Either.Right +import arrow.core.Either.Left +import arrow.core.getOrHandle fun main() { - Right(12).filterOrElse({ it > 10 }, { -1 }) // Result: Right(12) - Right(7).filterOrElse({ it > 10 }, { -1 }) // Result: Left(-1) - - val left: Either = Left(12) - left.filterOrElse({ it > 10 }, { -1 }) // Result: Left(12) + Right(12).getOrHandle { 17 } // Result: 12 + Left(12).getOrHandle { it + 5 } // Result: 17 } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-49.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-49.kt index 2d14098d02d..53f1923b8ac 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-49.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-49.kt @@ -1,17 +1,14 @@ // This file was automatically generated from Either.kt by Knit tool. Do not edit. package arrow.core.examples.exampleEither49 -import arrow.core.* +import arrow.core.Either.* +import arrow.core.Either +import arrow.core.filterOrElse -suspend fun main(): Unit { - //sampleStart - Either.Right(7).filterOrOther({ it == 10 }, { "Value '$it' is not equal to 10" }) - .let(::println) // Either.Left(Value '7' is not equal to 10") +fun main() { + Right(12).filterOrElse({ it > 10 }, { -1 }) // Result: Right(12) + Right(7).filterOrElse({ it > 10 }, { -1 }) // Result: Left(-1) - Either.Right(10).filterOrOther({ it == 10 }, { "Value '$it' is not equal to 10" }) - .let(::println) // Either.Right(10) - - Either.Left(12).filterOrOther({ str: String -> str.contains("impossible") }, { -1 }) - .let(::println) // Either.Left(12) - //sampleEnd + val left: Either = Left(12) + left.filterOrElse({ it > 10 }, { -1 }) // Result: Left(12) } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-50.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-50.kt index 458058b4dff..e8697f9cc61 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-50.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-50.kt @@ -1,11 +1,17 @@ // This file was automatically generated from Either.kt by Knit tool. Do not edit. package arrow.core.examples.exampleEither50 -import arrow.core.Either.Left -import arrow.core.Either.Right -import arrow.core.merge +import arrow.core.* -fun main() { - Right(12).merge() // Result: 12 - Left(12).merge() // Result: 12 +suspend fun main(): Unit { + //sampleStart + Either.Right(7).filterOrOther({ it == 10 }, { "Value '$it' is not equal to 10" }) + .let(::println) // Either.Left(Value '7' is not equal to 10") + + Either.Right(10).filterOrOther({ it == 10 }, { "Value '$it' is not equal to 10" }) + .let(::println) // Either.Right(10) + + Either.Left(12).filterOrOther({ str: String -> str.contains("impossible") }, { -1 }) + .let(::println) // Either.Left(12) + //sampleEnd } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-51.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-51.kt index 9826b45ba9a..9057d0efa58 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-51.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-51.kt @@ -1,12 +1,11 @@ // This file was automatically generated from Either.kt by Knit tool. Do not edit. package arrow.core.examples.exampleEither51 -import arrow.core.Either.* -import arrow.core.leftIfNull +import arrow.core.Either.Left +import arrow.core.Either.Right +import arrow.core.merge fun main() { - Right(12).leftIfNull({ -1 }) // Result: Right(12) - Right(null).leftIfNull({ -1 }) // Result: Left(-1) - - Left(12).leftIfNull({ -1 }) // Result: Left(12) + Right(12).merge() // Result: 12 + Left(12).merge() // Result: 12 } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-52.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-52.kt index eaf931db471..72aef4d31a3 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-52.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-52.kt @@ -1,9 +1,12 @@ // This file was automatically generated from Either.kt by Knit tool. Do not edit. package arrow.core.examples.exampleEither52 -import arrow.core.rightIfNotNull +import arrow.core.Either.* +import arrow.core.leftIfNull fun main() { - "value".rightIfNotNull { "left" } // Right(b="value") - null.rightIfNotNull { "left" } // Left(a="left") + Right(12).leftIfNull({ -1 }) // Result: Right(12) + Right(null).leftIfNull({ -1 }) // Result: Left(-1) + + Left(12).leftIfNull({ -1 }) // Result: Left(12) } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-53.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-53.kt index 37b9c008d27..34946b3a896 100644 --- a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-53.kt +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-53.kt @@ -1,13 +1,9 @@ // This file was automatically generated from Either.kt by Knit tool. Do not edit. package arrow.core.examples.exampleEither53 -import arrow.core.* +import arrow.core.rightIfNotNull -fun main(args: Array) { - //sampleStart - val string: Either = "Hello".right() - val chars: Either = - string.widen() - //sampleEnd - println(chars) +fun main() { + "value".rightIfNotNull { "left" } // Right(b="value") + null.rightIfNotNull { "left" } // Left(a="left") } diff --git a/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-54.kt b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-54.kt new file mode 100644 index 00000000000..9e22de7ca0a --- /dev/null +++ b/arrow-libs/core/arrow-core/src/jvmTest/kotlin/examples/example-either-54.kt @@ -0,0 +1,13 @@ +// This file was automatically generated from Either.kt by Knit tool. Do not edit. +package arrow.core.examples.exampleEither54 + +import arrow.core.* + +fun main(args: Array) { + //sampleStart + val string: Either = "Hello".right() + val chars: Either = + string.widen() + //sampleEnd + println(chars) +} From c4ac7ea3283c4a84cdc09b335e9e58b1e87310b1 Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Wed, 31 Aug 2022 13:52:15 +0200 Subject: [PATCH 20/20] Make ShiftCancellationException private --- arrow-libs/core/arrow-core/api/arrow-core.api | 7 +---- .../kotlin/arrow/core/continuations/Fold.kt | 28 ++++++++----------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/arrow-libs/core/arrow-core/api/arrow-core.api b/arrow-libs/core/arrow-core/api/arrow-core.api index 752d9746f66..e6aee5404b0 100644 --- a/arrow-libs/core/arrow-core/api/arrow-core.api +++ b/arrow-libs/core/arrow-core/api/arrow-core.api @@ -2622,6 +2622,7 @@ public final class arrow/core/continuations/Effect { public static final fun recover (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function1; public static final fun recover (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;)Lkotlin/jvm/functions/Function2; public static final fun result (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static final fun shiftedOrRethrow (Ljava/util/concurrent/CancellationException;Larrow/core/continuations/DefaultShift;)Ljava/lang/Object; public static final fun toEither (Lkotlin/jvm/functions/Function1;)Larrow/core/Either; public static final fun toEither (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final fun toIor (Lkotlin/jvm/functions/Function1;)Larrow/core/Ior; @@ -2838,12 +2839,6 @@ public final class arrow/core/continuations/Shift$DefaultImpls { public static fun recover (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; } -public final class arrow/core/continuations/ShiftCancellationException : java/util/concurrent/CancellationException { - public fun (Ljava/lang/Object;Larrow/core/continuations/Shift;)V - public final fun checkScope (Larrow/core/continuations/Shift;)Z - public final fun shifted ()Ljava/lang/Object; -} - public final class arrow/core/continuations/ShiftKt { public static final fun catch (Larrow/core/continuations/Shift;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object; public static final fun ensure (Larrow/core/continuations/Shift;ZLkotlin/jvm/functions/Function0;)V diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt index 3718c15570e..260dcd902ca 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/continuations/Fold.kt @@ -56,31 +56,25 @@ public inline fun fold( val shift = DefaultShift() return try { transform(program(shift)) - } catch (e: ShiftCancellationException) { - if (e.checkScope(shift)) recover(e.shifted()) else throw e + } catch (e: CancellationException) { + recover(e.shiftedOrRethrow(shift)) } catch (e: Throwable) { error(e.nonFatalOrThrow()) } } -/** - * **AVOID USING THIS TYPE, it's meant for low-level cancellation code** When in need in low-level code, - * you can use this type to differentiate between a foreign [CancellationException] such as JobCancellationException, and the one from [Effect]. - */ -public class ShiftCancellationException( - private val _shifted: Any?, - private val shift: Shift, -) : CancellationException("Shifted Continuation") { - @Suppress("UNCHECKED_CAST") - @PublishedApi - internal fun shifted(): R = _shifted as R - - @PublishedApi - internal fun checkScope(other: Shift): Boolean = shift === other -} +/** Returns the shifted value, rethrows the CancellationException if not our scope */ +@PublishedApi +internal fun CancellationException.shiftedOrRethrow(shift: DefaultShift): R = + if (this is ShiftCancellationException && this.shift === shift) _shifted as R + else throw this /** Serves as both purposes of a scope-reference token, and a default implementation for Shift. */ @PublishedApi internal class DefaultShift : Shift { override fun shift(r: Any?): B = throw ShiftCancellationException(r, this) } + +/** CancellationException is required to cancel coroutines when shifting from within them. */ +private class ShiftCancellationException(val _shifted: Any?, val shift: Shift) : + CancellationException("Shifted Continuation")