Module: Llull

Defined in:
lib/llull/result.rb,
lib/llull.rb,
lib/llull/version.rb,
lib/llull/pattern_matching.rb

Overview

Llull provides composable Result types for Ruby applications. Inspired by functional programming and railway-oriented programming patterns.

Defined Under Namespace

Modules: PatternMatching Classes: Error, Matcher, Result, ResultError

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.collect(results) ⇒ Result Also known as: all

Collect multiple Results into a single Result Returns Success with array of all values if all Results are successful, otherwise returns the first Failure encountered

Parameters:

  • results (Array<Result>)

    array of Results to collect

Returns:

  • (Result)

    Success with array of values or first Failure



265
266
267
268
269
270
271
# File 'lib/llull/result.rb', line 265

def collect(results)
  results.each do |result|
    raise TypeError, "All elements must be Results" unless result.is_a?(Result)
    return result if result.failure?
  end
  Llull::Success(results.map(&:value))
end

.Failure(error_type, error_data = nil) ⇒ Result

Create a failed Result

Parameters:

  • error_type (Symbol)

    the type/category of error

  • error_data (Object) (defaults to: nil)

    additional error information

Returns:

  • (Result)

    a failed Result containing the error information



256
257
258
# File 'lib/llull/result.rb', line 256

def Failure(error_type, error_data = nil)
  Result.new(false, nil, error_type, error_data)
end

.partition(results) ⇒ Array

Partition an array of Results into successes and failures

Parameters:

  • results (Array<Result>)

    array of Results to partition

Returns:

  • (Array)

    tuple of [success_values, failure_tuples] where success_values is Array of successful values and failure_tuples is Array of [error_type, error_data] pairs



305
306
307
308
309
310
# File 'lib/llull/result.rb', line 305

def partition(results)
  validate_results_array(results)

  successes, failures = results.partition(&:success?)
  [successes.map(&:value), failures.map { |f| [f.error_type, f.error_data] }]
end

.sequence(result) ⇒ Array<Result>

Convert a Result containing an array into an array of Results

Parameters:

  • result (Result)

    Result containing an array value

Returns:

  • (Array<Result>)

    array of individual Results

Raises:

  • (TypeError)


288
289
290
291
292
293
294
295
296
297
298
# File 'lib/llull/result.rb', line 288

def sequence(result)
  raise TypeError, "Argument must be a Result" unless result.is_a?(Result)

  if result.success?
    raise TypeError, "Result value must be an Array" unless result.value.is_a?(Array)

    result.value.map { |item| Llull::Success(item) }
  else
    [result] # Return array containing the single failure
  end
end

.Success(value) ⇒ Result

Create a successful Result

Parameters:

  • value (Object)

    the successful value

Returns:

  • (Result)

    a successful Result containing the value



248
249
250
# File 'lib/llull/result.rb', line 248

def Success(value)
  Result.new(true, value)
end

.traverse(collection) ⇒ Result

Transform a collection by applying a function that returns Results Similar to collect but maps over input and collects the results

Parameters:

  • collection (Array)

    input collection to transform

  • block (Proc)

    function that takes an element and returns a Result

Returns:

  • (Result)

    Success with array of transformed values or first Failure



280
281
282
283
# File 'lib/llull/result.rb', line 280

def traverse(collection, &)
  results = collection.map(&)
  collect(results)
end

.validate_results_array(results) ⇒ Object

Validate that all elements in array are Results

Parameters:

  • results (Array)

    array to validate

Raises:

  • (TypeError)

    if any element is not a Result



317
318
319
320
321
# File 'lib/llull/result.rb', line 317

def validate_results_array(results)
  results.each do |result|
    raise TypeError, "All elements must be Results" unless result.is_a?(Result)
  end
end