当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript random-js.pick函数代码示例

本文整理汇总了TypeScript中random-js.pick函数的典型用法代码示例。如果您正苦于以下问题:TypeScript pick函数的具体用法?TypeScript pick怎么用?TypeScript pick使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了pick函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: swap

function swap(random: Random, mum: Entity, dad: Entity): Entity[] {
  const nodeToSwapFromMum = findNode(mum, random.pick(nodeIds(mum))) as Entity;
  const nodeToSwapFromDad = findNode(dad, random.pick(nodeIds(dad))) as Entity;

  if (!nodeToSwapFromMum || !nodeToSwapFromDad) {
    return [mum, dad];
  }

  return [
    mapTree(
      mum,
      entity =>
        entity.id === nodeToSwapFromMum.id ? nodeToSwapFromDad : entity
    ),
    mapTree(
      dad,
      entity =>
        entity.id === nodeToSwapFromDad.id ? nodeToSwapFromMum : entity
    )
  ];
}
开发者ID:helix-pi,项目名称:helix-pi,代码行数:21,代码来源:index.ts

示例2:

 choice<T>(arr: T[]): T {
     return randomjs.pick(this.engine, arr);
 }
开发者ID:sbj42,项目名称:sbj42.github.io,代码行数:3,代码来源:random.ts

示例3:

export function pick<T>(array: T[], begin?: number, end?: number): T {
    return Random.pick(engine, array, begin, end);
}
开发者ID:MPennanti,项目名称:HungryDragon,代码行数:3,代码来源:random.ts

示例4: breedIndividuals

function breedIndividuals(random: Random, mum: Entity, dad: Entity): Entity[] {
  const strategy = random.pick([cat, swap, swap]);

  return strategy(random, mum, dad);
}
开发者ID:helix-pi,项目名称:helix-pi,代码行数:5,代码来源:index.ts

示例5: generateEntity

export function generateEntity(
  seed: number,
  keys: string[],
  depth = 0
): Entity {
  const random = new Random(Random.engines.mt19937().seed(seed));

  const isLeaf = depth > 1 || random.bool();

  if (isLeaf) {
    const possibleCommands = [
      "move",
      "setVelocity",
      "multiplyVelocity",
      "noop"
    ];

    const command = random.pick(possibleCommands);

    if (command === "noop") {
      return {
        type: command,
        id: seed.toString()
      };
    }

    if (command === "move") {
      const direction = random.pick([
        "up",
        "right",
        "left",
        "down"
      ]) as Direction;
      const amount = random.integer(0, 20) / 10;

      return {
        type: command,
        id: seed.toString(),
        direction,
        amount
      };
    }

    if (command === "setVelocity") {
      let velocity;

      if (random.bool(0.5)) {
        // cardinal direction
        const scalar = random.integer(0, 20) / 20;

        velocity = random.pick([
          { x: 0, y: -scalar },
          { x: scalar, y: 0 },
          { x: -scalar, y: 0 },
          { x: 0, y: scalar }
        ]);
      } else {
        velocity = {
          x: random.integer(-20, 20) / 20,
          y: random.integer(-20, 20) / 20
        };
      }

      return {
        type: command,
        id: seed.toString(),
        velocity
      };
    }

    if (command === "multiplyVelocity") {
      const scalar = random.integer(-20, 20) / 20;

      return {
        type: command,
        id: seed.toString(),
        scalar
      };
    }

    throw new Error("Unimplemented command");
  } else {
    const possibleCommands = [
      "sequence",
      "inputConditional",
      "collisionConditional"
    ];

    if (depth === 0) {
      possibleCommands.push("onCreate");
    }
    // TODO - fix definitely typed definition
    const command = (random as any).pick(possibleCommands) as any;

    if (command === "collisionConditional") {
      return {
        type: command,
        id: seed.toString(),
        children: makeChildren(random, 1, keys, depth)
      };
//.........这里部分代码省略.........
开发者ID:helix-pi,项目名称:helix-pi,代码行数:101,代码来源:index.ts

示例6: chooseMutation

function chooseMutation(
  keys: string[],
  seed: number,
  entity: Entity
): Mutation {
  const random = new Random(Random.engines.mt19937().seed(seed));

  if (random.bool(0.5)) {
    return {
      id: seed.toString(),
      type: "ConvertToNoopMutation"
    };
  }

  if (entity.type === "sequence") {
    const types = [
      "NewEntityMutation",
      "RemovalMutation",
      "SwitchMutation",
      "ReplaceMutation"
    ];

    const mutationType = random.pick(types);

    if (mutationType === "NewEntityMutation") {
      const newEntity = generateEntity(
        random.integer(MIN_SEED, MAX_SEED),
        keys,
        0
      );

      return {
        id: seed.toString(),
        type: mutationType,
        newEntity,
        spliceIndex: random.integer(0, (entity as Branch).children.length)
      };
    }

    if (mutationType === "ReplaceMutation") {
      const newEntity = generateEntity(
        random.integer(MIN_SEED, MAX_SEED),
        keys,
        0
      );

      return {
        id: seed.toString(),
        type: mutationType,
        newEntity,
        spliceIndex: random.integer(0, (entity as Branch).children.length - 1)
      };
    }

    if (mutationType === "RemovalMutation") {
      return {
        id: seed.toString(),
        type: mutationType,
        removeIndex: random.integer(0, entity.children.length - 1)
      };
    }

    if (mutationType === "SwitchMutation") {
      return {
        id: seed.toString(),
        type: mutationType,
        fromIndex: random.integer(0, entity.children.length - 1),
        toIndex: random.integer(0, entity.children.length - 1)
      };
    }
  }

  if (entity.type === "onCreate") {
    const newEntity = generateEntity(
      random.integer(MIN_SEED, MAX_SEED),
      keys,
      0
    );

    return {
      id: seed.toString(),
      type: "ReplaceMutation",
      newEntity,
      spliceIndex: random.pick([0, 1])
    };
  }

  if (entity.type === "collisionConditional") {
    const newEntity = generateEntity(
      random.integer(MIN_SEED, MAX_SEED),
      keys,
      0
    );

    return {
      id: seed.toString(),
      type: "ReplaceMutation",
      newEntity,
      spliceIndex: random.pick([0, 1])
    };
//.........这里部分代码省略.........
开发者ID:helix-pi,项目名称:helix-pi,代码行数:101,代码来源:index.ts


注:本文中的random-js.pick函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。