76 people like it.

Struct Tuple

Two/Three/Four-element generic tuples implemented as a value types for writing more efficient F# code.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
type Pair<'a, 'b> =
  struct 
    val Item1 : 'a
    val Item2 : 'b
    
    new(item1, item2) = { 
      Item1 = item1
      Item2 = item2
    }
  end

type Triple<'a, 'b, 'c> =
  struct 
    val Item1 : 'a
    val Item2 : 'b
    val Item3 : 'c
    
    new(item1, item2, item3) = { 
      Item1 = item1
      Item2 = item2
      Item3 = item3
    }
  end

type Quad<'a, 'b, 'c, 'd> =
  struct 
    val Item1 : 'a
    val Item2 : 'b
    val Item3 : 'c
    val Item4 : 'd
    
    new(item1, item2, item3, item4) = { 
      Item1 = item1
      Item2 = item2
      Item3 = item3
      Item4 = item4
    }
  end

let spair a b = Pair(a, b)
let striple a b c = Triple(a, b, c)
let squad a b c d = Quad(a, b, c, d)

//Usage
spair 1 2
striple 1 2 3
squad 1 2 3 4
Pair.Item1: 'a
Pair.Item2: 'b
val item1 : 'a
val item2 : 'b
Multiple items
type Triple<'a,'b,'c> =
  struct
    new : item1:'a * item2:'b * item3:'c -> Triple<'a,'b,'c>
    val Item1: 'a
    val Item2: 'b
    val Item3: 'c
  end

Full name: Script.Triple<_,_,_>

--------------------
Triple()
new : item1:'a * item2:'b * item3:'c -> Triple<'a,'b,'c>
Triple.Item1: 'a
Triple.Item2: 'b
Triple.Item3: 'c
val item3 : 'c
Multiple items
type Quad<'a,'b,'c,'d> =
  struct
    new : item1:'a * item2:'b * item3:'c * item4:'d -> Quad<'a,'b,'c,'d>
    val Item1: 'a
    val Item2: 'b
    val Item3: 'c
    val Item4: 'd
  end

Full name: Script.Quad<_,_,_,_>

--------------------
Quad()
new : item1:'a * item2:'b * item3:'c * item4:'d -> Quad<'a,'b,'c,'d>
Quad.Item1: 'a
Quad.Item2: 'b
Quad.Item3: 'c
Quad.Item4: 'd
val item4 : 'd
val spair : a:'a -> b:'b -> Pair<'a,'b>

Full name: Script.spair
val a : 'a
val b : 'b
Multiple items
type Pair<'a,'b> =
  struct
    new : item1:'a * item2:'b -> Pair<'a,'b>
    val Item1: 'a
    val Item2: 'b
  end

Full name: Script.Pair<_,_>

--------------------
Pair()
new : item1:'a * item2:'b -> Pair<'a,'b>
val striple : a:'a -> b:'b -> c:'c -> Triple<'a,'b,'c>

Full name: Script.striple
val c : 'c
val squad : a:'a -> b:'b -> c:'c -> d:'d -> Quad<'a,'b,'c,'d>

Full name: Script.squad
val d : 'd

More information

Link:http://fssnip.net/2P
Posted:13 years ago
Author:fholm
Tags: struct , tuple