Class: Pure::PureModule

Inherits:
Module
  • Object
show all
Defined in:
lib/pure/pure_module.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, &block) ⇒ PureModule

:nodoc:



4
5
6
7
8
# File 'lib/pure/pure_module.rb', line 4

def initialize(parser, &block)  #:nodoc:
  @parsing_active = true
  @parser = parser
  super(&block)
end

Instance Attribute Details

#parserObject (readonly)

:nodoc:



143
144
145
# File 'lib/pure/pure_module.rb', line 143

def parser
  @parser
end

Instance Method Details

#compute(*args) ⇒ Object

call-seq: compute(overrides = {})

compute(num_parallel, overrides = {})
compute(worker, overrides = {})

Initialize a computation.

All three forms take an optional hash for overriding pure functions.

In the first form, Pure.worker is the worker for the computation and Pure.worker decides the number of parallel computations.

In the second form, Pure.worker is the worker for the computation and num_parallel is passed as a hint to Pure.worker, which may accept or ignore the hint.

In the third form, worker is the worker for the computation and worker decides the number of parallel computations.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pure/pure_module.rb', line 30

def compute(*args)
  overrides = args.last.is_a?(Hash) ? args.pop : Hash.new
  worker, num_parallel = (
    case args.size
    when 0
      [Pure.worker, nil]
    when 1
      if args[0].is_a? Integer
        [Pure.worker, args[0]]
      else
        [args[0], nil]
      end
    else
      raise ArgumentError, "wrong number of arguments"
    end
  )
  driver = Driver.new(worker, self, num_parallel, overrides)
  delegate = Delegate.new(driver)
  if block_given?
    yield delegate
  else
    delegate
  end
end

#deactivate_parsingObject

:nodoc:



145
146
147
148
149
150
151
152
# File 'lib/pure/pure_module.rb', line 145

def deactivate_parsing  #:nodoc:
  @parsing_active = false
  begin
    yield
  ensure
    @parsing_active = true
  end
end

#define_method(*args, &block) ⇒ Object

:nodoc:



135
136
137
138
139
140
141
# File 'lib/pure/pure_module.rb', line 135

def define_method(*args, &block)  #:nodoc:
  if @parsing_active
    raise DefineMethodError.new(*Util.file_line(caller.first))
  else
    super
  end
end

#each_functionObject

:nodoc:



154
155
156
157
158
159
160
161
162
# File 'lib/pure/pure_module.rb', line 154

def each_function  #:nodoc:
  ancestors.each { |ancestor|
    if defs = ExtractedFunctions[parser][ancestor]
      defs.each_pair { |name, spec|
        yield name, spec
      }
    end
  }
end

#fun(arg, &block) ⇒ Object

call-seq: fun name => [name_a, name_b,…] do |arg_a, arg_b,…|

  ...
end

Define a pure function whose name and/or argument names are not known at compile time.

The name of the pure function is the value of name. The names of the function arguments are name_a, name_b,… The respective values of the function arguments are arg_a,arg_b,…

See README.rdoc for examples.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pure/pure_module.rb', line 70

def fun(arg, &block)
  function_str, arg_data = parse_fun_arg(arg)
  arg_names = (
    if arg_data.is_a? Enumerable
      arg_data.map { |t| t.to_sym }
    else
      [arg_data.to_sym]
    end
  )
  function_name = function_str.to_sym
  deactivate_parsing {
    define_method(function_name, &block)
  }
  Extractor.record_function(self, :fun, function_name, arg_names, caller)
  nil
end

#fun_map(arg, &block) ⇒ Object

call-seq: fun_map name => enumerable do |elem|

  ...
end

Define an anonymous pure function which is applied to each element of the given enumerable. The pure function name returns the result array.

See README.rdoc for examples.



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
# File 'lib/pure/pure_module.rb', line 98

def fun_map(arg, &block)
  function_name, elems = parse_fun_arg(arg)

  function_name = function_name.to_sym
  elems = elems.to_a

  input_elem_names, output_elem_names = [:input, :output].map { |which|
    (0...elems.size).map { |index|
      "__elem_#{which}_#{index}_#{function_name}".to_sym
    }
  }

  entry = ExtractedFunctions[parser][self]
  code = Extractor.record_function(self, :fun, :__tmp, [], caller)[:code]
  entry.delete(:__tmp)
  
  fun function_name => output_elem_names do |*result|
    result
  end
  entry[function_name][:elems] = input_elem_names.zip(elems)

  output_elem_names.zip(input_elem_names) { |output, input|
    fun output => input do |*args|
      block.call(*args)
    end
    entry[output][:code] = code
  }
  nil
end

#method_added(function_name) ⇒ Object

:nodoc:



128
129
130
131
132
133
# File 'lib/pure/pure_module.rb', line 128

def method_added(function_name)  #:nodoc:
  super
  if @parsing_active
    Extractor.record_function(self, :def, function_name, nil, caller)
  end
end

#parse_fun_arg(arg) ⇒ Object

:nodoc:



164
165
166
167
168
169
170
171
172
173
# File 'lib/pure/pure_module.rb', line 164

def parse_fun_arg(arg)  #:nodoc:
  if arg.is_a? Hash
    unless arg.size == 1
      raise ArgumentError, "`fun' given hash of size != 1"
    end
    arg.to_a.first
  else
    [arg, []]
  end
end