Permutations with repetitions: Difference between revisions
m
→JS ES6
(→JS ES6: added 'Strict' and 'Lazy' subheadings) |
m (→JS ES6) |
||
Line 985:
'use strict';
// main :: IO ()
const main = () => {
// permsWithRepn :: [a] -> Int -> Generator [a]
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 qr = quotRem(v, intBase);
return Just(Tuple(vs[qr[1]], qr[0]))
})() : Nothing(),
index++
);
yield replicate(
intGroup - ds.length,
vs[0]
).concat(ds);
};
}
};
// Generator object
Line 991 ⟶ 1,017:
// Search without needing to generate whole set:
let nxt = gen.next();
let
while(!nxt.done && toLower(concat(nxt.value)) !== 'crack') {
nxt = gen.next()
i++
Line 1,004 ⟶ 1,026:
console.log(nxt.value)
return (
'
' possible permutations
JSON.stringify(nxt.value)
)
};
//
//
// in the context of the JS libraries whose source
// filePaths are listed in fps :: [FilePath]
// Evaluate a function f :: (() -> a)
// in the context of the JS libraries whose source
// filePaths are listed in fps :: [FilePath]
// usingLibs :: [FilePath] -> (() -> a) -> a
const usingLibs = (fps, f) =>
fps.every(doesFileExist) ? (
eval(`(() => {
'use strict';
${fps.map(readFile).join('\n\n')}
return (${f})();
})();`)
) : 'Library not found at: ' + [].concat(...fps.map(
fp => doesFileExist(fp) ? [] : [fp]
)).join('\n');
//
const
return $.NSFileManager.defaultManager
.fileExistsAtPathIsDirectory(
$(strPath)
.stringByStandardizingPath, ref
) && ref[0] !== 1;
};
//
const
$.NSString.stringWithContentsOfFileEncodingError(
$(strPath)
.stringByStandardizingPath,
$.NSUTF8StringEncoding,
ObjC.unwrap(error.localizedDescription)
) : str;
};
// MAIN ------------------------------------------------
return
[
'~/prelude-jxa/jsPrelude.js',
'~/prelude-jxa/jxaSystemIO.js'
// '~/Library/Script Libraries/BBDrafts.js'
],
main
);
})();</lang>
<pre>Generated 590 of 1024 possible permutations,
searching from: ["A","A","A","A","A"] thru: ["A","R","A","C","K"]
|