Module: Optimize

Included in:
FunctionalSchemeHelper
Defined in:
lib/lisp/interpreter/functional.rb

Overview

Optimization module

Instance Method Summary collapse

Instance Method Details

#apply_helper(func, values) ⇒ Object



51
52
53
54
# File 'lib/lisp/interpreter/functional.rb', line 51

def apply_helper(func, values)
  return func.call(*values) if func.is_a? Proc
  send func, values
end

#build_compose_expr(funcs) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/lisp/interpreter/functional.rb', line 73

def build_compose_expr(funcs)
  expr = ['(', 'x', ')']
  funcs.each do |f|
    expr << '('
    expr << f
  end
  expr << 'x'
  funcs.size.times { expr << ')' }
  expr
end

#call_compose(other) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/lisp/interpreter/functional.rb', line 64

def call_compose(other)
  funcs, idx, tmp = call_compose_helper other
  value, = find_next_value tmp[idx + 1..-1]
  funcs.reverse.each do |t|
    value = calc_input_val ['(', t, value.to_s, ')']
  end
  value
end

#call_compose_helper(other) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/lisp/interpreter/functional.rb', line 56

def call_compose_helper(other)
  tmp = ['(', *other[1..-1]]
  idx = find_bracket_idx tmp, 0
  funcs = find_all_values tmp[1..idx - 1]
  raise '' if tmp[idx + 1..-1].empty?
  [funcs, idx, tmp]
end

#do_not_call_compose(other) ⇒ Object



84
85
86
87
88
89
# File 'lib/lisp/interpreter/functional.rb', line 84

def do_not_call_compose(other)
  funcs = find_all_values other
  raise 'Incorrect data type' if funcs.any? { |t| t.to_s.number? }
  expr = build_compose_expr funcs
  proc_lambda expr
end

#fetch_inner_scope(scope, idx = 0, def_vars = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/lisp/interpreter/functional.rb', line 30

def fetch_inner_scope(scope, idx = 0, def_vars = {})
  until idx >= scope.size
    if scope[idx] == 'define'
      idx, scope, def_vars = rm_from_in_scope scope, idx - 1, def_vars
    else
      idx += 1
    end
  end
  inner_scope_replace scope, def_vars
end

#filter_helper(func, values) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/lisp/interpreter/functional.rb', line 41

def filter_helper(func, values)
  result =
    if func.is_a? Proc
      values.select { |t| func.call(*t) == '#t' }
    else
      values.select { |t| (send func, [t]) == '#t' }
    end
  build_list result
end

#fold_values_helper(other) ⇒ Object



3
4
5
6
# File 'lib/lisp/interpreter/functional.rb', line 3

def fold_values_helper(other)
  other = other.map { |t| find_list_function_value [t] }
  (equalize_lists other).transpose
end

#get_fold_values(other) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/lisp/interpreter/functional.rb', line 8

def get_fold_values(other)
  values = find_all_values other
  raise 'Incorrect number of arguments' if values.empty?
  x = values[0]
  y = fold_values_helper values[1..-1]
  [x, y]
end

#inner_scope_replace(scope, vars) ⇒ Object



23
24
25
26
27
28
# File 'lib/lisp/interpreter/functional.rb', line 23

def inner_scope_replace(scope, vars)
  scope.each_with_index do |t, i|
    scope[i] = vars[t.to_s] if vars.key? t.to_s
  end
  scope.flatten
end

#rm_from_in_scope(scope, idx, def_vars) ⇒ Object



16
17
18
19
20
21
# File 'lib/lisp/interpreter/functional.rb', line 16

def rm_from_in_scope(scope, idx, def_vars)
  i = find_bracket_idx scope, idx
  def_vars[scope[idx + 2].to_s] = scope[idx + 3..i - 1]
  scope.slice!(idx..i)
  [i + 1, scope, def_vars]
end