{ lib ? import }: let inherit (builtins) length genList pathExists; inherit (lib) take splitString concatStringsSep last foldl foldr head tail singleton removePrefix hasSuffix removeSuffix flatten crossLists reverseList; butlast = list: take (length list - 1) list; dirname = path: let _p = (butlast (splitString "/" path)); in if _p != [] then concatStringsSep "/" _p else "./"; basename = path: (last (splitString "/" path)); doFileSuffix = "do"; genPatterns = path: let fn = basename path; in (map (x: "default." + x) (foldr (a: b: let x = (head b); in [ (a + "." + x) ] ++ b) [ doFileSuffix ] (tail (splitString "." fn)))); searchPath = path: let pathlist = (splitString "/" path); pat = (last pathlist); doFile = path + "." + doFileSuffix; patlist = genPatterns pat; foldedpaths = foldr (a: b: let h = if length b == 0 then "" else head b; in [ (h + a + "/") ] ++ b) [] (tail (reverseList pathlist)); pths = crossLists (p: pat: p + pat) [ foldedpaths patlist ]; in [ doFile ] ++ pths; cwd = "/home/spacefrogg/tmp"; path = cwd + "/foo/bar/baz.def.c"; whichdo = path: let pathlist = searchPath path; in foldl (res: p: if res == "" then if pathExists p then p else res else res) "" pathlist; d1 = path: pat: let dir = (dirname pat) + "/"; in removePrefix dir path; d2 = path: pat: let dir = (dirname pat) + "/"; _path = removePrefix dir path; _pat = removeSuffix ".${doFileSuffix}" (removePrefix "${dir}default" pat); _out = removeSuffix _pat _path; in if _out == "" then _path else _out; self = { inherit dirname basename searchPath cwd path doFileSuffix whichdo d1 d2; }; in self