Skip to content

Instantly share code, notes, and snippets.

@eyston
Created September 30, 2011 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyston/1252411 to your computer and use it in GitHub Desktop.
Save eyston/1252411 to your computer and use it in GitHub Desktop.
// possibly tail recursive :p
open System.IO
// Seq.toList because I don't know how to pattern match an empty sequence :)
let folderWithDepth' path depth =
try
System.IO.Directory.EnumerateDirectories(path) |> Seq.toList |> List.map (fun d -> (d, depth))
with
| _ -> []
// we keep two variables
// - dirsToScan :: directories we need to check their subdirectories
// - directories :: everything we have scanned ... our accumulator
//
// so if dirsToScan is empty, we are done, and return our accumulator (directories)
// if dirsToScan isn't empty, we pop off the head, scan it for sub-dirs, and append them to both
// dirsToScan and directories
let rec scanSubFolders'' dirsToScan directories =
match dirsToScan with
| [] -> directories
| (p, d)::rest ->
let dirs = folderWithDepth' p (d+1)
scanSubFolders'' (rest |> List.append dirs) (directories |> List.append dirs)
let scanSubFolders' path depth =
let dirs = folderWithDepth' path depth
scanSubFolders'' dirs dirs
// (path, 0) adds our initial path .. no sub dirs returns the path argument, 0
let deepestFolder' path =
(path, 0) :: scanSubFolders' path 1 |> Seq.maxBy(fun (p, d) -> d)
let folderWithDepth' path depth =
try
System.IO.Directory.EnumerateDirectories(path) |> Seq.toList |> List.map (fun d -> (d, depth))
with
| _ -> []
let rec scanSubFolders''' dirsToScan maxDirectory =
match dirsToScan with
| [] -> maxDirectory
| (p, d)::rest ->
let dirs = folderWithDepth' p (d+1)
scanSubFolders''' (rest |> List.append dirs) (maxDirectory :: dirs |> List.maxBy(fun (p,d) -> d))
let deepestFolder' path =
scanSubFolders''' ((path, 0) :: []) (path, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment