Teacup rim text: Difference between revisions

Content deleted Content added
Hout (talk | contribs)
Hout (talk | contribs)
→‎{{header|JavaScript}}: Switched from Unix to MIT dictionary, formatted output
Line 146: Line 146:
// main :: IO ()
// main :: IO ()
const main = () =>
const main = () =>
showLog(
showGroups(
circularWords(
circularWords(
lines(readFile('~/unixdict.txt'))
// Local copy of:
// https://www.mit.edu/~ecprice/wordlist.10000
lines(readFile('~/mitWords.txt'))
)
)
);
);
Line 165: Line 167:
)[1];
)[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
)
)
)
));




Line 188: Line 207:


// GENERIC FUNCTIONS ----------------------------------
// 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]
// lines :: String -> [String]
const lines = s => s.split(/[\r\n]/);
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
// rotated :: String -> String
Line 203: Line 273:
.join(' -> ')
.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
// until :: (a -> Bool) -> (a -> a) -> a -> a
Line 215: Line 300:
})();</lang>
})();</lang>
{{Out}}
{{Out}}
<pre>aaa
<pre>["aaa","apt","arc","ate","car","eat","iii","pta","rca","tap","tea"]</pre>
arc -> car -> rca
ate -> eat -> tea
aim -> ima -> mai
asp -> pas -> spa
iii
ips -> psi -> sip
ooo
www
xxx</pre>


=={{header|Julia}}==
=={{header|Julia}}==