module Optimize

Optimization module

Public Instance Methods

apply_helper(func, values) click to toggle source
# File lib/lisp/interpreter/core/functional.rb, line 54
def apply_helper(func, values)
  *vs, lst = values
  raise 'Incorrect data type' unless lst.list?
  (find_list_function_value [lst]).each { |t| vs << t }
  return func.call(*vs) if func.is_a? Proc
  send func, vs
end
build_compose_expr(funcs) click to toggle source
# File lib/lisp/interpreter/core/functional.rb, line 81
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) click to toggle source
# File lib/lisp/interpreter/core/functional.rb, line 70
def call_compose(other)
  funcs, idx, tmp = call_compose_helper other
  value = find_all_values tmp[idx + 1..-1]
  funcs.reverse.each do |t|
    value = value.to_s unless value.is_a? Array
    value = calc_input_val ['(', t, *value, ')']
  end
  is_arr = value.is_a? Array
  is_arr ? value[0] : value
end
call_compose_helper(other) click to toggle source
# File lib/lisp/interpreter/core/functional.rb, line 62
def call_compose_helper(other)
  tmp = ['(', *other[1..-1]]
  idx = find_bracket_idx tmp, 0
  funcs = find_all_values tmp[1..idx - 1]
  return '' if tmp[idx + 1..-1].nil? && funcs.empty?
  [funcs, idx, tmp]
end
define_var_stl(var, values) click to toggle source
# File lib/lisp/interpreter/core/functional.rb, line 99
def define_var_stl(var, values)
  valid_stl = methods.include? values.to_s[2..-3].to_sym
  return set_var var, values.to_s[2..-3].to_sym if valid_stl
  set_var var, values[0]
end
do_not_call_compose(other) click to toggle source
# File lib/lisp/interpreter/core/functional.rb, line 92
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 = {}) click to toggle source
# File lib/lisp/interpreter/core/functional.rb, line 33
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) click to toggle source
# File lib/lisp/interpreter/core/functional.rb, line 44
def filter_helper(func, values)
  result =
    if func.is_a? Proc
      values.select { |t| func.call(*t) == TRUE }
    else
      values.select { |t| (send func, [t]) == FALSE }
    end
  build_list result
end
fold_values_helper(other) click to toggle source
# File lib/lisp/interpreter/core/functional.rb, line 6
def fold_values_helper(other)
  other = other.map { |t| find_list_function_value [t] }
  (equalize_lists other).transpose
end
get_fold_values(other) click to toggle source
# File lib/lisp/interpreter/core/functional.rb, line 11
def get_fold_values(other)
  values = find_all_values other
  raise arg_err_build 'at least 2', 0 if values.empty?
  x = values[0]
  y = fold_values_helper values[1..-1]
  [x, y]
end
inner_scope_replace(scope, vars) click to toggle source
# File lib/lisp/interpreter/core/functional.rb, line 26
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) click to toggle source
# File lib/lisp/interpreter/core/functional.rb, line 19
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