import buildModel from 'state/utils/buildModel';

const keypath = 'level';

const groundsOwnKeypath = 'grounds';
const groundsKeypath = [keypath, groundsOwnKeypath];

const entitiesOwnKeypath = 'entities';
const entitiesKeypath = [keypath, entitiesOwnKeypath];

const grounds = (s) => s.getIn(groundsKeypath);
const entities = (s) => s.getIn(entitiesKeypath);

const tileAt = curry((kp, col, row, state) => {
  return state.getIn([...kp, row, col]);
});

const setTilePropAt = curry((kp, col, row, key, val, state) => {
  return state.setIn([...kp, row, col, key], val);
});

const deleteTileAt = curry((kp, col, row, state) => {
  return state.deleteIn([...kp, row, col]);
});

const api = buildModel(keypath, [
  entitiesOwnKeypath,
  groundsOwnKeypath,
  'numTapesTotal',
  'playerStart',
import curry from 'lodash/function/curry';
import capitalize from 'lodash/string/capitalize';
import camelCase from 'lodash/string/camelCase';

import ensureArray from 'utils/ensureArray';

const getProp = curry((keypath, state) => state.getIn(keypath));
const setProp = curry((keypath, value, state) => state.setIn(keypath, value));

export default function buildModel(keypath, props) {
  keypath = ensureArray(keypath);
  return props.reduce((api, prop) => {
    prop = ensureArray(prop);
    const camelized = capitalize(camelCase(prop));
    const propKeypath = [...keypath, ...prop];
    api[`get${camelized}`] = getProp(propKeypath);
    api[`set${camelized}`] = setProp(propKeypath);
    return api;
  }, {});
};
示例#3
0
 return mapValues(helpers, (method) => {
   return curry(function(text) {
     let render = (value) => hogan.compile(value, compileOptions).render(this);
     return method.call(data, text, render);
   });
 });
import curry from 'lodash/function/curry';
import flow from 'lodash/function/flow';
import isFunction from 'lodash/lang/isFunction';
import negate from 'lodash/function/negate';

export const returnTrue = () => true;
export const not = negate;

const buildAbility = (prop) => (state, entity) => {
  const fn = entity.get(prop);
  return isFunction(fn) ? fn(state, entity) : false;
};

export const canBlock = buildAbility('canBlock');
export const canCollect = buildAbility('canCollect');
export const canDie = buildAbility('canDie');
export const canDestroy = buildAbility('canDestroy');
export const canKill = buildAbility('canKill');
export const canWin = buildAbility('canWin');

const hasPowerup = curry((powerup, state, entity) => {
  return state.get('powerups').includes(powerup);
});

export const hasBoots = hasPowerup('boots');
export const hasHammer = hasPowerup('hammer');
export const hasSilverware = hasPowerup('silverware');
export const hasSpeedboat = hasPowerup('speedboat');
export const hasSunglasses = hasPowerup('sunglasses');
示例#5
0
(function() {
  'use strict';

  function* traverse(store, graph, startVertex) {
    // Output
    let output = {
      // Lexicographical order vertices
      lexicographical: [],
      // Parenthetical order vertices
      parenthetical: [],
      // Arcs in lexicographical order
      arcs: [],
      minCapacity: Infinity,
      currentArc: null,
      currentVertex: null,
      progress: true
    }
    // Set of vertices
    let V = graph.vertices;
    // Start vertex
    let s = startVertex || graph.source;

    // Minimum capacities
    let minCapacities = [];
    let lastMinCapacity = Infinity;
    let overallMinCapacity = Infinity;

    // First element of the store
    let v;
    // Next arc
    let a;

    // Initialize traversal
    store.push(s);
    s.seen = true;
    output.lexicographical.push(s);

    // Set the level for the source to 0 for building a level graph (only relevant for Dinic)
    s.setLevel(0);

    while (!store.empty) {
      // Retrieve the current vertex from the top of the stack or the front of the queue
      v = store.top();
      // Get next arc in outgoing arc list of v
      a = v.nextArc();

      // Update state information
      output.currentArc = a;
      output.currentVertex = v;
      output.progress = true;

      if (a === null) {
        // Vertex v has no more outgoing in its outgoing arc list
        v.finished = true;
        store.pop();

        // Remove arc because output.arcs should only contain arcs that lead to a termination node
        output.arcs.pop();
        // Remove last minimum capacity because it refers to an arc that is no longer parts of the arc array
        minCapacities.pop();
        // Find new last minimum capacity
        lastMinCapacity = minCapacities[minCapacities.length - 1];
        lastMinCapacity = lastMinCapacity === undefined ? Infinity : lastMinCapacity;

        output.parenthetical.push(v);
      } else if (!a.to.seen && a.capacity > 0) {
        // Mark target node of arc a as seen
        a.to.seen = true;
        store.push(a.to);

        // Set the target vertex' level (only relevant for Dinic and the level graph it requires)
        a.to.setLevel(v.level + 1);

        // Set properties for finding paths with BFS
        a.to.parent = a.from;
        a.to.parentArc = a;
        a.to.parentArcMinCapacity = a.from.parentArcMinCapacity === undefined ? a.capacity : Math.min(a.from.parentArcMinCapacity, a.capacity);

        // Add a new value to the min capacities array
        minCapacities.push(Math.min(lastMinCapacity, a.capacity));
        lastMinCapacity = minCapacities[minCapacities.length - 1];

        // Update the observed minimum capacity w.r.t. the whole graph
        overallMinCapacity = Math.min(overallMinCapacity, a.capacity);

        output.lexicographical.push(a.to);
        output.arcs.push(a);
      } else {
        output.progress = false;
      }

      output.minCapacity = lastMinCapacity === Infinity ? overallMinCapacity : lastMinCapacity;

      // Yield controll to the caller
      // Procedure is resumed when caller uses .next()
      yield output;
    }
    // BREAK CONDITION: store (queue or stack) is empty


    return null;
  }

  function init(store, graph, startVertex) {
    if (!(store instanceof queue.Queue) && !(store instanceof stack.Stack)) {
      throw new Error('Unsupported data structure. Please provide queue or stack');
    }

    return traverse(store, graph, startVertex);
  }

  exports.init = curry(init, 2);

  exports.run = (traverse, termination) => {
    let output = null;
    let result = traverse.next();
    let last;

    while (!result.done) {
      output = result.value;
      last = output.lexicographical[output.lexicographical.length - 1];

      // Check if the currently last visited vertex is the termination vertex
      if (termination && last && last.equals(termination)) {
        // Terminate
        result.done = true;
      } else {
        result = traverse.next();
      }

    }

    return output;
  };
}());
import curry from 'lodash/function/curry';

export default curry((min, max, num) => {
  return Math.max(Math.min(num, max), min);
});
示例#7
0
文件: invoke.js 项目: jhamlet/protean
var curry    = require('lodash/function/curry');
var methodOf = require('lodash/utility/methodOf');
/**
 * @member module:Protean.invoke
 * @param {String} property
 * @param {Object} subject
 * @returns {*}
 */
module.exports = curry(function (path, object) {
    return methodOf(object)(path);
});
import curry from 'lodash/function/curry';
import rearg from 'lodash/function/rearg';

import { entities } from 'state/definitions/entities';
import { grounds } from 'state/definitions/grounds';

const splitter = /\s+\|?\s*/;

const createTile = curry((defs, row, col, shortType) => {
  return (
    (shortType in defs) ?
    { ...defs[shortType], row, col, shortType } :
    null
  );
});

const parseLevelGrid = curry((defs, data) => {
  return data
    .map((row) => row.split(splitter))
    .map((row, rowIdx) => {
      const createItem = rearg(createTile(defs, rowIdx), 1, 0);
      return row.map(createItem);
    });
});

export const parseGrounds = parseLevelGrid(grounds);
export const parseEntities = parseLevelGrid(entities);