Module: Trxl::StdLib

Included in:
Calculator
Defined in:
lib/trxl/trxl.rb

Constant Summary collapse

FOREACH_IN =
"  foreach_in = fun(enumerable, body) {\n    if(SIZE(enumerable) > 0)\n      __foreach_in__(enumerable, body, 0)\n    end\n  };\n  __foreach_in__ = fun(enumerable, body, index) {\n    if(index < SIZE(enumerable) - 1)\n      body(enumerable[index]);\n      __foreach_in__(enumerable, body, index + 1)\n    else\n      body(enumerable[index])\n    end\n  };\n"
INJECT =
"  inject = fun(memo, enumerable, body) {\n    if(SIZE(enumerable) > 0)\n      __inject__(memo, enumerable, body, 0)\n    else\n      memo\n    end\n  };\n  __inject__ = fun(memo, enumerable, body, index) {\n    if(index < SIZE(enumerable) - 1)\n      __inject__(body(memo, enumerable[index]), enumerable, body, index + 1)\n    else\n      body(memo, enumerable[index])\n    end\n  };\n"
MAP =
"  require 'stdlib/inject';\n  map = fun(enumerable, body) {\n    __body__ = body; # WORK AROUND a bug in Trxl::Environment\n    inject([], enumerable, fun(memo, e) { memo << __body__(e); });\n  };\n"
SELECT =
"  require 'stdlib/inject';\n  select = fun(enumerable, body) {\n    __body__ = body; # WORK AROUND a bug in Trxl::Environment\n    inject([], enumerable, fun(selected, value) {\n      if(__body__(value))\n        selected << value\n      else\n        selected\n      end\n    });\n  };\n"
REJECT =
"  require 'stdlib/inject';\n  reject = fun(enumerable, filter) {\n    __filter__ = filter; # WORKAROUND for a bug in Trxl::Environment\n    inject([], enumerable, fun(rejected, value) {\n      if(__filter__(value))\n        rejected\n      else\n        rejected << value\n      end\n    })\n  };\n"
IN_GROUPS_OF =
"  require 'stdlib/foreach_in';\n  require 'stdlib/inject';\n  in_groups_of = fun(size_of_group, enumerable, group_function) {\n    count = 0; groups = []; cur_group = [];\n    foreach_in(enumerable, fun(element) {\n      if(count < size_of_group)\n        cur_group << element;\n        count = count + 1\n      end;\n      if(count == size_of_group)\n        groups << cur_group;\n        cur_group = [];\n        count = 0\n      end\n    });\n    group_count = 0;\n    inject([], groups, fun(memo, group) {\n      group_count = group_count + 1;\n      memo << group_function(group, group_count);\n      memo\n    });\n  };\n"
HASH_VALUES =
"  require 'stdlib/map';\n  hash_values = fun(hash) {\n    map(TO_ARRAY(hash), fun(pair) { pair[1] });\n  };\n"
HASH_VALUE_SUM =
"  require 'stdlib/hash_values';\n  hash_value_sum = fun(hash) {\n    SUM(hash_values(hash))\n  };\n"
AVG_HASH_VALUE_SUM =
"  require 'stdlib/hash_values';\n  avg_hash_value_sum = fun(hash) {\n    AVG_SUM(hash_values(hash))\n  };\n"
HASH_RANGE_VALUES =
"  require 'stdlib/foreach_in';\n  require 'stdlib/hash_values';\n  hash_range_values = fun(hash_range) {\n    inject([], hash_range, fun(values, hash_variable) {\n      values << hash_values(ENV[hash_variable]);\n    });\n  };\n"
HASH_RANGE_VALUE_SUM =
"  require 'stdlib/hash_range_values';\n  hash_range_value_sum = fun(hash_range) {\n    SUM(hash_range_values(hash_range))\n  };\n"
AVG_HASH_RANGE_VALUE_SUM =
"  require 'stdlib/hash_range_values';\n  avg_hash_range_value_sum = fun(hash_range) {\n    inject(0, hash_range_values(hash_range), fun(sum, bucket) {\n      sum + AVG_SUM(bucket);\n    });\n  };\n"
SUM_OF_TYPE =
"  sum_of_type = fun(type, all_types, all_values) {\n    SUM(VALUES_OF_TYPE(type, all_types, all_values));\n  };\n"
AVG_SUM_OF_TYPE =
"  avg_sum_of_type = fun(type, all_types, all_values) {\n    AVG_SUM(VALUES_OF_TYPE(type, all_types, all_values));\n  };\n"
AVG_RANGE_SUM_OF_TYPE =
"  require 'stdlib/inject';\n  require 'stdlib/avg_sum_of_type';\n  avg_range_sum_of_type = fun(type, all_types, variable_range) {\n    inject(0, variable_range, fun(sum, variable) {\n      sum + avg_sum_of_type(type, all_types, ENV[variable])\n    });\n  };\n"
TOTAL_RANGE_SUM_OF_TYPE =
"  require 'stdlib/inject';\n  require 'stdlib/sum_of_type';\n  total_range_sum_of_type = fun(type, all_types, variable_range) {\n    inject(0, variable_range, fun(sum, variable) {\n      sum + sum_of_type(type, all_types, ENV[variable])\n    });\n  };\n"
AVG_RANGE_SUM =
"  require 'stdlib/inject';\n  avg_range_sum = fun(variable_range) {\n    inject(0, variable_range, fun(sum, variable) {\n      sum + AVG_SUM(ENV[variable])\n    });\n  };\n"
YEAR_FROM_DATE =
"  year_from_date = fun(date) {\n    date = SPLIT(date, '/');\n    TO_INT(date[1]);\n  };\n"
MONTH_FROM_DATE =
"  month_from_date = fun(date) {\n    date = SPLIT(date, '/');\n    TO_INT(date[0]);\n  };\n"
DATES =
"  require 'stdlib/month_from_date';\n  require 'stdlib/year_from_date';\n"
RATIO =
"  require 'stdlib/foreach_in';\n  ratio = fun(enumerable, true_condition, base_condition) {\n    base = 0;\n    positives = 0;\n    foreach_in(enumerable, fun(val) {\n      if(ENV[val] != base_condition)\n        base = base + 1\n      end;\n      if(ENV[val] == true_condition)\n        positives = positives + 1\n      end;\n    });\n    if(base > 0)\n      ROUND((ROUND(positives, 1) / base) * 100, 2)\n    else\n      NULL\n    end\n  };\n"