F # Higher Order Access Elements

I just updated my prototype tuple to the record. Someday it will become a real class. In the meantime, I want to translate the code as follows:

type Example = int * int let examples = [(1,2); (3,4); (5,6)] let descs = Seq.map (fst >> sprintf "%d") examples 

:

 type Example = { Field1 : int Field2 : int Description : string } let examples = [{Field1 = 1; Field2 = 2; Description = "foo"} {Field1 = 3; Field2 = 4; Description = "bar"} {Field1 = 5; Field2 = 6; Description = "baz"}] let descs = Seq.map Description examples 

The problem is that I was expecting to get a Description : Example -> string function when I declared an example record, but I do not. I joked a bit and tried properties on classes, but that doesn't work either. Am I just missing something in the documentation or will I have to write higher order accessors manually? (This is the workaround I'm using now.)

+2
source share
2 answers

I agree that it would be nice to have a way to use a member instance as a function value in F # (without explicitly constructing a lambda function). This has been discussed several times in the F # community. Here is one related link:

A few suggested options from this discussion:

 // This would turn '_' automatically into a lambda parameter // (this looks okay in simple cases, but doesn't probably scale well) examples |> Seq.map (_.Description) // You would specify instance member using special '#' symbol examples |> Seq.map (Example#Description) 

So, this is what the F # team knows about, but I don’t think there is any conclusion whether this is really this important function and how best to support it.

+8
source
 examples |> Seq.map (fun e -> e.Description) 

(A declaration of a record does not create any related functions, but a record has properties, so the tiny lambda, as indicated above, makes it easy to project specific fields.)

0
source

Source: https://habr.com/ru/post/1312596/


All Articles