Module: Divergent::Try

Includes:
Monad
Included in:
Failure, Success
Defined in:
lib/divergent/try.rb

Overview

The ‘Try` type represents a computation that may either result in an exception, or return a successfully computed value. It’s similar to, but semantically different from Either.

Instances of Try, are either an instance of Success or Failure.

For example, ‘Try` can be used to perform division on a user-defined input, without the need to do explicit exception-handling in all of the places that an exception might occur.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Monad

included

Class Method Details

.unitObject



18
19
20
21
22
# File 'lib/divergent/try.rb', line 18

def self.unit()
  Success.new(yield)
rescue => e
  Failure.new(e)
end

Instance Method Details

#each(&block) ⇒ Object

Applies the given block if this is a ‘Success`, otherwise returns `Unit` if this is a `Failure`.

Notes:

If block throws, then this method may throw an exception.

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/divergent/try.rb', line 69

def each(&block)
  raise NotImplementedError
end

#failedObject

Inverts this ‘Try`. If this is a `Failure`, returns its exception wrapped in a `Success`. If this is a `Success`, returns a `Failure` containing an UnSupportedOperationError.

Raises:

  • (NotImplementedError)


121
122
123
# File 'lib/divergent/try.rb', line 121

def failed
  raise NotImplementedError
end

#failure?Boolean

Returns ‘true` if the `Try` is a `Failure`, `false` otherwise.

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/divergent/try.rb', line 25

def failure?
  raise NotImplementedError
end

#filter(&block) ⇒ Object

Converts this to a ‘Failure` if the predicate is not satisfied.

Raises:

  • (NotImplementedError)


81
82
83
# File 'lib/divergent/try.rb', line 81

def filter(&block)
  raise NotImplementedError
end

#flattenObject

Transforms a nested ‘Try` into an un-nested `Try`.

Raises:

  • (NotImplementedError)


114
115
116
# File 'lib/divergent/try.rb', line 114

def flatten
  raise NotImplementedError
end

#getObject

Returns the value from this ‘Success` or throws the exception if this is a `Failure`.

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/divergent/try.rb', line 58

def get
  raise NotImplementedError
end

#get_or_else(default) ⇒ Object

Returns the value from this ‘Success` or the given `default` argument if this is a `Failure`.



36
37
38
39
40
41
42
# File 'lib/divergent/try.rb', line 36

def get_or_else(default)
  if success?
    get
  else
    default
  end
end

#map(&block) ⇒ Object

Maps the given function to the value from this ‘Success` or returns this if this is a `Failure`.

Raises:

  • (NotImplementedError)


76
77
78
# File 'lib/divergent/try.rb', line 76

def map(&block)
  raise NotImplementedError
end

#or_else(default) ⇒ Object

Returns this ‘Try` if it’s a ‘Success` or the given `default` argument if this is a `Failure`.

Notes: the ‘default` value should be an instance of Try.



48
49
50
51
52
53
54
# File 'lib/divergent/try.rb', line 48

def or_else(default)
  if success?
    self
  else
    default
  end
end

#recover(*errors, &block) ⇒ Try

Recover the given error classes by applying the block, and leave other error case unrecoverd. If no class is given, recover it anyway. The error instance(if this is a Failure) will be passed into the block. And, error raised from the block call will not rescued by it! Otherwise returns this if this is a ‘Success`.

This is like ‘map` for the exception.

Parameters:

  • errors (Array)
  • blk (Block)

Returns:

Raises:

  • (NotImplementedError)


109
110
111
# File 'lib/divergent/try.rb', line 109

def recover(*errors, &block)
  raise NotImplementedError
end

#recover_with(*errors, &block) ⇒ Try

Applies the given block if this is a ‘Failure`, otherwise returns this if this is a `Success`.

Notes: block call should return an instance of Try. And, error raised from the block call will not rescued by it! This is like ‘fmap` for the exception.

Parameters:

  • errors (Array)
  • blk, (Block)

    this blk should return an instance of Try

Returns:

  • (Try)

    block call result

Raises:

  • (NotImplementedError)


94
95
96
# File 'lib/divergent/try.rb', line 94

def recover_with(*errors, &block)
  raise NotImplementedError
end

#success?Boolean

Returns ‘true` if the `Try` is a `Success`, `false` otherwise.

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/divergent/try.rb', line 30

def success?
  raise NotImplementedError
end

#transform(s, f) ⇒ Object

Completes this ‘Try` by applying the function `f` to this if this is of type `Failure`, or conversely, by applying `s` if this is a `Success`.

Raises:

  • (NotImplementedError)


128
129
130
# File 'lib/divergent/try.rb', line 128

def transform(s, f)
  raise NotImplementedError
end