<commit_before>import calc from './calc.js';

const literal = {
  even: n => !(n % 2),
  odd:  n => !!(n % 2),
};

/**
 * TODO: optimization
 */
function nth(input, curr, max) {
  for (let i = 0; i <= max; ++i) {
    if (calc(input, { n: i }) == curr) {
      return true;
    }
  }
}

export default {

  at({ x, y }) {
    return (x1, y1) => (x == x1 && y == y1);
  },

  nth({ count, grid }) {
    return (...exprs) => exprs.some(expr =>
      literal[expr]
        ? literal[expr](count)
        : nth(expr, count, grid.count)
    );
  },

  row({ y, grid }) {
    return (...exprs) => exprs.some(expr =>
      literal[expr]
        ? literal[expr](y)
        : nth(expr, y, grid.y)
    );
  },

  col({ x, grid }) {
    return (...exprs) => exprs.some(expr =>
      literal[expr]
        ? literal[expr](x)
        : nth(expr, x, grid.x)
    );
  },

  even({ count, grid, x, y }) {
    return arg => literal.odd(x + y);
  },

  odd({ count, grid, x, y}) {
    return arg => literal.even(x + y);
  },

  random({ random }) {
    return (ratio = .5) => {
      if (ratio >= 1 && ratio <= 0) ratio = .5;
      return random() < ratio;
    }
  },

  match({ count, grid, x, y, random }) {
    return expr => {
      return !!calc('(' + expr + ')', {
        x, X: grid.x,
        y, Y: grid.y,
        i: count, I: grid.count,
        random,
      });
    }
  },

}
<commit_msg>Simplify calc in random selector (#83)<commit_after>import calc from './calc.js';

const literal = {
  even: n => !(n % 2),
  odd:  n => !!(n % 2),
};

/**
 * TODO: optimization
 */
function nth(input, curr, max) {
  for (let i = 0; i <= max; ++i) {
    if (calc(input, { n: i }) == curr) {
      return true;
    }
  }
}

export default {

  at({ x, y }) {
    return (x1, y1) => (x == x1 && y == y1);
  },

  nth({ count, grid }) {
    return (...exprs) => exprs.some(expr =>
      literal[expr]
        ? literal[expr](count)
        : nth(expr, count, grid.count)
    );
  },

  row({ y, grid }) {
    return (...exprs) => exprs.some(expr =>
      literal[expr]
        ? literal[expr](y)
        : nth(expr, y, grid.y)
    );
  },

  col({ x, grid }) {
    return (...exprs) => exprs.some(expr =>
      literal[expr]
        ? literal[expr](x)
        : nth(expr, x, grid.x)
    );
  },

  even({ count, grid, x, y }) {
    return arg => literal.odd(x + y);
  },

  odd({ count, grid, x, y}) {
    return arg => literal.even(x + y);
  },

  random({ random, count, x, y, grid }) {
    return (ratio = .5) => {
      if (/\D/.test(ratio)) {
        return random() < calc('(' + ratio + ')', {
          x, X: grid.x,
          y, Y: grid.y,
          i: count, I: grid.count,
          random,
        });
      }
      return random() < ratio;
    }
  },

  match({ count, grid, x, y, random }) {
    return expr => {
      return !!calc('(' + expr + ')', {
        x, X: grid.x,
        y, Y: grid.y,
        i: count, I: grid.count,
        random,
      });
    }
  },

}

{'fraction_non_alphanumeric': 0.21648091860857818, 'fraction_numerical': 0.0070921985815602835, 'mean_word_length': 1.7630597014925373, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1405672', 'n_tokens_mistral': 1150, 'n_tokens_neox': 1126, 'n_words': 287}