Module: Trxl::StdLib

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

Constant Summary collapse

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