Module: FunctionalSchemeHelper

Includes:
Optimize
Included in:
FunctionalScheme
Defined in:
lib/lisp/interpreter/functional.rb

Overview

FunctionalScheme helper

Instance Method Summary collapse

Methods included from Optimize

#apply_helper, #build_compose_expr, #call_compose, #call_compose_helper, #do_not_call_compose, #fetch_inner_scope, #filter_helper, #fold_values_helper, #get_fold_values, #inner_scope_replace, #rm_from_in_scope

Instance Method Details

#arg_finder(args) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/lisp/interpreter/functional.rb', line 188

def arg_finder(args)
  result = []
  until args.empty?
    name = predefined_method_caller [args[0]]
    temp, args = arg_finder_helper name, args
    result << temp
  end
  result
end

#arg_finder_helper(name, args) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/lisp/interpreter/functional.rb', line 179

def arg_finder_helper(name, args)
  if !name.nil?
    args = args[1..-1]
    [name, args]
  else
    find_next_value args
  end
end

#define_func_helper(other, params, args) ⇒ Object



174
175
176
177
# File 'lib/lisp/interpreter/functional.rb', line 174

def define_func_helper(other, params, args)
  temp = set_values_define other, params, args
  calc_input_val temp
end

#define_function(other) ⇒ Object



198
199
200
201
202
203
# File 'lib/lisp/interpreter/functional.rb', line 198

def define_function(other)
  idx = find_bracket_idx other, 0
  name, *params = other[1..idx - 1]
  build_fn = ['(', 'lambda', '(', *params, ')', *other[idx + 1..-1], ')']
  define_var name, (find_all_values build_fn)
end

#define_var(var, values) ⇒ Object



157
158
159
160
161
# File 'lib/lisp/interpreter/functional.rb', line 157

def define_var(var, values)
  raise 'Incorrect number of arguments' if values.size != 1
  raise 'Invalid variable name' unless valid_var_name var
  set_var_helper var, values[0]
end

#equalize_lists(other) ⇒ Object



109
110
111
112
# File 'lib/lisp/interpreter/functional.rb', line 109

def equalize_lists(other)
  min = other.map(&:size).min
  other.map { |t| t[0..min - 1] }
end

#eval_lambda(other) ⇒ Object



126
127
128
129
130
# File 'lib/lisp/interpreter/functional.rb', line 126

def eval_lambda(other)
  idx = find_bracket_idx other.unshift('('), 0
  to_eval = other[1..idx - 1]
  (proc_lambda to_eval).call(*other[idx + 1..-1])
end

#fetch_define(other) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/lisp/interpreter/functional.rb', line 149

def fetch_define(other)
  if other[0] == '('
    define_function other
  else
    define_var other[0].to_s, (find_all_values other[1..-1])
  end
end

#find_params_lambda(other) ⇒ Object



120
121
122
123
124
# File 'lib/lisp/interpreter/functional.rb', line 120

def find_params_lambda(other)
  raise 'Unbound symbol ' + other.to_s if other[0] != '('
  idx = find_bracket_idx other, 0
  [other[1..idx - 1], other[idx + 1..-1]]
end

#foldl_helper(func, accum, lst) ⇒ Object



95
96
97
98
99
100
# File 'lib/lisp/interpreter/functional.rb', line 95

def foldl_helper(func, accum, lst)
  return accum if lst.empty?
  value = func.call(*lst[0], accum.to_s) if func.is_a? Proc
  value = send func, [*lst[0], accum] if value.nil?
  foldl_helper func, value.to_s, lst[1..-1]
end

#foldr_helper(func, accum, lst) ⇒ Object



102
103
104
105
106
107
# File 'lib/lisp/interpreter/functional.rb', line 102

def foldr_helper(func, accum, lst)
  return accum if lst.empty?
  value = foldr_helper func, accum, lst[1..-1]
  return func.call(*lst[0], value.to_s) if func.is_a? Proc
  send func, [*lst[0], value.to_s]
end

#member_helper(to_check, values) ⇒ Object



114
115
116
117
118
# File 'lib/lisp/interpreter/functional.rb', line 114

def member_helper(to_check, values)
  return '#f' unless values.include? to_check
  idx = values.index(to_check)
  build_list values[idx..-1]
end

#proc_lambda(other) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/lisp/interpreter/functional.rb', line 138

def proc_lambda(other)
  params, other = find_params_lambda other
  other = fetch_inner_scope other
  to_return = other[0..1].join == '(compose' && params.empty?
  return calc_input_val other if to_return
  proc = proc do |*args|
    proc_lambda_helper other, params, args
  end
  proc
end

#proc_lambda_helper(other, params, args) ⇒ Object



132
133
134
135
136
# File 'lib/lisp/interpreter/functional.rb', line 132

def proc_lambda_helper(other, params, args)
  args = arg_finder args
  raise 'Incorrect number of arguments' unless params.size == args.size
  define_func_helper other.dup, params.dup, args
end

#set_values_define(other, params, args) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/lisp/interpreter/functional.rb', line 163

def set_values_define(other, params, args)
  args = [args] unless args.is_a? Array
  other.each_with_index do |t, idx|
    if params.include? t
      i = params.index t
      other[idx] = args[i]
    end
  end
  other
end