Permutations with repetitions: Difference between revisions

→‎JS Lazy evaluation with a generator: Replaced code with correct version
(→‎JS Lazy evaluation with a generator: Replaced code with correct version)
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 1,017 ⟶ 991:
 
// Search without needing to generate whole set:
let nxt = gen.next();
let i nxt = 1gen.next(),
i = 1,
while(!nxt.done && toLower(concat(nxt.value)) !== 'crack') {
console.log(alpha = nxt.value),
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 (
'generatedGenerated ' + i + ' of ' + Math.pow(4, 5) +
' possible permutations before finding:,\n' +
'searching from: ' + show(alpha) + ' thru: ' + show(psi) +
JSON.stringify(nxt.value)
'\nbefore finding: ' + show(nxt.value)
)
);
};
 
// LIBRARYPERMUTATION IMPORTGENERATOR --------------------------------------
 
// Evaluate a function fpermsWithRepn :: (()[a] -> Int -> Generator [a)]
function* permsWithRepn(xs, intGroup) {
// in the context of the JS libraries whose source
const
// filePaths are listed in fps :: [FilePath]
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 ----------------------------------
// 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');
 
// doesFileExistJust :: FilePatha -> IOMaybe Boola
const doesFileExistJust = strPathx => ({
consttype: ref = Ref();'Maybe',
Nothing: false,
return $.NSFileManager.defaultManager
Just: x
.fileExistsAtPathIsDirectory(
});
$(strPath)
.stringByStandardizingPath, ref
) && ref[0] !== 1;
};
 
// readFileNothing :: FilePathMaybe -> IO Stringa
const readFileNothing = strPath() => ({
lettype: error = $()'Maybe',
Nothing: str = ObjC.unwrap(true,
});
$.NSString.stringWithContentsOfFileEncodingError(
 
$(strPath)
// Tuple (,) :: a -> b -> (a, b)
.stringByStandardizingPath,
const Tuple = (a, b) => ({
$.NSUTF8StringEncoding,
type: error'Tuple',
'0': )a,
'1': );b,
returnlength: Boolean(error.code) ? (2
});
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 usingLibsmain();
[
'~/prelude-jxa/jsPrelude.js',
'~/prelude-jxa/jxaSystemIO.js'
// '~/Library/Script Libraries/BBDrafts.js'
],
main
);
})();</lang>
{{Out}}
9,655

edits