Skip to content

Commit

Permalink
added Tree.scan and then used it to implement calculateMoves
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyson Williams committed Aug 4, 2020
1 parent dca5f31 commit 76d2aa8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 3 additions & 7 deletions FSharp/ArchivePictures/Archive.fs
Expand Up @@ -23,10 +23,6 @@ module Archive =
let calculateMoves =
let replaceDirectory (f : FileInfo) d =
FileInfo (Path.Combine (d, f.Name))
let rec imp path = function
| Leaf x ->
Leaf { Source = x; Destination = replaceDirectory x path }
| Node (x, xs) ->
let newNPath = Path.Combine (path, x)
Tree.node newNPath (List.map (imp newNPath) xs)
imp ""
let leaf x path = { Source = x; Destination = replaceDirectory x path }
let node x path = Path.Combine (path, x)
Tree.scan node leaf ""
6 changes: 6 additions & 0 deletions FSharp/ArchivePictures/Tree.fs
Expand Up @@ -7,6 +7,12 @@ module Tree =

let node x xs = Node (x, xs)

let rec scan f g z = function
| Leaf x -> g x z |> leaf
| Node (x, xs) ->
let z' = f x z
xs |> List.map (scan f g z') |> node z'

let rec cata fd ff = function
| Leaf x -> ff x
| Node (x, xs) -> xs |> List.map (cata fd ff) |> fd x
Expand Down

0 comments on commit 76d2aa8

Please sign in to comment.