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: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
namespace FSKit

module StructTuple =

  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 inline spair a b = Pair(a, b)
  let inline striple a b c = Triple(a, b, c)
  let inline squad a b c d = Quad(a, b, c, d)

  let inline spair_fst (pair:Pair<_, _>) = pair.Item1
  let inline spair_snd (pair:Pair<_, _>) = pair.Item2

  let inline spair_ref_fst (pair:Pair<_, _> byref) = pair.Item1
  let inline spair_ref_snd (pair:Pair<_, _> byref) = pair.Item2

  let inline striple_fst (triple:Triple<_, _, _>) = triple.Item1
  let inline striple_snd (triple:Triple<_, _, _>) = triple.Item2
  let inline striple_trd (triple:Triple<_, _, _>) = triple.Item3

  let inline striple_ref_fst (triple:Triple<_, _, _> byref) = triple.Item1
  let inline striple_ref_snd (triple:Triple<_, _, _> byref) = triple.Item2
  let inline striple_ref_trd (triple:Triple<_, _, _> byref) = triple.Item3

  let inline squad_fst (quad:Quad<_, _, _, _>) = quad.Item1
  let inline squad_snd (quad:Quad<_, _, _, _>) = quad.Item2
  let inline squad_trd (quad:Quad<_, _, _, _>) = quad.Item3
  let inline squad_fth (quad:Quad<_, _, _, _>) = quad.Item4

  let inline squad_ref_fst (quad:Quad<_, _, _, _> byref) = quad.Item1
  let inline squad_ref_snd (quad:Quad<_, _, _, _> byref) = quad.Item2
  let inline squad_ref_trd (quad:Quad<_, _, _, _> byref) = quad.Item3
  let inline squad_ref_fth (quad:Quad<_, _, _, _> byref) = quad.Item4
Multiple items
type Pair<'a,'b> =
  struct
    new : item1:'a * item2:'b -> Pair<'a,'b>
    val Item1: 'a
    val Item2: 'b
  end

Full name: FSKit.StructTuple.Pair<_,_>

--------------------
Pair()
new : item1:'a * item2:'b -> Pair<'a,'b>
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: FSKit.StructTuple.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: FSKit.StructTuple.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: FSKit.StructTuple.spair
val a : 'a
val b : 'b
val striple : a:'a -> b:'b -> c:'c -> Triple<'a,'b,'c>

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

Full name: FSKit.StructTuple.squad
val d : 'd
val spair_fst : pair:Pair<'a,'b> -> 'a

Full name: FSKit.StructTuple.spair_fst
val pair : Pair<'a,'b>
val spair_snd : pair:Pair<'a,'b> -> 'b

Full name: FSKit.StructTuple.spair_snd
val spair_ref_fst : pair:byref<Pair<'a,'b>> -> 'a

Full name: FSKit.StructTuple.spair_ref_fst
val pair : byref<Pair<'a,'b>>
type byref<'T> = (# "<Common IL Type Omitted>" #)

Full name: Microsoft.FSharp.Core.byref<_>
val spair_ref_snd : pair:byref<Pair<'a,'b>> -> 'b

Full name: FSKit.StructTuple.spair_ref_snd
val striple_fst : triple:Triple<'a,'b,'c> -> 'a

Full name: FSKit.StructTuple.striple_fst
val triple : Triple<'a,'b,'c>
val striple_snd : triple:Triple<'a,'b,'c> -> 'b

Full name: FSKit.StructTuple.striple_snd
val striple_trd : triple:Triple<'a,'b,'c> -> 'c

Full name: FSKit.StructTuple.striple_trd
val striple_ref_fst : triple:byref<Triple<'a,'b,'c>> -> 'a

Full name: FSKit.StructTuple.striple_ref_fst
val triple : byref<Triple<'a,'b,'c>>
val striple_ref_snd : triple:byref<Triple<'a,'b,'c>> -> 'b

Full name: FSKit.StructTuple.striple_ref_snd
val striple_ref_trd : triple:byref<Triple<'a,'b,'c>> -> 'c

Full name: FSKit.StructTuple.striple_ref_trd
val squad_fst : quad:Quad<'a,'b,'c,'d> -> 'a

Full name: FSKit.StructTuple.squad_fst
val quad : Quad<'a,'b,'c,'d>
val squad_snd : quad:Quad<'a,'b,'c,'d> -> 'b

Full name: FSKit.StructTuple.squad_snd
val squad_trd : quad:Quad<'a,'b,'c,'d> -> 'c

Full name: FSKit.StructTuple.squad_trd
val squad_fth : quad:Quad<'a,'b,'c,'d> -> 'd

Full name: FSKit.StructTuple.squad_fth
val squad_ref_fst : quad:byref<Quad<'a,'b,'c,'d>> -> 'a

Full name: FSKit.StructTuple.squad_ref_fst
val quad : byref<Quad<'a,'b,'c,'d>>
val squad_ref_snd : quad:byref<Quad<'a,'b,'c,'d>> -> 'b

Full name: FSKit.StructTuple.squad_ref_snd
val squad_ref_trd : quad:byref<Quad<'a,'b,'c,'d>> -> 'c

Full name: FSKit.StructTuple.squad_ref_trd
val squad_ref_fth : quad:byref<Quad<'a,'b,'c,'d>> -> 'd

Full name: FSKit.StructTuple.squad_ref_fth

More information

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