    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477

    # ............................................................................................... #
    #
    # Copyright (c) 2012-2017 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others
    #
    # This program and the accompanying materials are made available under the
    # terms of the Eclipse Public License 2.0 which is available at
    # http://www.eclipse.org/legal/epl-2.0.
    #
    # SPDX-License-Identifier: EPL-2.0
    #
    # ............................................................................................... #

    ----
    Useful augmentations, decorators and function to deal with errors in Golo.
    ----
    module gololang.Errors

    import java.util.`function
    import java.util.Collections

    # ............................................................................ #
    ## Constructors

    ----
    Constructor for `gololang.error.Result` values.

    - *param* `value`: the value to encapsulate in the `Result`
    ----
    function Ok = |value| -> gololang.error.Result.ok(value)

    ----
    Constructor for `gololang.error.Result` errors.

    - *param* `message`: the message for the error in the `Result`
    ----
    function Error = |message| -> gololang.error.Result.fail(message)

    ----
    Constructor for an empty `gololang.error.Result`.
    ----
    function Empty = -> gololang.error.Result.empty()

    ----
    Constructor for an empty `java.util.Optional`.
    ----
    function None = -> java.util.Optional.empty()

    ----
    Constructor `java.util.Optional` values.

    - *param* `value`: the value to encapsulate in the `Optional`
    ----
    function Some = |value| -> java.util.Optional.of(value)

    ----
    “Smart” constructor for `java.util.Optional`.

    If the given value is already an `Optional`, returns it unchanged; if it's a
    `Result`, converts it into an `Optional`, and create an `Optional` otherwise
    (using `ofNullable`)
    ----
    function Option = |v| -> match {
      when v oftype java.util.Optional.class then v
      when v oftype gololang.error.Result.class then v: toOptional()
      otherwise java.util.Optional.ofNullable(v)
    }

    ----
    “Smart” constructor for `gololang.error.Result`.

    If the given value is already a `Result`, returns it unchanged
    and create an `Optional` otherwise.
    ----
    function Result = |v| -> match {
      when v oftype gololang.error.Result.class then v
      otherwise gololang.error.Result.of(v)
    }

    # ............................................................................ #
    augment java.util.Optional {
      ----
      Test if this optional is empty.
      ----
      function isEmpty = |this| -> not this: isPresent()

      ----
      Alias for `isEmpty`
      ----
      function isNone = |this| -> not this: isPresent()

      ----
      Alias for `isPresent`
      ----
      function isSome = |this| -> this: isPresent()

      ----
      Test if this optional contains a value equals to the provided one.
      ----
      function isSome = |this, value| ->
          this: isPresent() and this: get(): equals(value)

      function iterator = |this| -> match {
        when this: isPresent() then singleton(this: get()): iterator()
        otherwise emptyIterator()
      }

      ----
      Reduce this option using `func` with `init` as initial value.

      For instance:

          Some("b"): reduce("a", |x, y| -> x + y) == "ab"
          None(): reduce(42, |x, y| -> x + y) == 42

      - *param* `init` the initial value
      - *param* `func` the aggregation function
      - *return* the initial value if this is empty, the aggregated result otherwise
      ----
      function reduce = |this, init, func| -> match {
        when this: isPresent() then func: invoke(init, this: get())
        otherwise init
      }

      ----
      Remove one level of optional.

      This is actually equivalent to `:flatMap(identity)`
      (or `:flatMap(f)` is equivalent to `:map(f): flattened()`).

      For instance:

          Some(Some(42)): flattened() == Some(42)
          None(): flattened() == None()
          Some(None()): flattened() == None()
      ----
      function flattened = |this| -> this: flatMap(Function.identity())

      ----
      Convert this optional into a list

      - *return* a singleton list containing the value if present, otherwise an
        empty list
      ----
      function toList = |this| -> match {
        when this: isPresent() then singletonList(this: get())
        otherwise emptyList()
      }

      ----
      Same as `map` or `flatMap`, depending on the type returned by `f`.

      This is a generic version for `map` and `flatMap`: if `f` returns an
      `Optional`, it's equivalent to `flatMap`, otherwise, it's equivalent to `map`.

      This allows code such as:

          Some(21): andThen(|x| -> x + 1): andThen(|x| -> Some(2 * x)) == Some(42)
      ----
      function andThen = |this, f| {
        case {
          when this: isPresent() {
            let r = f: invoke(this: get())
            return match {
              when r oftype java.util.Optional.class then r
              otherwise java.util.Optional.ofNullable(r)
            }
          }
          otherwise {
            return this
          }
        }
      }

      ----
      Conjunctive chaining.

      This is equivalent to `:flatMap(|_| -> other)`,
      i.e. applicative or monadic sequence with discarding.
      (Can also be seen as `:map(const(^id)): apply(other)`)

      For instance:

          Some(1): `and(None()) == None()
          None(): `and(Some(1)) == None()
          Some(1): `and(Some("2")) == Some("2")

      Note that this method is eager. If you want laziness, use the `flatMap`
      equivalent.

      - *param* `other` the other optional
      - *return* `other` if this optional is present, otherwise `this`
      ----
      function `and = |this, other| -> match {
        when this: isPresent() then other
        otherwise this
      }

      ----
      Disjunctive chaining.

      For instance:

          Some(1): `or(Some(2)) == Some(1)
          None(): `or(Some(1)) == Some(1)
          Some(1): `or(None()) == Some(1)
          None(): `or(None()) == None()

      Note that this method is eager.

        Some(1): orElseGet(-> Some(2))
        None(): orElseGet(-> Some(2))

      - *param* `other` the other optional
      - *return* `other` if this optional is empty, otherwise `this`
      ----
      function `or = |this, other| -> match {
        when this: isPresent() then this
        otherwise other
      }

      function toResult = |this, param| -> match {
        when this: isSome() then
          gololang.error.Result.ok(this: get())
        when param oftype java.lang.Throwable.class then
          gololang.error.Result.error(param)
        otherwise
          gololang.error.Result.fail(param: toString())
      }

      function toResult = |this| ->
        this: toResult(NoSuchElementException("empty Optional"))

      ----
      Apply the function contained is this optional to the given optional. If the
      function has several parameters, an optional containing a partialized version
      is returned, that can be `apply`ed to subsequent optionals.
      This makes `Optional` an “applicative functor”.

      For instance:
          let f = Some(|x| -> x + 10)
          f: apply(Some(32)) # Some(42)
          f: apply(None())  # None()

          Some(|a, b| -> a + b): apply(Some(21)): apply(Some(21)) # Some(42)
      ----
      function apply = |this, arg| {
        case {
          when this: isPresent() {
            let f = this: get()
            require(isClosure(f), "The optional must contain a function to be applied")
            return match {
              when arg: isNone() then arg
              when f: arity() > 1 then Some(f: bindTo(arg: get()))
              otherwise Some(f: invoke(arg: get()))
            }
          }
          otherwise {
            return this
          }
        }
      }

      ----
      Case analysis for the option.

      If the result is not empty, apply the first function (similar to `flatMap`),
      otherwise invoke the second function (similar to `orElseGet`). For instance:

          Some(21): either(|x| -> x * 2, -> "plop") == 42
          None(): either(|x| -> x * 2, -> "plop") == "plop"

      This is indeed equivalent to

          opt: map(mapping): orElseGet(default)

      - *param* `mapping`: the function to apply to the contained value
      - *param* `default`: the function to invoke if the option is empty (takes no arguments)
      - *return* the result of applying the corresponding function
      ----
      function either = |this, mapping, default| -> match {
        when this: isPresent() then mapping(this: get())
        otherwise default()
      }
    }

    # ............................................................................ #
    ## Decorators

    ----
    Transform a function raising an exception into a function returning a `Result`.
    The resulting function returns `Result.ok(f(x))` if `f(x)` succeeds and
    `Result.error(e)` if `f(x)` raises `e`.

    Can be used as a decorator.
    ----
    function result = |f| -> |args...| {
      try {
        return Result(f: invoke(args))
      } catch (e) {
        return Result(e)
      }
    }

    ----
    Transform a function raising an exception into a function returning a option.
    The resulting function returns `Option(f(x))` if `f(x)` succeeds and `None` if
    `f(x)` raises an exception.

    Can be used as a decorator.

    See also [`raising`](#raising_1)
    ----
    function option = |f| -> |args...| {
      try {
        return Option(f: invoke(args))
      } catch (e) {
        return None()
      }
    }

    ----
    Execute the given block and return a `Result`.

    This is similar to [`result`](#result_1), except that the block take no parameter and is
    immediately executed. This is *not* a decorator, but rather a replacement for a
    `try catch` block.

    For instance:

        let res = trying({
          let f = foo()
          let x = bar()
          let z = f(x)
          return z + plop()
        })

    If any of the operation in the block raises an exception, `res` will be a
    `Result.error` containing the exception, otherwise it will be a `Result.ok`
    containing the returned value.

    See also [`result`](#result_1), [`catching`](#catching_1)
    ----
    function trying = |f| {
      # TODO: later... make it a macro?
      try {
        return gololang.error.Result.ok(f())
      } catch (e) {
        return gololang.error.Result.error(e)
      }
    }

    ----
    Transforms a function returning a result or an option into a function returning
    the associated value or raising an exception.

    This is the inverse behavior of [`result`](#result_1) and [`option`](#option_1)

    Can be used as a decorator.

    See also [`result`](#result_1), [`option`](#option_1)
    ----
    function raising = |f| -> |args...| -> f: invoke(args): get()

    ----
    Transform a function into one that can return `null` if something went wrong
    (i.e. None, Error or exception).

    Can be used as a decorator.
    ----
    function nullify = |f| -> |args...| {
      try {
        let r = f: invoke(args)
        return match {
          when r oftype java.util.Optional.class then r: get()
          when r oftype gololang.error.Result.class then r: get()
          otherwise r
        }
      } catch (e) {
        return null
      }
    }

    ----
    Allows an unary function to be called with an `Optional` or a `Result` value.
    Similar to `flatMap`, but using `default` on the exception if the call failed.

    For instance:

        let foo = |x| -> x: toUpperCase()

        let safeFoo = catching("plop")(foo)

        safeFoo(null)         # "plop"
        safeFoo(None())       # "plop"
        safeFoo(Error("err")) # "plop"
        safeFoo(Ok("a"))      # "A"
        safeFoo(Some("a"))    # "A"

        catching(^gololang.error.Result::error)(foo)(null) # Result.error(NullPointerException())

    Can be used as a decorator.

    - *param* `default`: the value to return if a exception occurred, or the function
      to apply to the exception.

    See also [`trying`](#trying_1), [`result`](#result_1), [`option`](#option_1)
    ----
    function catching = |default| -> |func| -> |val| {
      try {
        return func: invoke(val: get())
      } catch (e) {
        return match {
          when isClosure(default) then default: invoke(e)
          otherwise default
        }
      }
    }

    ----
    Create a catcher that execute the given block dealing with exceptions.

    This is similar to [`trying`](#trying_1), but encapsulate the `catch` function
    instead of returning a `Result`. It is also similar to [`catching`](#catching),
    but the block is the block take no parameter and is immediately executed.

        let recover = catcher(|ex| -> match {
          when ex oftype IllegalArgumentException.class then "default"
          otherwise ""
        })

        let result = recover({
          let a = bar()
          let r = foo(42)
          return "foo: " + r
        )}

    This function can be used as a decorator:

        @!catcher
        function recoverIAE = |ex| -> match {
          when ex oftype IllegalArgumentException.class then "default"
          otherwise ""
        }

        let result = recoverIAE({
          let a = bar()
          let r = foo(42)
          return "foo: " + r
        })

    It is somewhat equivalent to a `trying` followed by `either` as in:

        let recover = |ex| -> match { ... }

        result = catcher(recover)({
          // some code that can fail
        })

        result = trying({
          // some code that can fail
        }): either(^gololang.Functions::id, recover)

    - *param* `recover`: the value to return if a exception occurred, or the function
      to apply to the exception.

    See also [`trying`](#trying_1), [`catching`](#catching)
    ----
    function catcher = |recover| -> |block| {
      try {
        return block()
      } catch(e) {
        return match {
          when isClosure(recover) then recover(e)
          otherwise recover
        }
      }
    }