Teacup rim text: Difference between revisions

Content added Content deleted
(→‎{{header|zkl}}: added code)
(→‎{{header|JavaScript}}: Added a JS draft)
Line 52: Line 52:
rotated [] _ = []
rotated [] _ = []
rotated xs n = zipWith const (drop n (cycle xs)) xs</lang>
rotated xs n = zipWith const (drop n (cycle xs)) xs</lang>
{{Out}}
<pre>["aaa","apt","arc","ate","car","eat","iii","pta","rca","tap","tea"]</pre>

=={{header|JavaScript}}==
<lang javascript>(() => {
'use strict';

// main :: IO ()
const main = () =>
showLog(
circularWords(
lines(readFile('~/unixdict.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, w]
)[1];
}

// rotated :: String -> String
const rotated = xs =>
xs.slice(1) + xs[0];


// GENERIC FUNCTIONS ----------------------------------

// lines :: String -> [String]
const lines = s => s.split(/[\r\n]/);

// 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);
};

// showLog :: a -> IO ()
const showLog = (...args) =>
console.log(
args
.map(JSON.stringify)
.join(' -> ')
);

// 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();
})();</lang>
{{Out}}
{{Out}}
<pre>["aaa","apt","arc","ate","car","eat","iii","pta","rca","tap","tea"]</pre>
<pre>["aaa","apt","arc","ate","car","eat","iii","pta","rca","tap","tea"]</pre>
Line 101: Line 176:
ima: ["aim", "mai"]
ima: ["aim", "mai"]
</pre>
</pre>



=={{header|Lychen}}==
=={{header|Lychen}}==