In Clojure I would write the above code:
user=> (def points [{:x 11 :y 12} {:x 21 :y 22}])
#'user/points
user=> (map :x r)
(11 21)
I can do this because :x can be used as a function. This is a very useful feature
The same code in F# would look like this:
> type Point = {x : int; y: int};;
> let points = [{x=11;y=12}; {x=21; y=22}];;
> List.map (fun p -> p.x) points
val it : int list = [11; 21]
Because I hate writing the anonymous function all the time I find myself writing the type Point like this:
type Point =
{
x: int
y : int
}
static member getX p = p.x;;
...which gives me the possibility of doing:
> List.map Point.getX points
This is still messy because I need to write a getter for each record member that I use.
Instead what I would like is a syntax like this:
> List.map Point.x points
Is there a way to do this without having to write the messy anonymous function (fun p -> p.x) or the static getter?
UPDATE:
By the way, Haskell also does it the same as Clojure (actually is the other way around):
Prelude> data Point = Point { x :: Int, y :: Int}
Prelude> let p = Point { x = 11, y=22}
Prelude> x p
11
UPDATE 2:
Hopefully a more obvious reason against the lambda is an example when the type inference doesn't work without help:
type Point2D = { x : int; y : int}
type Point3D = { x : int; y : int; z : int}
let get2dXes = List.map (fun (p:Point2D) -> p.x)
let get2dXes' : Point2D list -> int list = List.map (fun p -> p.x)
let get2dXes'' (ps : Point2D list) = List.map (fun p -> p.x) ps
...which are way less elegant than something like:
let get2dXes = List.map Point2D.x
I don't want to start flame wars about which syntax is better. I was just sincerely hoping that there is some elegant way to do the above since I haven't found any myself.
Apparently all I can do is pray to the mighty gods of F# to include a feature like this in a future version, next to the type classes ;)
UPDATE 3:
This feature is already proposed for a future language version. https://fslang.uservoice.com/forums/245727-f-language/suggestions/5663326-syntax-for-turning-properties-into-functions Thanks JackP.!