How to use the @mathigon/core.flatten function in @mathigon/core

To help you get started, we’ve selected a few @mathigon/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mathigon / textbooks / transformations-and-symmetry / functions.js View on Github external
function drawShape($step, $geopad, goal, shape, expand=false) {
  // `shape` should be names of a polygon, segment or line.
  const lines = flatten(words(shape).map(s => $geopad.model[s].edges || $geopad.model[s]));

  $geopad.setActiveTool('line');

  $geopad.waitForPaths(lines, {
    onCorrect(path, i) {
      if (expand) expandSegment($geopad, path.points, lines[i]);
    },
    onIncorrect() {
      $step.addHint('incorrect', {force: true});
    },
    onHint() {
      $step.addHint('draw-hint', {force: true});
    },
    onComplete(afterError) {
      if (!afterError) $step.addHint('correct');
      $step.score(goal);
github mathigon / textbooks / content / transformations-and-symmetry / functions.js View on Github external
function drawShape($step, $geopad, goal, shape, expand=false) {
  // `shape` should be names of a polygon, segment or line.
  const lines = flatten(words(shape).map(s => $geopad.model[s].edges || $geopad.model[s]));

  $geopad.setActiveTool('line');

  $geopad.waitForPaths(lines, {
    onCorrect(path, i) {
      if (expand) expandSegment($geopad, path.points, lines[i]);
    },
    onIncorrect() {
      $step.addHint('incorrect', {force: true});
    },
    onHint() {
      $step.addHint('draw-hint', {force: true});
    },
    onComplete(afterError) {
      if (!afterError) $step.addHint('correct');
      $step.score(goal);
github mathigon / textbooks / content / transformations / functions.ts View on Github external
function drawShape($step: Step, $geopad: Geopad, goal: string, shape: string,
                   expand = false) {
  // `shape` should be names of a polygon, segment or line.
  const lines = flatten(
      words(shape).map(s => $geopad.model[s].edges || $geopad.model[s]));

  $geopad.setActiveTool('line');

  $geopad.waitForPaths(lines, {
    onCorrect(path, i) {
      if (expand) expandSegment($geopad, path.points, lines[i]);
    },
    onIncorrect() {
      $step.addHint('incorrect', {force: true});
    },
    onHint() {
      $step.addHint('draw-hint', {force: true});
    },
    onComplete(afterError) {
      if (!afterError) $step.addHint('correct');
github mathigon / textbooks / content / sequences / functions.ts View on Github external
export function pascalSequences($step: Step) {
  const $rows = $step.$$('.r').map($r => $r.$$('.c'));
  const $cells = flatten($rows);
  const $tabbox = $step.$('x-tabbox') as Tabbox;
  const $body = $tabbox.$('.body')!;
  const $sums = $rows.map($row => last($row));

  // Fibonacci numbers are coloured by diagonal
  const $fibonacci = tabulate(() => [] as ElementView[], 16 * 2 + 1);  // 16 -> number of rows
  $rows.forEach(($cells, i) => $cells.forEach(($c, j) => {
    if (i + i - j >= 0) $fibonacci[i + i - j].push($c);
  }));

  function onChange(i: number) {
    colourIndex += 1;
    $body.setAttr('class', 'body s-' + colours[i]);

    if (i === 4) $sums.forEach(($s, t) => { $s.textStr = Math.pow(2, t); });
    if (i === 6) $sums.forEach(($s, t) => { $s.textStr = fibonacci(t); });
github mathigon / textbooks / content / transformations / components / wallpaper.js View on Github external
function grid(points, x, y) {
  return flatten(tabulate((i, j) => points.map(p => p.shift(i * x, j * y)),
    width/x, height/y));
}
github mathigon / textbooks / transformations-and-symmetry / components / wallpaper.js View on Github external
function grid(points, x, y) {
  return flatten(tabulate((i, j) => points.map(p => p.shift(i * x, j * y)),
    width/x, height/y));
}
github mathigon / fermat.js / src / expression.js View on Github external
for (let p of placeholders) {
      if (!p) return null;
      for (let key of Object.keys(p)) {
        if (key in combined) {
          if (!same(combined[key], p[key])) return null;
        } else {
          combined[key] = p[key]
        }
      }
    }

    return combined;
  }, ...argPlaceholders.map(a => a.length));

  return flatten(possibilities).filter(p => !!p);
}
github mathigon / textbooks / content / sequences / functions.ts View on Github external
export function modular1($step: Step) {
  const $rows = $step.$$('.r').map($r => $r.$$('.c'));
  const $cells = flatten($rows);

  for (const $btn of $step.$$('.pascal-buttons .btn')) {
    const x = +$btn.data.value!;
    const colour = ['red', 'blue', 'green', 'yellow'][x - 2];
    const fn = (i: number, j: number, n: number) => (n % x === 0) ? colour : '';
    $btn.on('click', () => {
      colourIndex += 1;
      colourPascal($rows, $cells, fn, colourIndex);
      $step.score('c' + x);
    });
  }
}