Longest common prefix: Difference between revisions
m
→JS ES6: Updated primitives and output.
m (format) |
m (→JS ES6: Updated primitives and output.) |
||
Line 1,856:
===ES6===
<lang javascript>(() => {
// -------------- LONGEST COMMON PREFIX --------------
// lcp :: (Eq a) => [[a]] -> [a]
const lcp = xs => {
const go =
[]
) :
go(ws.map(
go(map(tail, xs))▼
);
return
allSame,▼
)
.map(head)
};
//
// main :: IO ()▼
].map(showPrefix).join("\n");
// showPrefix :: [String] -> String
const showPrefix = xs =>
▲ // main :: IO ()
▲ const main = () => {
showPrefix, [▼
▲ ["interspecies", "interstellar", "interstate"],
▲ ["throne", "throne"],
▲ ["throne", "dungeon"],
▲ ["cheese"],
▲ ["prefix", "suffix"],
▲ ["foo", "foobar"]
▲ ]
));▼
return (▼
strResults▼
//
// allSame :: [a] -> Bool
Line 1,908 ⟶ 1,903:
0 === xs.length || (() => {
const x = xs[0];
return xs.every(y => x === y);
})();
//
// isNull :: [a] -> Bool
// isNull :: String -> Bool
const isNull = xs =>
(Array.isArray(xs) ? (▼
// show :: a -> String
const show = JSON.stringify;
// tail :: [a] -> [a]
const tail = xs =>
// takeWhile :: (a -> Bool) -> [a] -> [a]
const
// MAIN ---
Line 1,977 ⟶ 1,948:
})();</lang>
{{Out}}
<pre>["interspecies","interstellar","interstate"]
["throne","throne"]
["throne","dungeon"]
["cheese"]
[""]
["prefix","suffix"]
["foo","foobar"]
=={{header|jq}}==
|