Module: FunRuby

Extended by:
FunRuby
Included in:
FunRuby
Defined in:
lib/fun_ruby.rb,
lib/fun_ruby/enum.rb,
lib/fun_ruby/file.rb,
lib/fun_ruby/hash.rb,
lib/fun_ruby/array.rb,
lib/fun_ruby/string.rb,
lib/fun_ruby/version.rb,
lib/fun_ruby/function.rb,
lib/fun_ruby/container.rb,
lib/fun_ruby/common/helpers.rb,
lib/fun_ruby/container/mixin.rb,
lib/fun_ruby/container/define.rb,
lib/fun_ruby/container/resolve.rb

Overview

Top-level

Defined Under Namespace

Modules: Array, Enum, File, Function, Hash, String Classes: Container

Constant Summary collapse

PLACEHOLDER =
Object.new.freeze
VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._Object

A placeholder that helps to use not positioned currying



82
83
84
# File 'lib/fun_ruby.rb', line 82

def _
  PLACEHOLDER
end

.containerFunRuby::Container

Returns a global container

Returns:

Since:

  • 0.1.0



34
35
36
# File 'lib/fun_ruby.rb', line 34

def container
  @container ||= Container.new
end

.define(&block) ⇒ Object

Allows to define globally available functions

Examples:

Basic: defines a function

F.define do
  namespace :functions do
    f(:sum) { ->(x, y) { x + y } }
  end
end
F.container.fetch("functions.sum").(2, 3) # => 5

Returns:

  • void

Since:

  • 0.1.0



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

def define(&block)
  Container::Define.build(container: container).(&block)
end

.import(*aliases) ⇒ FunRuby::Container::Mixin

Allows to import global container to your classes and modules

Examples:

F.define do
  namespace :app do
    namespace :string do
      f(:to_s) { ->(x) { x.to_s } }
      f(:map) { F::Enum.map(f(:to_s)) }
    end

    namespace :math do
      f(:x2) { ->(x) { x * 2 } }
      f(:map) { F::Enum.map(f(:x2)) }
    end
  end
end

class Service
  include F.import(
    "app.string" => "s",
    "app.math" => "m"
  )

  def s_map(ary)
    f("s.map").(ary)
  end

  def m_map(ary)
    f("m.map").(ary)
  end
end

ary = [1, 2, 3]
Service.new.s_map(ary) #=> ["1", "2", "3"]
Service.new.m_map(ary) #=> [2, 4, 6]

Returns:

Since:

  • 0.1.0



77
78
79
# File 'lib/fun_ruby.rb', line 77

def import(*aliases)
  container.import(*aliases)
end

Instance Method Details

#_Object

A placeholder that helps to use not positioned currying



82
83
84
# File 'lib/fun_ruby.rb', line 82

def _
  PLACEHOLDER
end

#containerFunRuby::Container

Returns a global container

Returns:

Since:

  • 0.1.0



34
35
36
# File 'lib/fun_ruby.rb', line 34

def container
  @container ||= Container.new
end

#define(&block) ⇒ Object

Allows to define globally available functions

Examples:

Basic: defines a function

F.define do
  namespace :functions do
    f(:sum) { ->(x, y) { x + y } }
  end
end
F.container.fetch("functions.sum").(2, 3) # => 5

Returns:

  • void

Since:

  • 0.1.0



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

def define(&block)
  Container::Define.build(container: container).(&block)
end

#import(*aliases) ⇒ FunRuby::Container::Mixin

Allows to import global container to your classes and modules

Examples:

F.define do
  namespace :app do
    namespace :string do
      f(:to_s) { ->(x) { x.to_s } }
      f(:map) { F::Enum.map(f(:to_s)) }
    end

    namespace :math do
      f(:x2) { ->(x) { x * 2 } }
      f(:map) { F::Enum.map(f(:x2)) }
    end
  end
end

class Service
  include F.import(
    "app.string" => "s",
    "app.math" => "m"
  )

  def s_map(ary)
    f("s.map").(ary)
  end

  def m_map(ary)
    f("m.map").(ary)
  end
end

ary = [1, 2, 3]
Service.new.s_map(ary) #=> ["1", "2", "3"]
Service.new.m_map(ary) #=> [2, 4, 6]

Returns:

Since:

  • 0.1.0



77
78
79
# File 'lib/fun_ruby.rb', line 77

def import(*aliases)
  container.import(*aliases)
end