Class: Calculi::Function

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/calculi/function.rb

Overview

A calculated function.

Constant Summary

Constants included from Utility

Utility::NON_IVAR, Utility::NULL

Attributes collapse

Instance Attribute Summary collapse

DSL methods collapse

Instance Method Summary collapse

Methods included from Attributes

#configure, #initialize

Methods included from HasOptionSet

#option_set

Methods included from Utility

#at_prefixed, #callable?, #constantly, #eval_or_value, #instance_variable_compute, #procable?, #set_attribute

Instance Attribute Details

#dependency_namesObject (readonly)



91
92
93
# File 'lib/calculi/function.rb', line 91

def dependency_names
  @dependency_names ||= Set.new
end

#function_setObject (readonly)

Returns the value of attribute function_set.



97
98
99
# File 'lib/calculi/function.rb', line 97

def function_set
  @function_set
end

#keyObject (readonly)

Returns the value of attribute key.



100
101
102
# File 'lib/calculi/function.rb', line 100

def key
  @key
end

#memoizeBoolean (readonly)

Returns:

  • (Boolean)


7
# File 'lib/calculi/function.rb', line 7

calculi_bool :memoize

#recursiveBoolean (readonly)

TODO:

implement trampoline or some pseudostack to recur?

Note:

currently no effect

Returns:

  • (Boolean)


13
# File 'lib/calculi/function.rb', line 13

calculi_bool :recursive

#targetClass (readonly)

Returns:

  • (Class)


104
# File 'lib/calculi/function.rb', line 104

delegate :target, to: :function_set

Instance Method Details

#body(&body_proc) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/calculi/function.rb', line 49

def body(&body_proc)
  if block_given?
    @body = body_proc
  end

  @body
end

#call(context, *args) ⇒ Object

Parameters:

  • context

    the object that will actually be calling the function, i.e. what the value of ‘self` should be.

  • args

    any arguments to pass to the body of the function



20
21
22
23
24
# File 'lib/calculi/function.rb', line 20

def call(context, *args)
  fn_caller = Calculi::Caller.new(self, context)

  context.instance_exec(target, fn_caller, args, &body)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/calculi/function.rb', line 26

def eql?(other)
  other.is_a?(Calculi::Function) && other.hash == hash
end

#hashObject



30
31
32
# File 'lib/calculi/function.rb', line 30

def hash
  [function_set.hash, key.hash].hash
end

#inspectObject Also known as: to_s



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

def inspect
  "Calculi::Function(:#{key}, :dependencies => #{inspect_dependency_names})"
end

#inspect_dependency_namesObject



38
39
40
41
42
43
44
# File 'lib/calculi/function.rb', line 38

def inspect_dependency_names
  mapped = dependency_names.map do |name|
    ":#{name}"
  end.join(', ')

  "[#{mapped}]"
end

#name(&name_proc) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/calculi/function.rb', line 57

def name(&name_proc)
  if block_given?
    @name = name_proc
  end

  @name
end

#realized_bodyObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/calculi/function.rb', line 74

def realized_body
  #@realized_body ||= begin
    #__fn = self

    #->(*args) { __fn.call(self, *args) }
  #end
  instance_variable_compute 'realized_body' do
    __fn = self

    ->(*args) { __fn.call(self, *args) }
  end
end

#realized_nameObject



70
71
72
# File 'lib/calculi/function.rb', line 70

def realized_name
  @realized_name ||= target.instance_eval(&@name)
end

#requires(*deps) ⇒ Object



65
66
67
# File 'lib/calculi/function.rb', line 65

def requires(*deps)
  dependency_names.merge deps.flatten.map(&:to_s)
end