本文整理汇总了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
)
];
}
示例2:
choice<T>(arr: T[]): T {
return randomjs.pick(this.engine, arr);
}
示例3:
export function pick<T>(array: T[], begin?: number, end?: number): T {
return Random.pick(engine, array, begin, end);
}
示例4: breedIndividuals
function breedIndividuals(random: Random, mum: Entity, dad: Entity): Entity[] {
const strategy = random.pick([cat, swap, swap]);
return strategy(random, mum, dad);
}
示例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)
};
//.........这里部分代码省略.........
示例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])
};
//.........这里部分代码省略.........