Find common directory path: Difference between revisions

→‎OCaml: simplify
m (→‎{{header|Wren}}: Changed to Wren S/H)
(→‎OCaml: simplify)
Line 1,727:
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec auxcommon_prefix accxs pathsys = match xs, ys with
| x :: xs, y :: ys when x = y -> x :: common_prefix xs ys
| dirs_ -> []
 
let common_prefix_all = function
<syntaxhighlight lang="ocaml">let rec aux acc paths =
| []x :: xs -> invalid_argList.fold_left "common_prefix" x xs
if List.mem [] paths
| _ -> []
then (List.rev acc) else
let heads = List.map List.hd paths in
let item = List.hd heads in
let all_the_same =
List.for_all ((=) item) (List.tl heads)
in
if all_the_same
then aux (item::acc) (List.map List.tl paths)
else (List.rev acc)
 
let common_prefixcommon_ancestor ~sep =paths function=
let paths = List.map (Str.split(split_delim (Str.regexp_string sep)) dirs inpaths
| [] -> invalid_arg "common_prefix"
|> common_prefix_all
| dirs ->
|> (sep ^ (String.concat sep res))
let paths = List.map (Str.split (Str.regexp_string sep)) dirs in
let res = aux [] paths in
(sep ^ (String.concat sep res))
 
let ()_ = assert begin
common_ancestor ~sep:"/" [
let dirs = [
"/home/user1/tmp/coverage/test";
"/home/user1/tmp/covert/operator";
"/home/user1/tmp/coven/members";
] = "/home/user1/tmp"
] in
;;end</syntaxhighlight>
print_endline (common_prefix "/" dirs);
;;</syntaxhighlight>
 
(usesRequires the modulestandard <code>[httphttps://camlocaml.inria.fr/pub/docsorg/manual-ocaml/libref/Strlibstr.html Strstr]</code>, str.cma)library
 
=={{header|OpenEdge/Progress}}==
13

edits