Permutations with repetitions: Difference between revisions
→JS Lazy evaluation with a generator: Replaced code with correct version
m (→JS ES6) |
(→JS Lazy evaluation with a generator: Replaced code with correct version) |
||
Line 985:
'use strict';
const main = () => {
// Generator object
Line 1,017 ⟶ 991:
// Search without needing to generate whole set:
let
i = 1,
psi = alpha;
while (!nxt.done && 'crack' !== toLower(concat(nxt.value))) {
psi = nxt.value;
console.log(psi)
nxt = gen.next()
i++
Line 1,026 ⟶ 1,004:
console.log(nxt.value)
return (
'
' possible permutations
'searching from: ' + show(alpha) + ' thru: ' + show(psi) +
'\nbefore finding: ' + show(nxt.value)
);
};
//
//
function* permsWithRepn(xs, intGroup) {
const
vs = Array.from(xs),
intBase = vs.length,
intSet = Math.pow(intBase, intGroup);
if (0 < intBase) {
let index = 0;
while (index < intSet) {
const
ds = unfoldr(
v => 0 < v ? (() => {
const rd = quotRem(v, intBase);
return Just(Tuple(vs[rd[1]], rd[0]))
})() : Nothing(),
index++
);
yield replicate(
intGroup - ds.length,
vs[0]
).concat(ds);
};
}
};
// GENERIC FUNCTIONS ----------------------------------
//
const
Nothing: false,
Just: x
});
//
const
Nothing:
});
// Tuple (,) :: a -> b -> (a, b)
const Tuple = (a, b) => ({
type:
'0':
'1':
});
// concat :: [[a]] -> [a]
// concat :: [String] -> String
const concat = xs =>
0 < xs.length ? (() => {
const unit = 'string' !== typeof xs[0] ? (
[]
) : '';
return unit.concat.apply(unit, xs);
})() : [];
// index (!!) :: [a] -> Int -> a
// index (!!) :: String -> Int -> Char
const index = (xs, i) => xs[i];
// quotRem :: Int -> Int -> (Int, Int)
const quotRem = (m, n) =>
Tuple(Math.floor(m / n), m % n);
// replicate :: Int -> a -> [a]
const replicate = (n, x) =>
Array.from({
length: n
}, () => x);
// show :: a -> String
const show = x => JSON.stringify(x);
// toLower :: String -> String
const toLower = s => s.toLocaleLowerCase();
// unfoldr(x => 0 !== x ? Just([x, x - 1]) : Nothing(), 10);
// --> [10,9,8,7,6,5,4,3,2,1]
// unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
const unfoldr = (f, v) => {
let
xr = [v, v],
xs = [];
while (true) {
const mb = f(xr[1]);
if (mb.Nothing) {
return xs
} else {
xr = mb.Just;
xs.push(xr[0])
}
}
};
// MAIN
return
})();</lang>
{{Out}}
|