Permalink
Browse files

Little cleanup of the tree zipper

  • Loading branch information...
forki committed Sep 17, 2012
1 parent 9e9bc72 commit bb8f50ca1a875e419e657a9c7d7a2c4df8dd3397
Showing with 11 additions and 12 deletions.
  1. +11 −12 src/FSharpx.Core/DataStructures/TreeZipper.fs
@@ -3,13 +3,12 @@
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module FSharpx.DataStructures.TreeZipper
-/// A simple binarys tree
+/// A simple binary tree
type 'a Tree=
| Leaf
| Branch of 'a * 'a Tree * 'a Tree
type TreeDirection = Left | Right
-type ZipperDirection = Up | Left | Right
/// The tree zipper datastructure
type 'a TreeZipper = {
@@ -21,10 +20,8 @@ open FSharpx
/// Moves the zipper one level up
let up z =
match z.Path with
- | (d,v,other)::ep ->
- match d with
- | TreeDirection.Left -> { Focus = Branch(v,z.Focus,other); Path = ep }
- | TreeDirection.Right -> { Focus = Branch(v,other,z.Focus); Path = ep }
+ | (Left,v,other)::ep -> { Focus = Branch(v,z.Focus,other); Path = ep }
+ | (Right,v,other)::ep -> { Focus = Branch(v,other,z.Focus); Path = ep }
| [] -> failwith "can't go up" // because ep only goes down and is empty
/// Moves the zipper to the top
@@ -36,15 +33,20 @@ let rec top z =
/// Moves the zipper to the left
let left z =
match z.Focus with
- | (Branch(v, explored, other)) -> { Focus = explored; Path = (TreeDirection.Left, v, other)::z.Path }
+ | (Branch(v, explored, other)) -> { Focus = explored; Path = (Left, v, other) :: z.Path }
| Leaf -> failwith "can't go down on leaf"
/// Moves the zipper to the right
let right z =
match z.Focus with
- | (Branch(v, other, explored)) -> { Focus = explored; Path= (TreeDirection.Right, v, other)::z.Path }
+ | (Branch(v, other, explored)) -> { Focus = explored; Path = (Right, v, other) :: z.Path }
| Leaf -> failwith "can't go down on leaf"
+/// Creates a zipper from a tree
+let zipper t = { Focus = t; Path = [] }
+
+type TreeZipperDirection = Up | Left | Right
+
let inline getMove direction =
match direction with
| Up -> up
@@ -55,7 +57,4 @@ let inline getMove direction =
let rec move directions (z:'a TreeZipper) =
directions
|> Seq.map getMove
- |> Seq.fold (|>) z
-
-/// Creates a zipper from a tree
-let zipper t = { Focus = t; Path = [] }
+ |> Seq.fold (|>) z

0 comments on commit bb8f50c

Please sign in to comment.