Last active
December 25, 2023 07:35
-
-
Save habib-sadullaev/c9841d022c90aef327d7db9e8b1a0a1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Tuple = Tuple with | |
static member inline TupleMap (Tuple, (a, b)) = fun f -> (f a, f b) | |
static member inline TupleMap (Tuple, (a, b, c)) = fun f -> (f a, f b, f c) | |
static member inline TupleMap (Tuple, (a, b, c, d)) = fun f -> (f a, f b, f c, f d) | |
let inline mapTuple f x = | |
let inline map (a: ^a) (b : ^b) = ((^a or ^b) : (static member TupleMap : ^a * ^b -> ^t) (a, b)) | |
map Tuple x f | |
mapTuple (fun x -> x * x + 1) (1, 2) | |
mapTuple string (1, 2, 3) | |
mapTuple id (1, 2, 3, 4) |
type Show =
static member asString (x: int) : string = string x
static member asString (x: double) : string = string x
let inline show x =
let inline resolve (_: ^t) (x: ^x) = ((^t or ^x) : (static member asString: ^x -> string) x)
resolve Unchecked.defaultof<Show> x
type Show with
static member inline asString (x: ^t option) : string =
match x with
| Some x -> show x
| None -> ""
show 1 |> printfn "%s"
show 2.9 |> printfn "%s"
show "3" |> printfn "%s"
show (Some 5) |> printfn "%s"
show (None: int option) |> printfn "%s"
show (Some 6L) |> printfn "%s" // error
show 1L |> printfn "%s" // error
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/Lanayx/18c21ea6319deb04be673e58958bca89