1 people like it.

Generic fst and snd functions

It seems original design for struct tuples doesn't contain struct versions of fst and snd functions. SRTP FTW!

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
type PairDispatcher = PairDispatcher with
  static member inline ($) (PairDispatcher,        (a, b)) = fun f -> f a b
  static member inline ($) (PairDispatcher, struct (a, b)) = fun f -> f a b
  
let inline fst x = (PairDispatcher $ x) (fun a _ -> a)
let inline snd x = (PairDispatcher $ x) (fun _ b -> b)

let res1 = fst (1, 2)
let res2 = snd struct (1, 2)
Multiple items
union case PairDispatcher.PairDispatcher: PairDispatcher

--------------------
type PairDispatcher =
  | PairDispatcher
  static member ( $ ) : 'a -> (('b -> 'c -> 'd) -> 'd)
  static member ( $ ) : PairDispatcher:PairDispatcher * ('a * 'b) -> (('a -> 'b -> 'c) -> 'c)

Full name: Script.PairDispatcher
val a : 'a
val b : 'b
val f : ('a -> 'b -> 'c)
val f : ('b -> 'c -> 'd)
val fst : 'a * 'b -> 'a

Full name: Script.fst
val x : 'a * 'b
val snd : 'a * 'b -> 'b

Full name: Script.snd
val res1 : int

Full name: Script.res1
val res2 : ('a * 'b -> 'b)

Full name: Script.res2

More information

Link:http://fssnip.net/7TT
Posted:19 days ago
Author:Evgeniy Andreev
Tags: struct , tuple