Created
December 28, 2014 15:01
-
-
Save eiriktsarpalis/0d54b7c06909514bd378 to your computer and use it in GitHub Desktop.
Functional pipelines & F# member constraints
This file contains hidden or 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
| let inline map (alg : ^Alg) (f : ^t -> ^s) (ts : ^ts) : ^ss = | |
| (^Alg : (member Map : (^t -> ^s) * ^ts -> ^ss) (alg, f, ts)) | |
| let inline filter (alg : ^Alg) (f : ^t -> bool) (ts : ^ts) : ^ts = | |
| (^Alg : (member Filter : (^t -> bool) * ^ts -> ^ts) (alg, f, ts)) | |
| let inline reduce (alg : ^Alg) (f : ^t -> ^t -> ^t) (ts : ^ts) : ^t = | |
| (^Alg : (member Reduce : (^t -> ^t -> ^t) * ^ts -> ^t) (alg, f, ts)) | |
| // type signature is unreadable, machine consumable only | |
| let inline test alg xs = | |
| xs | |
| |> filter alg (fun i -> i % 2 = 0) | |
| |> map alg (fun i -> i * i) | |
| |> reduce alg (+) | |
| type ListAlg() = | |
| member __.Map (f : 'T -> 'S, ts : 'T list) = List.map f ts | |
| member __.Filter (f : 'T -> bool, ts : 'T list) = List.filter f ts | |
| member __.Reduce (f : 'T -> 'T -> 'T, ts : 'T list) = List.reduce f ts | |
| let list = new ListAlg() | |
| test list [1 .. 100] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment