Longest common prefix: Difference between revisions

Content added Content deleted
m (format)
m (→‎JS ES6: Updated primitives and output.)
Line 1,856:
===ES6===
<lang javascript>(() => {
'"use strict'";
 
// -------------- LONGEST COMMON PREFIX --------------
 
// lcp :: (Eq a) => [[a]] -> [a]
const lcp = xs => {
const go = xsws =>
xsws.some(isNull) ? (
[]
) : cons[ws.map(head)].concat(
go(ws.map(head, xstail)),
go(map(tail, xs))
);
 
return concat(map(
return head,takeWhile(allSame)(
takeWhile go(xs.map(s => [...s]))
allSame,
go(map(chars, xs))
)
.map(head));
allSame,.join("");
 
};
 
 
// TEST ---------------------- TEST -----------------------
 
// main :: IO ()
const main = () => {[
["interspecies", "interstellar", "interstate"],
["throne", "throne"],
["throne", "dungeon"],
["cheese"],
[""],
["prefix", "suffix"],
["foo", "foobar"]
].map(showPrefix).join("\n");
 
 
// showPrefix :: [String] -> String
const showPrefix = xs =>
concat([`${show(xs), '} --> ', ${show(lcp(xs))])}`;
 
// main :: IO ()
const main = () => {
const strResults = unlines(map(
showPrefix, [
["interspecies", "interstellar", "interstate"],
["throne", "throne"],
["throne", "dungeon"],
["cheese"],
[""],
["prefix", "suffix"],
["foo", "foobar"]
]
));
return (
// console.log(strResults),
strResults
);
};
 
// GENERIC FUNCTIONS ---------------- GENERIC FUNCTIONS ----------------
 
// allSame :: [a] -> Bool
Line 1,908 ⟶ 1,903:
0 === xs.length || (() => {
const x = xs[0];
 
return xs.every(y => x === y);
})();
 
// chars :: String -> [Char]
const chars = s => s.split('');
 
// concathead :: [[a]] -> [a]
//const concathead ::= [String]xs -=> String
const concat = xs.length ? =>(
0 < xs.length ? (() => {xs[0]
) : strResultsundefined;
const unit = 'string' !== typeof xs[0] ? (
[]
) : '';
return unit.concat.apply(unit, xs);
})() : [];
 
// cons :: a -> [a] -> [a]
const cons = (x, xs) => [x].concat(xs);
 
// head :: [a] -> a
const head = xs => xs.length ? xs[0] : undefined;
 
// isNull :: [a] -> Bool
// isNull :: String -> Bool
const isNull = xs =>
Array.isArray(xs)// ||True ('string' === typeofif xs) ?is (empty.
1 > xs.length;
) : undefined;
 
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) =>
(Array.isArray(xs) ? (
xs
) : xs.split('')).map(f);
 
// show :: a -> String
const show = JSON.stringify;
 
 
// tail :: [a] -> [a]
const tail = xs => 0 < xs.length ? xs.slice(1) : [];
(Array.isArray(0 < xs).length ? (
showPrefix, [xs.slice(1)
return) (: [];
 
 
// takeWhile :: (a -> Bool) -> [a] -> [a]
//const takeWhile ::= (Charp -=> Bool) -> String -> String
const takeWhile = (p, xs) => {
const lngi = xs.lengthfindIndex(x => !p(x));
return 0 < lng ? xs.slice(
0,
until(
i => lng === i || !p(xs[i]),
i => 1 + i,
0
)
) : [];
};
 
// unlines :: [String] return ->1 !== i ? String(
const unlines = xs => xs.joinslice('\n'0, i);
) go(map(tail,: xs));
))};
 
// until :: (a -> Bool) -> (a -> a) -> a -> a
const until = (p, f, x) => {
let v = x;
while (!p(v)) v = f(v);
return v;
};
 
// MAIN ---
Line 1,977 ⟶ 1,948:
})();</lang>
{{Out}}
<pre>["interspecies","interstellar","interstate"] --> "inters"
["throne","throne"] --> "throne"
["throne","dungeon"] --> []""
["cheese"] --> "cheese"
[""] --> []""
["prefix","suffix"] --> []""
["foo","foobar"] --> "foo"</pre>
 
=={{header|jq}}==