Module: Optimize
- Included in:
- FunctionalSchemeHelper
- Defined in:
- lib/lisp/interpreter/core/functional.rb
Overview
Optimization module
Instance Method Summary collapse
- #apply_helper(func, values) ⇒ Object
- #build_compose_expr(funcs) ⇒ Object
- #call_compose(other) ⇒ Object
- #call_compose_helper(other) ⇒ Object
- #define_var_stl(var, values) ⇒ Object
- #do_not_call_compose(other) ⇒ Object
- #fetch_inner_scope(scope, idx = 0, def_vars = {}) ⇒ Object
- #filter_helper(func, values) ⇒ Object
- #fold_values_helper(other) ⇒ Object
- #get_fold_values(other) ⇒ Object
- #inner_scope_replace(scope, vars) ⇒ Object
- #rm_from_in_scope(scope, idx, def_vars) ⇒ Object
Instance Method Details
#apply_helper(func, values) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/lisp/interpreter/core/functional.rb', line 51 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) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/lisp/interpreter/core/functional.rb', line 78 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
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/lisp/interpreter/core/functional.rb', line 67 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) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/lisp/interpreter/core/functional.rb', line 59 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) ⇒ Object
96 97 98 99 100 |
# File 'lib/lisp/interpreter/core/functional.rb', line 96 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) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/lisp/interpreter/core/functional.rb', line 89 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/core/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/core/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/core/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/core/functional.rb', line 8 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) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/lisp/interpreter/core/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/core/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 |