c $ c> But of course there's more potential here. We can write whatever we want in the for block. Below, we add an additional condition to skip Queens Q using continueconst d1 = [ 'J', 'Q', 'K', 'A' ]const d2 = [ '♤', '♡', '♧', '♢' ]for (const [ rank, suit ] of combinations (d1, d2)){ if (rank === 'Q') continue if (rank === 'J' || suit === '♡' ) console.log (rank, suit)}// J ♤// J ♡// J ♧// J ♢// K ♡ <--- Queens dropped from the output// A ♡也许这里最强大的是我们可以停止与 break 生成组合。下面,如果遇到King K ,我们会立即停止发电机And perhaps the most powerful thing here is we can stop generating combinations with break. Below, if a King K is encountered, we halt the generator immediatelyconst d1 = [ 'J', 'Q', 'K', 'A' ]const d2 = [ '♤', '♡', '♧', '♢' ]for (const [ rank, suit ] of combinations (d1, d2)){ if (rank === 'K') break if (rank === 'J' || suit === '♡' ) console.log (rank, suit)}// J ♤// J ♡// J ♧// J ♢// Q ♡ <-- no Kings or Aces; generator stopped at K你可以根据条件变得非常有创意。对于(const [a,b,c,d,e],在心中开始或结束的所有组合怎么样? You can get pretty creative with the conditions. How about all the combinations that begin or end in a Heartfor (const [ a, b, c, d, e ] of combinations (d2, d2, d2, d2, d2)){ if (a === '♡' && e === '♡') console.log (a, b, c, d, e)}// ♡ ♤ ♤ ♤ ♡// ♡ ♤ ♤ ♡ ♡// ♡ ♤ ♤ ♧ ♡// ...// ♡ ♢ ♢ ♡ ♡// ♡ ♢ ♢ ♧ ♡// ♡ ♢ ♢ ♢ ♡并显示生成器适用于大型数据集And to show you generators work for large data setsconst d1 = [ 1, 2, 3, 4, 5, 6 ]Array.from (combinations (d1, d1, d1, d1, d1, d1, d1)) .length// 6^7 = 279936Array.from (combinations (d1, d1, d1, d1, d1, d1, d1, d1)) .length// 6^8 = 1679616我们甚至可以写得更高 - order函数用于生成器,例如我们自己的过滤器函数。下面,我们找到三个20面骰子的所有组合,形成一个 Pythagorean triple - 3个整体构成有效直角三角形边长的数字We can even write higher-order functions to work with generators such as our own filter function. Below, we find all combinations of three 20-sided dice that form a Pythagorean triple - 3 whole numbers that make up the side lengths of a valid right triangleconst filter = function* (f, iterable){ for (const x of iterable) if (f (x)) yield x}const d20 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]const combs = combinations (d20, d20, d20)const pythagoreanTriple = ([ a, b, c ]) => (a * a) + (b * b) === (c * c)for (const c of filter (pythagoreanTriple, combs)) console.log (c)// [ 3, 4, 5 ]// [ 4, 3, 5 ]// [ 5, 12, 13 ]// [ 6, 8, 10 ]// [ 8, 6, 10 ]// [ 8, 15, 17 ]// [ 9, 12, 15 ]// [ 12, 5, 13 ]// [ 12, 9, 15 ]// [ 12, 16, 20 ]// [ 15, 8, 17 ]// [ 16, 12, 20 ]或使用 Array.from ,带有映射函数,可以将每个组合同时转换为新结果并收集数组中的所有结果Or use Array.from with a mapping function to simultaneously transform each combination into a new result and collect all results in an arrayconst allResults = Array.from ( filter (pythagoreanTriple, combs) , ([ a, b, c ], index) => ({ result: index + 1, solution: `${a}² + ${b}² = ${c}²`}) )console.log (allResults)// [ { result: 1, solution: '3² + 4² = 5²' }// , { result: 2, solution: '4² + 3² = 5²' }// , { result: 3, solution: '5² + 12² = 13²' }// , ...// , { result: 10, solution: '12² + 16² = 20²' }// , { result: 11, solution: '15² + 8² = 17²' }// , { result: 12, solution: '16² + 12² = 20²' }// ] 功能是什么? 功能编程很深。潜入! const None = Symbol ()// Array ApplicativeArray.prototype.ap = function (args) { const loop = (acc, [ x = None, ...xs ]) => x === None ? this.map (f => f (acc)) : x.chain (a => loop ([ ...acc, a ], xs)) return loop ([], args) } // Array Monad (this is the same as flatMap above)Array.prototype.chain = function chain (f) { return this.reduce ((acc, x) => [ ...acc, ...f (x) ], []) }// Identity functionconst identity = x => x// math is programming is math is ...const combinations = (...arrs) => [ identity ] .ap (arrs)console.log (combinations ([ 0, 1 ], [ 'A', 'B' ], [ '♡', '♢' ]))// [ [ 0, 'A', '♡' ]// , [ 0, 'A', '♢' ]// , [ 0, 'B', '♡' ]// , [ 0, 'B', '♢' ]// , [ 1, 'A', '♡' ]// , [ 1, 'A', '♢' ]// , [ 1, 'B', '♡' ]// , [ 1, 'B', '♢' ]// ] 这篇关于javascript递归骰子组合并将结果存储在矩阵中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 18:21