Module: Functional

Defined in:
lib/functional.rb,
lib/functional/memo.rb,
lib/functional/delay.rb,
lib/functional/tuple.rb,
lib/functional/union.rb,
lib/functional/either.rb,
lib/functional/option.rb,
lib/functional/record.rb,
lib/functional/version.rb,
lib/functional/protocol.rb,
lib/functional/final_var.rb,
lib/functional/type_check.rb,
lib/functional/final_struct.rb,
lib/functional/value_struct.rb,
lib/functional/protocol_info.rb,
lib/functional/abstract_struct.rb,
lib/functional/synchronization.rb,
lib/functional/method_signature.rb,
lib/functional/pattern_matching.rb

Overview

Erlang, Clojure, and Go inspired functional programming tools to Ruby.

Defined Under Namespace

Modules: Memo, PatternMatching, Protocol, Record, TypeCheck, Union Classes: Delay, Either, FinalStruct, FinalVar, Option, ProtocolInfo, Tuple, ValueStruct

Constant Summary collapse

Infinity =

Infinity

1/0.0
NaN =

Not a number

0/0.0
VERSION =

The current gem version.

'1.3.0'
ProtocolError =

An exception indicating a problem during protocol processing.

Class.new(StandardError)
FinalityError =

An exception raised when an attempt is made to modify an immutable object or attribute.

Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.SpecifyProtocol(name) { ... } ⇒ Functional::ProtocolInfo

Specify a new protocol or retrieve the specification of an existing protocol.

When called without a block the global protocol registry will be searched for a protocol with the matching name. If found the corresponding ProtocolInfo object will be returned. If not found nil will be returned.

When called with a block, a new protocol with the given name will be created and the block will be processed to provide the specifiction. When successful the new ProtocolInfo object will be returned. An exception will be raised if a protocol with the same name already exists.

Examples:

Functional::SpecifyProtocol(:Queue) do
  instance_method :push, 1
  instance_method :pop, 0
  instance_method :length, 0
end

Parameters:

  • name (Symbol)

    The global name of the new protocol

Yields:

  • The protocol definition

Returns:

Raises:

See Also:



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/functional/protocol.rb', line 38

def SpecifyProtocol(name, &block)
  name = name.to_sym
  protocol_info = Protocol.class_variable_get(:@@info)[name]

  return protocol_info unless block_given?

  if block_given? && protocol_info
    raise ProtocolError.new(":#{name} has already been defined")
  end

  info = ProtocolInfo.new(name, &block)
  Protocol.class_variable_get(:@@info)[name] = info
end