Module: FunctionalScheme

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

Overview

Functional programming main functions

Instance Method Summary collapse

Methods included from FunctionalSchemeHelper

#arg_finder, #arg_finder_helper, #define_func_helper, #define_function, #define_var, #equalize_lists, #eval_lambda, #fetch_define, #find_params_lambda, #foldl_helper, #foldr_helper, #member_helper, #proc_lambda, #proc_lambda_helper, #set_values_define

Methods included from Optimize

#apply_helper, #build_compose_expr, #call_compose, #call_compose_helper, #define_var_stl, #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

#apply(other) ⇒ Object



261
262
263
264
265
266
267
# File 'lib/lisp/interpreter/core/functional.rb', line 261

def apply(other)
  raise arg_err_build 'at least 2', 0 if other.nil? || other.empty?
  func, other = valid_function other
  raise arg_err_build 'at least 2', 1 if other.empty?
  values = find_all_values other
  apply_helper func, values
end

#compose(other) ⇒ Object



269
270
271
272
273
274
275
# File 'lib/lisp/interpreter/core/functional.rb', line 269

def compose(other)
  if other[0] != 'compose'
    do_not_call_compose other
  else
    call_compose other
  end
end

#define(other) ⇒ Object



285
286
287
288
# File 'lib/lisp/interpreter/core/functional.rb', line 285

def define(other)
  raise arg_err_build 2, other.size if other.size < 2
  fetch_define other
end

#filter(other) ⇒ Object



235
236
237
238
239
240
241
242
# File 'lib/lisp/interpreter/core/functional.rb', line 235

def filter(other)
  raise arg_err_build 2, other.size if other.size < 2
  func, other = valid_function other
  raise arg_err_build 2, 1 if other.empty?
  values = find_all_values other
  values = find_list_function_value [values[0]]
  filter_helper func, values
end

#foldl(other) ⇒ Object



221
222
223
224
225
226
# File 'lib/lisp/interpreter/core/functional.rb', line 221

def foldl(other)
  raise arg_err_build 'at least 2', other.size if other.size < 2
  func, other = valid_function other
  val_one, val_two = get_fold_values other
  foldl_helper func, val_one, val_two
end

#foldr(other) ⇒ Object



228
229
230
231
232
233
# File 'lib/lisp/interpreter/core/functional.rb', line 228

def foldr(other)
  raise arg_err_build 'at least 2', other.size if other.size < 2
  func, other = valid_function other
  val_one, val_two = get_fold_values other
  foldr_helper func, val_one, val_two
end

#lambda(other) ⇒ Object



277
278
279
280
281
282
283
# File 'lib/lisp/interpreter/core/functional.rb', line 277

def lambda(other)
  if other[0] == 'lambda'
    eval_lambda other[1..-1]
  else
    proc_lambda other
  end
end

#member(other) ⇒ Object



244
245
246
247
248
249
250
251
# File 'lib/lisp/interpreter/core/functional.rb', line 244

def member(other)
  raise arg_err_build 2, other.size unless other.size == 2
  to_check = other[0]
  split_val = split_list_string other[1]
  raise 'Invalid argument' unless split_val.pair? || split_val.list?
  values = find_all_values split_val[2..-2]
  member_helper to_check, values
end

#remove(other) ⇒ Object



253
254
255
256
257
258
259
# File 'lib/lisp/interpreter/core/functional.rb', line 253

def remove(other)
  raise arg_err_build 2, other.size unless other.size == 2
  to_remove = other[0]
  values = find_list_function_value [other[1]]
  values.delete_at(values.index(to_remove) || values.length)
  build_list values
end