Permutations with repetitions: Difference between revisions

m
(→‎JS ES6: added 'Strict' and 'Lazy' subheadings)
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 nxti = gen.next(),1
while(!nxt.done && toLower(concat(nxt.value)) !== 'crack') {
i = 1,
alpha = console.log(nxt.value,)
psi = alpha;
while (!nxt.done && toLower(concat(nxt.value)) !== 'crack') {
psi = nxt.value;
console.log(psi)
nxt = gen.next()
i++
Line 1,004 ⟶ 1,026:
console.log(nxt.value)
return (
'Generatedgenerated ' + i + ' of ' + Math.pow(4, 5) +
' possible permutations, before finding:\n' +
JSON.stringify(nxt.value)
'searching from: ' + show(alpha) + ' thru: ' + show(psi) +
)
'\nbefore finding: ' + show(nxt.value)
);
};
 
// PERMUTATIONLIBRARY GENERATORIMPORT --------------------------------------
 
// permsWithRepnEvaluate a function f :: [a](() -> Int -> Generator [a])
// in the context of the JS libraries whose source
function* permsWithRepn(xs, intGroup) {
// filePaths are listed in fps :: [FilePath]
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);
};
}
};
 
// Evaluate a function f :: (() -> a)
// GENERIC FUNCTIONS ----------------------------------
// 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');
 
// JustdoesFileExist :: aFilePath -> MaybeIO aBool
const JustdoesFileExist = xstrPath => ({
type:const 'Maybe',ref = Ref();
return $.NSFileManager.defaultManager
Nothing: false,
.fileExistsAtPathIsDirectory(
Just: x
$(strPath)
});
.stringByStandardizingPath, ref
) && ref[0] !== 1;
};
 
// NothingreadFile :: MaybeFilePath a-> IO String
const NothingreadFile = ()strPath => ({
type:let 'Maybe'error = $(),
Nothing: true, str = ObjC.unwrap(
$.NSString.stringWithContentsOfFileEncodingError(
});
$(strPath)
 
.stringByStandardizingPath,
// Tuple (,) :: a -> b -> (a, b)
$.NSUTF8StringEncoding,
const Tuple = (a, b) => ({
type: 'Tuple', error
'0': a, )
'1': b, );
length:return 2Boolean(error.code) ? (
ObjC.unwrap(error.localizedDescription)
});
) : str;
 
// 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 mainusingLibs();
[
'~/prelude-jxa/jsPrelude.js',
'~/prelude-jxa/jxaSystemIO.js'
// '~/Library/Script Libraries/BBDrafts.js'
],
main
);
})();</lang>
{{Out}}
<pre>Generated 590 of 1024 possible permutations,
searching from: ["A","A","A","A","A"] thru: ["A","R","A","C","K"]
9,655

edits