Module: LazyFuncGeneratorSettingUtility

Included in:
LazyFuncGenerator, LazyFuncGeneratorSetting
Defined in:
lib/statsailr/block_to_r/sts_lazy_func_gen.rb

Instance Method Summary collapse

Instance Method Details

#is_param_name?(val) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 204

def is_param_name?( val )
  val.is_a? RBridge::RParamName
end

#is_r_obj?(val) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 208

def is_r_obj?(val)
  RBridge.is_pointer?(val)
end

#is_result_name?(val) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 196

def is_result_name?( val )
  val.is_a? RBridge::RResultName
end

#is_result_name_array?(val) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 200

def is_result_name_array?( val )
  val.is_a? RBridge::RResultNameArray
end

#one_from(name, *addl) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 212

def one_from( name, *addl )
  if addl.empty?
    if ! ( is_result_name?(name) || is_result_name_array?(name) || is_param_name?(name) ||  is_pointer?(name) )
        raise "one_from() create RBridge::RNameContainer, which only stores RResultName, RParamName or R object(pointer)."
    end
    return name
  else
    ary = ([name] + addl).map(){|elem|
      elem
    }
    return RBridge::RNameContainer.new(ary)
  end
end

#param(name) ⇒ Object



180
181
182
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 180

def param( name )
  return RBridge::RParamName.new(name)
end

#previous_inst_nameObject



188
189
190
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 188

def previous_inst_name()
  return RBridge::RInstPrevious.new()
end

#previous_or(default_obj) ⇒ Object



184
185
186
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 184

def previous_or( default_obj )
  return RBridge::RResultPrevious.new( default_obj )
end

#r_obj(val) ⇒ Object



192
193
194
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 192

def r_obj( val )
  return RBridge.convert_to_r_object( val )
end

#read_as_formula(ary) ⇒ Object



4
5
6
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 4

def read_as_formula(ary)
  return RBridge.create_formula_from_syms( ary )
end

#read_as_intvec(ary) ⇒ Object



24
25
26
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 24

def read_as_intvec(ary)
  return RBridge.create_intvec( ary )
end

#read_as_numvec(ary) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 16

def read_as_numvec(ary)
  if ary.any?(){|elem| elem.is_a?(Float) }
    read_as_realvec(ary)
  else
    read_as_intvec(ary)
  end
end

#read_as_one_str(ary) ⇒ Object



12
13
14
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 12

def read_as_one_str(ary)
  return RBridge.create_strvec( [ ary.map(){|elem| elem.to_s }.join(" ") ] )
end

#read_as_realvec(ary) ⇒ Object



28
29
30
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 28

def read_as_realvec(ary)
  return RBridge.create_realvec( ary )
end

#read_as_strvec(ary) ⇒ Object



8
9
10
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 8

def read_as_strvec(ary)
  return RBridge.create_strvec( ary.map(){|elem| elem.to_s } )
end

#read_as_symbol(ary) ⇒ Object



32
33
34
35
36
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 32

def read_as_symbol(ary)
  raise "main argument is expected to be length of 1" unless ary.length == 1
  raise "symbol is expected" unless ary[0].is_a?( RBridge::SymbolR )
  return ary[0].to_r_symbol
end

#read_named_args_as_named_strvec(ary) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 87

def read_named_args_as_named_strvec(ary)
  arg_num_ary = Array.new( ary.size )
  deapth_ary = Array.new( ary.size )
  idx = 0
  last_idx = ary.size - 1
  arg_num = nil
  deapth = nil

  while( idx <= last_idx )
    if( idx == 0 )
      # name starts
      unless ( deapth.nil? && ary[idx].is_a?(RBridge::SymbolR) && (ary[idx + 1].is_a?(RBridge::SignR) && ary[idx + 1].to_s == "="))
        raise "read_named_args_as_named_strvec requires an argument to start with name=expr"
      end
      arg_num = 0
      deapth = 0
    elsif( deapth == 0 && ary[idx].is_a?(RBridge::SymbolR) && (ary[idx + 1].is_a?(RBridge::SignR) && ary[idx + 1].to_s == "="))
      # name starts
      arg_num = arg_num + 1
    elsif( ary[idx].is_a?(RBridge::SymbolR) && (ary[idx + 1].is_a?(RBridge::SignR) && ary[idx + 1].to_s == "("))
      # function starts
      deapth_ary[idx] = deapth
      arg_num_ary[idx] = arg_num
      deapth = deapth + 1
      idx = idx + 1
      deapth_ary[idx] = deapth
    elsif( ary[idx].is_a?(RBridge::SignR) && ary[idx].to_s == "(")
      # parenthesis starts
      deapth = deapth + 1
      deapth_ary[idx] = deapth
    elsif( ary[idx].is_a?(RBridge::SignR) && ary[idx].to_s == ")")
      # parenthesis ends or function ends
      deapth_ary[idx] = deapth
      deapth = deapth - 1
    else
      deapth_ary[idx] = deapth
    end
    arg_num_ary[idx] = arg_num
    idx = idx + 1
  end

  result_ary = []
  name_ary = []
  prev_arg_num = -1
  idx = 0
  elem_arg_num_ary = ary.zip( arg_num_ary )
  while( idx < elem_arg_num_ary.size )
    elem = ary[idx]
    arg_num = arg_num_ary[idx]
    if elem.respond_to? :to_s_for_r_parsing
      elem_str = elem.to_s_for_r_parsing
    else
      elem_str = elem.to_s
    end

    if( arg_num != prev_arg_num ) # starts new 'name=expr'
      name_ary.push elem_str
      idx = idx + 1
      if( ary[idx].to_s != "=" )
        raise "An argument should be in the form of 'name=expr'"
      end
    else
      if( result_ary[arg_num].nil? )
        unless result_ary.size == arg_num
          raise "arg_num is not an appropriate number."
        end
        result_ary[arg_num] = [elem_str]
      else
        result_ary[arg_num].push( elem_str )
      end
    end
    idx = idx + 1
    prev_arg_num = arg_num
  end

  result_vec = RBridge.create_strvec( result_ary.map(){|ary| ary.join(" ") } )
  name_vec = RBridge.create_strvec( name_ary )
  add_name_func = RBridge.create_ns_function_call("stats", "setNames", {"object" => result_vec, "nm" => name_vec })
  result_name_vec = RBridge.exec_function( add_name_func )
  return result_name_vec
end

#read_symbols_as_strvec(ary) ⇒ Object



38
39
40
41
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 38

def read_symbols_as_strvec(ary)
  raise "symbol is expected as an element" unless ary.all?{|elem| elem.is_a?( RBridge::SymbolR )}
  return RBridge.create_strvec( ary.map(){|elem| elem.to_s } )
end

#read_symbols_or_functions_as_strvec(ary) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 43

def read_symbols_or_functions_as_strvec(ary)
  deapth_ary = Array.new( ary.size )
  idx = 0
  last_idx = ary.size - 1
  deapth = 0
  while( idx <= last_idx )
    if( ary[idx].is_a?(RBridge::SymbolR) && (ary[idx + 1].is_a?(RBridge::SignR) && ary[idx + 1].to_s == "("))
      # function starts
      deapth_ary[idx] = deapth
      deapth = deapth + 1
      idx = idx + 1
      deapth_ary[idx] = deapth
    elsif( ary[idx].is_a?(RBridge::SignR) && ary[idx].to_s == "(")
      # parenthesis starts
      deapth = deapth + 1
      deapth_ary[idx] = deapth
    elsif( ary[idx].is_a?(RBridge::SignR) && ary[idx].to_s == ")")
      # parenthesis ends or function ends
      deapth_ary[idx] = deapth
      deapth = deapth - 1
    else
      deapth_ary[idx] = deapth
    end
    idx = idx + 1
  end

  result_ary = []
  ary.zip( deapth_ary).each(){|elem, deapth|
    if elem.respond_to? :to_s_for_r_parsing
      elem_str = elem.to_s_for_r_parsing
    else
      elem_str = elem.to_s
    end

    if( deapth == 0)
      result_ary.push( elem_str )
    else
      result_ary.last << " " << elem_str
    end
  }

  return RBridge.create_strvec( result_ary )
end

#result(name, *addl) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/statsailr/block_to_r/sts_lazy_func_gen.rb', line 169

def result( name , *addl )
  if addl.empty?
    return RBridge::RResultName.new(name)
  else
    ary = ([name] + addl).map(){|elem|
      RBridge::RResultName.new(elem)
    }
    return RBridge::RResultNameArray.new(ary)
  end
end