Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following F# code takes 2 sequences of values (rank and suit in the example) and returns the cartesian product as a sequence of pairs (cards), using a pair type dynamically generated at runtime using Reflection:</p> <pre><code>open System open System.Reflection open System.Reflection.Emit open Microsoft.FSharp.Reflection /// Creates a dynamic module via reflection let createModule () = let name = Guid.NewGuid().ToString() let d = AppDomain.CurrentDomain let a = d.DefineDynamicAssembly(AssemblyName(name), AssemblyBuilderAccess.Run) a.DefineDynamicModule(name) /// Creates a dynamic pair type using the specified x and y types let createPairType (x:Type, y:Type) = let m = createModule() let t = m.DefineType("Pair", TypeAttributes.Public ||| TypeAttributes.Class) let x = t.DefineField(x.Name, x, FieldAttributes.Public) let y = t.DefineField(y.Name, y, FieldAttributes.Public) t.CreateType() /// Creates a pair value using the specified pair type let createPairValue (pairType:Type) (x:'X, y:'Y) = let instance = Activator.CreateInstance(pairType) pairType.GetField(typeof&lt;'X&gt;.Name).SetValue(instance, x) pairType.GetField(typeof&lt;'Y&gt;.Name).SetValue(instance, y) instance /// Creates a cartesian product let createCartesianProduct (xs:'X seq, ys:'Y seq) = let pairType = createPairType (typeof&lt;'X&gt;,typeof&lt;'Y&gt;) seq { for x in xs do for y in ys -&gt; createPairValue pairType (x, y) } /// Defines dynamic lookup operator for accessing a named field let inline (?) (x:obj) name = x.GetType().GetField(name).GetValue(x) /// Card suit discriminated union type type Suit = Club | Diamond | Heart | Spade /// Card rank discriminated union type type Rank = | One | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace /// Gets union case values let getUnionValues&lt;'T&gt;() = FSharpType.GetUnionCases(typeof&lt;'T&gt;) |&gt; Seq.map (fun x -&gt; FSharpValue.MakeUnion(x,[||]) :?&gt; 'T) let ranks, suits = getUnionValues&lt;Rank&gt;(), getUnionValues&lt;Suit&gt;() /// Sequence of dynamically generated pairs let cards = createCartesianProduct (ranks, suits) // Paste this into F# interactive to print the generated cards for card in cards do printfn "%A %A" card?Rank card?Suit </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload