Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
158
Task/Teacup-rim-text/JavaScript/teacup-rim-text-1.js
Normal file
158
Task/Teacup-rim-text/JavaScript/teacup-rim-text-1.js
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// main :: IO ()
|
||||
const main = () =>
|
||||
showGroups(
|
||||
circularWords(
|
||||
// Local copy of:
|
||||
// https://www.mit.edu/~ecprice/wordlist.10000
|
||||
lines(readFile('~/mitWords.txt'))
|
||||
)
|
||||
);
|
||||
|
||||
// circularWords :: [String] -> [String]
|
||||
const circularWords = ws =>
|
||||
ws.filter(isCircular(new Set(ws)), ws);
|
||||
|
||||
// isCircular :: Set String -> String -> Bool
|
||||
const isCircular = lexicon => w => {
|
||||
const iLast = w.length - 1;
|
||||
return 1 < iLast && until(
|
||||
([i, bln, s]) => iLast < i || !bln,
|
||||
([i, bln, s]) => [1 + i, lexicon.has(s), rotated(s)],
|
||||
[0, true, rotated(w)]
|
||||
)[1];
|
||||
};
|
||||
|
||||
// DISPLAY --------------------------------------------
|
||||
|
||||
// showGroups :: [String] -> String
|
||||
const showGroups = xs =>
|
||||
unlines(map(
|
||||
gp => map(snd, gp).join(' -> '),
|
||||
groupBy(
|
||||
(a, b) => fst(a) === fst(b),
|
||||
sortBy(
|
||||
comparing(fst),
|
||||
map(x => Tuple(concat(sort(chars(x))), x),
|
||||
xs
|
||||
)
|
||||
)
|
||||
).filter(gp => 1 < gp.length)
|
||||
));
|
||||
|
||||
|
||||
// MAC OS JS FOR AUTOMATION ---------------------------
|
||||
|
||||
// readFile :: FilePath -> IO String
|
||||
const readFile = fp => {
|
||||
const
|
||||
e = $(),
|
||||
uw = ObjC.unwrap,
|
||||
s = uw(
|
||||
$.NSString.stringWithContentsOfFileEncodingError(
|
||||
$(fp)
|
||||
.stringByStandardizingPath,
|
||||
$.NSUTF8StringEncoding,
|
||||
e
|
||||
)
|
||||
);
|
||||
return undefined !== s ? (
|
||||
s
|
||||
) : uw(e.localizedDescription);
|
||||
};
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// Tuple (,) :: a -> b -> (a, b)
|
||||
const Tuple = (a, b) => ({
|
||||
type: 'Tuple',
|
||||
'0': a,
|
||||
'1': b,
|
||||
length: 2
|
||||
});
|
||||
|
||||
// chars :: String -> [Char]
|
||||
const chars = s => s.split('');
|
||||
|
||||
// comparing :: (a -> b) -> (a -> a -> Ordering)
|
||||
const comparing = f =>
|
||||
(x, y) => {
|
||||
const
|
||||
a = f(x),
|
||||
b = f(y);
|
||||
return a < b ? -1 : (a > b ? 1 : 0);
|
||||
};
|
||||
|
||||
// concat :: [[a]] -> [a]
|
||||
// concat :: [String] -> String
|
||||
const concat = xs =>
|
||||
0 < xs.length ? (() => {
|
||||
const unit = 'string' !== typeof xs[0] ? (
|
||||
[]
|
||||
) : '';
|
||||
return unit.concat.apply(unit, xs);
|
||||
})() : [];
|
||||
|
||||
// fst :: (a, b) -> a
|
||||
const fst = tpl => tpl[0];
|
||||
|
||||
// groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
|
||||
const groupBy = (f, xs) => {
|
||||
const tpl = xs.slice(1)
|
||||
.reduce((a, x) => {
|
||||
const h = a[1].length > 0 ? a[1][0] : undefined;
|
||||
return (undefined !== h) && f(h, x) ? (
|
||||
Tuple(a[0], a[1].concat([x]))
|
||||
) : Tuple(a[0].concat([a[1]]), [x]);
|
||||
}, Tuple([], 0 < xs.length ? [xs[0]] : []));
|
||||
return tpl[0].concat([tpl[1]]);
|
||||
};
|
||||
|
||||
// lines :: String -> [String]
|
||||
const lines = s => s.split(/[\r\n]/);
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = (f, xs) =>
|
||||
(Array.isArray(xs) ? (
|
||||
xs
|
||||
) : xs.split('')).map(f);
|
||||
|
||||
// rotated :: String -> String
|
||||
const rotated = xs =>
|
||||
xs.slice(1) + xs[0];
|
||||
|
||||
// showLog :: a -> IO ()
|
||||
const showLog = (...args) =>
|
||||
console.log(
|
||||
args
|
||||
.map(JSON.stringify)
|
||||
.join(' -> ')
|
||||
);
|
||||
|
||||
// snd :: (a, b) -> b
|
||||
const snd = tpl => tpl[1];
|
||||
|
||||
// sort :: Ord a => [a] -> [a]
|
||||
const sort = xs => xs.slice()
|
||||
.sort((a, b) => a < b ? -1 : (a > b ? 1 : 0));
|
||||
|
||||
// sortBy :: (a -> a -> Ordering) -> [a] -> [a]
|
||||
const sortBy = (f, xs) =>
|
||||
xs.slice()
|
||||
.sort(f);
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
// until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
const until = (p, f, x) => {
|
||||
let v = x;
|
||||
while (!p(v)) v = f(v);
|
||||
return v;
|
||||
};
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
140
Task/Teacup-rim-text/JavaScript/teacup-rim-text-2.js
Normal file
140
Task/Teacup-rim-text/JavaScript/teacup-rim-text-2.js
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// main :: IO ()
|
||||
const main = () =>
|
||||
anagrams(lines(readFile('~/mitWords.txt')))
|
||||
.flatMap(circularOnly)
|
||||
.map(xs => xs.join(' -> '))
|
||||
.join('\n')
|
||||
|
||||
// anagrams :: [String] -> [[String]]
|
||||
const anagrams = ws =>
|
||||
groupBy(
|
||||
on(eq, fst),
|
||||
sortBy(
|
||||
comparing(fst),
|
||||
ws.map(w => Tuple(sort(chars(w)).join(''), w))
|
||||
)
|
||||
).flatMap(
|
||||
gp => 2 < gp.length ? [
|
||||
gp.map(snd)
|
||||
] : []
|
||||
)
|
||||
|
||||
// circularOnly :: [String] -> [[String]]
|
||||
const circularOnly = ws => {
|
||||
const h = ws[0];
|
||||
return ws.length < h.length ? (
|
||||
[]
|
||||
) : (() => {
|
||||
const rs = rotations(h);
|
||||
return rs.every(r => ws.includes(r)) ? (
|
||||
[rs]
|
||||
) : [];
|
||||
})();
|
||||
};
|
||||
|
||||
// rotations :: String -> [String]
|
||||
const rotations = s =>
|
||||
takeIterate(s.length, rotated, s)
|
||||
|
||||
// rotated :: [a] -> [a]
|
||||
const rotated = xs => xs.slice(1).concat(xs[0]);
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------
|
||||
|
||||
// Tuple (,) :: a -> b -> (a, b)
|
||||
const Tuple = (a, b) => ({
|
||||
type: 'Tuple',
|
||||
'0': a,
|
||||
'1': b,
|
||||
length: 2
|
||||
});
|
||||
|
||||
// chars :: String -> [Char]
|
||||
const chars = s => s.split('');
|
||||
|
||||
// comparing :: (a -> b) -> (a -> a -> Ordering)
|
||||
const comparing = f =>
|
||||
(x, y) => {
|
||||
const
|
||||
a = f(x),
|
||||
b = f(y);
|
||||
return a < b ? -1 : (a > b ? 1 : 0);
|
||||
};
|
||||
|
||||
// eq (==) :: Eq a => a -> a -> Bool
|
||||
const eq = (a, b) => a === b
|
||||
|
||||
// fst :: (a, b) -> a
|
||||
const fst = tpl => tpl[0];
|
||||
|
||||
// groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
|
||||
const groupBy = (f, xs) => {
|
||||
const tpl = xs.slice(1)
|
||||
.reduce((a, x) => {
|
||||
const h = a[1].length > 0 ? a[1][0] : undefined;
|
||||
return (undefined !== h) && f(h, x) ? (
|
||||
Tuple(a[0], a[1].concat([x]))
|
||||
) : Tuple(a[0].concat([a[1]]), [x]);
|
||||
}, Tuple([], 0 < xs.length ? [xs[0]] : []));
|
||||
return tpl[0].concat([tpl[1]]);
|
||||
};
|
||||
|
||||
// lines :: String -> [String]
|
||||
const lines = s => s.split(/[\r\n]/);
|
||||
|
||||
// mapAccumL :: (acc -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
|
||||
const mapAccumL = (f, acc, xs) =>
|
||||
xs.reduce((a, x, i) => {
|
||||
const pair = f(a[0], x, i);
|
||||
return Tuple(pair[0], a[1].concat(pair[1]));
|
||||
}, Tuple(acc, []));
|
||||
|
||||
// on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
|
||||
const on = (f, g) => (a, b) => f(g(a), g(b));
|
||||
|
||||
// readFile :: FilePath -> IO String
|
||||
const readFile = fp => {
|
||||
const
|
||||
e = $(),
|
||||
uw = ObjC.unwrap,
|
||||
s = uw(
|
||||
$.NSString.stringWithContentsOfFileEncodingError(
|
||||
$(fp)
|
||||
.stringByStandardizingPath,
|
||||
$.NSUTF8StringEncoding,
|
||||
e
|
||||
)
|
||||
);
|
||||
return undefined !== s ? (
|
||||
s
|
||||
) : uw(e.localizedDescription);
|
||||
};
|
||||
|
||||
// snd :: (a, b) -> b
|
||||
const snd = tpl => tpl[1];
|
||||
|
||||
// sort :: Ord a => [a] -> [a]
|
||||
const sort = xs => xs.slice()
|
||||
.sort((a, b) => a < b ? -1 : (a > b ? 1 : 0));
|
||||
|
||||
// sortBy :: (a -> a -> Ordering) -> [a] -> [a]
|
||||
const sortBy = (f, xs) =>
|
||||
xs.slice()
|
||||
.sort(f);
|
||||
|
||||
// takeIterate :: Int -> (a -> a) -> a -> [a]
|
||||
const takeIterate = (n, f, x) =>
|
||||
snd(mapAccumL((a, _, i) => {
|
||||
const v = 0 !== i ? f(a) : x;
|
||||
return [v, v];
|
||||
}, x, Array.from({
|
||||
length: n
|
||||
})));
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue