Module: Optimize
Overview
Constant Summary
Constants included
from SchemeStl
SchemeStl::DO_NOT_CALCULATE_FUNCTIONS, SchemeStl::PREDEFINED_FUNCTIONS, SchemeStl::RESERVED_KEYWORDS, SchemeStl::SPECIAL_CHARACTER_FUNCTIONS
Instance Method Summary
collapse
Instance Method Details
#apply_helper(func, values) ⇒ Object
54
55
56
57
58
59
60
|
# 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) ⇒ Object
81
82
83
84
85
86
87
88
89
90
|
# 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) ⇒ Object
70
71
72
73
74
75
76
77
78
79
|
# 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) ⇒ Object
62
63
64
65
66
67
68
|
# 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) ⇒ Object
99
100
101
102
103
|
# 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) ⇒ Object
92
93
94
95
96
97
|
# 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 = {}) ⇒ Object
33
34
35
36
37
38
39
40
41
42
|
# 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) ⇒ Object
44
45
46
47
48
49
50
51
52
|
# 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) ⇒ Object
6
7
8
9
|
# 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) ⇒ Object
11
12
13
14
15
16
17
|
# 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) ⇒ Object
26
27
28
29
30
31
|
# 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) ⇒ Object
19
20
21
22
23
24
|
# 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
|