Computation Builder for Cartesian Products

Sample framework for computing Cartesian products using a computation builder.

Copy Source
Copy Link
Tools:
 1: // Computation expression for 
 2: // Cartesian products.
 3: // I use lists here, but any type with
 4: // aggregate operations will work 
 5: // (e.g. sequences).
 6: // Notice how tiny it would be without
 7: // all these comments and formatting -- 
 8: // just three lines of code!
 9: type Product () =
10: 
11:   member this.Bind (l,f) =
12:     // Collect lets you capture the result
13:     // in a list, but other operations 
14:     // are possible.  For example, map 
15:     // builds a hierarchy, and iter will 
16:     // let you use the results without 
17:     // aggregating them (return must be 
18:     // modified as shown below).
19:     List.collect f l
20: 
21:   member this.Return n = 
22:     // For collect and map:
23:     [n]
24:     // For iter:
25:     //()
26: 
27: 
28: let enumeratedPizzas = 
29:   Product() {
30:     // I never met a pizza I didn't like.
31:     let! x = ["New York";"Chicago"]
32:     let! y = ["Pepperoni";"Sausage"]
33:     let! z = ["Cheese";"Double Cheese"]
34:     // I capture the results in a tuple,
35:     // but you can do whatever here,
36:     // depending on aggregation, etc.
37:     return x,y,z
38:   }
39: 
type Product =
  class
    new : unit -> Product
    member Bind : l:'b list * f:('b -> 'c list) -> 'c list
    member Return : n:'a -> 'a list
  end

Full name: Snippet.Product
val this : Product
member Product.Bind : l:'b list * f:('b -> 'c list) -> 'c list

Full name: Snippet.Product.Bind
val l : 'b list

  type: 'b list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'b>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'b>
  implements: System.Collections.IEnumerable
val f : ('b -> 'c list)
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------

type List<'T> =
  | ( [] )
  | ( :: ) of 'T * 'T list
  with
    interface System.Collections.IEnumerable
    interface System.Collections.Generic.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
  end

Full name: Microsoft.FSharp.Collections.List<_>

  type: List<'T>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<'T>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<'T>
  implements: System.Collections.IEnumerable
val collect : ('T -> 'U list) -> 'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.collect
member Product.Return : n:'a -> 'a list

Full name: Snippet.Product.Return
val n : 'a
val enumeratedPizzas : (string * string * string) list

Full name: Snippet.enumeratedPizzas

  type: (string * string * string) list
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<List<string * string * string>>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
  implements: System.Collections.Generic.IEnumerable<string * string * string>
  implements: System.Collections.IEnumerable
val x : string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
val y : string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>
val z : string

  type: string
  implements: System.IComparable
  implements: System.ICloneable
  implements: System.IConvertible
  implements: System.IComparable<string>
  implements: seq<char>
  implements: System.Collections.IEnumerable
  implements: System.IEquatable<string>

More information

Link: http://fssnip.net/4L
Posted: 3 years ago
Author: TechNeilogy (website)
Tags: computation builder, cartesian