2 people like it.

Composite Pattern with Object Expressions

An example of the composite pattern implemented simply with functions and object expressions

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
type Point = Point of int * int
type DrawingInstruction = Line of Point * Point

type Shape =
  abstract Draw: unit -> DrawingInstruction list
  abstract HitTest: Point -> bool

let line (p1:Point) (p2:Point) = 
    { new Shape with
      member this.Draw() = [Line(p1, p2)]
      member this.HitTest(point) = true
    }

let compositeShape (shapes:Shape list) = 
  { new Shape with 
    member this.Draw() = 
      [ for shape in shapes do 
          for instr in shape.Draw() do
            yield instr ]
    member this.HitTest(point) = 
      shapes |> List.exists(fun shape -> shape.HitTest(point)) }
Multiple items
union case Point.Point: int * int -> Point

--------------------
type Point = | Point of int * int

Full name: Script.Point
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
type DrawingInstruction = | Line of Point * Point

Full name: Script.DrawingInstruction
union case DrawingInstruction.Line: Point * Point -> DrawingInstruction
type Shape =
  interface
    abstract member Draw : unit -> DrawingInstruction list
    abstract member HitTest : Point -> bool
  end

Full name: Script.Shape
abstract member Shape.Draw : unit -> DrawingInstruction list

Full name: Script.Shape.Draw
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
abstract member Shape.HitTest : Point -> bool

Full name: Script.Shape.HitTest
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
val line : p1:Point -> p2:Point -> Shape

Full name: Script.line
val p1 : Point
val p2 : Point
val this : Shape
abstract member Shape.Draw : unit -> DrawingInstruction list
abstract member Shape.HitTest : Point -> bool
val point : Point
val compositeShape : shapes:Shape list -> Shape

Full name: Script.compositeShape
val shapes : Shape list
val shape : Shape
val instr : DrawingInstruction
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val exists : predicate:('T -> bool) -> list:'T list -> bool

Full name: Microsoft.FSharp.Collections.List.exists
Raw view Test code New version

More information

Link:http://fssnip.net/oi
Posted:9 years ago
Author:Steve Goguen
Tags: patterns