Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, tuples will be fixed by dimension. Also note that .NET tuples are boxed. If you are operating on large collections of points with small dimension (such as arrays of 2d points), using structs may help.</p> <p>If you really want to push the F#/.NET advantage over Java, have a look at generics. Writing code with generics allows to write code that works for any dimension, and use different representations for different dimensions (say structs for 1-3 dimensions, and vectors for larger dimensions):</p> <pre><code>let op&lt;'T where 'T :&gt; IVector&gt; (x: 'T) = ... </code></pre> <p>This is only relevant though if you are willing to go a long way to get the absolutely best performance and generality. Most projects do not need that, stick with the simplest thing that works.</p> <p>For the fun of it, here is an extended example of how to utilize generics and F# inlining:</p> <pre><code>open System.Numerics type IVector&lt;'T,'V&gt; = abstract member Item : int -&gt; 'T with get abstract member Length : int abstract member Update : int * 'T -&gt; 'V let lift&lt;'T,'V when 'V :&gt; IVector&lt;'T,'V&gt;&gt; f (v: 'V) : 'V = if v.Length = 0 then v else let mutable r = v.Update(0, f v.[0]) for i in 1 .. v.Length - 1 do r &lt;- r.Update(i, f v.[i]) r let inline norm (v: IVector&lt;_,_&gt;) = let sq i = let x = v.[i] x * x Seq.sum (Seq.init v.Length sq) let inline normalize (v: 'V) : 'V = let n = norm v lift (fun x -&gt; x / n) v [&lt;Struct&gt;] type Vector2D&lt;'T&gt;(x: 'T, y: 'T) = member this.X = x member this.Y = y interface IVector&lt;'T,Vector2D&lt;'T&gt;&gt; with member this.Item with get (i: int) = match i with | 0 -&gt; x | _ -&gt; y member this.Length = 2 member this.Update(i: int, v: 'T) = match i with | 0 -&gt; Vector2D(v, y) | _ -&gt; Vector2D(x, v) override this.ToString() = System.String.Format("{0}, {1}", x, y) [&lt;Sealed&gt;] type Vector&lt;'T&gt;(x: 'T []) = interface IVector&lt;'T,Vector&lt;'T&gt;&gt; with member this.Item with get (i: int) = x.[i] member this.Length = x.Length member this.Update(i: int, v: 'T) = let a = Array.copy x a.[i] &lt;- v Vector(a) override this.ToString() = x |&gt; Seq.map (fun e -&gt; e.ToString()) |&gt; String.concat ", " [&lt;Struct&gt;] type C(c: Complex) = member this.Complex = c static member Zero = C(Complex(0., 0.)) static member ( + ) (a: C, b: C) = C(a.Complex + b.Complex) static member ( * ) (a: C, b: C) = C(a.Complex * b.Complex) static member ( / ) (a: C, b: C) = C(a.Complex / b.Complex) override this.ToString() = string c let v1 = Vector2D(10., 30.) normalize v1 |&gt; printfn "%O" let v2 = Vector2D(C(Complex(1.25, 0.8)), C(Complex(0.5, -1.))) normalize v2 |&gt; printfn "%O" let v3 = Vector([| 10.; 30.; 50.|]) normalize v3 |&gt; printfn "%O" </code></pre> <p>Note that <code>norm</code> and <code>normalize</code> are fairly general, they cope with specialized 2D vectors and generalized N-dimensional vectors, and with different component types such as complex numbers (you can define your own). The use of generics and F# inlining ensure that while general, these algorithms perform well for the special cases, using compact representations. This is where F# and .NET generics shine compared to Java, where you are obliged to create specialized copies of your code to get decent performance.</p>
    singulars
    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