Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# shorthand to call method on object in lambda

I think this is somewhat related to this question, but being not sure and since there's no real answer there, here I go:
in Scala there's you can write code such as:

aStringArray.map(_.toUpperCase())

which is shorthand for:

aStringArray.map(s => s.toUpperCase())

Is there anything like this in F# or a way to implement it (without the ? operator or heavy use of reflection)? If that's not possible, is this being considered as a language feature for a future version? (I really the verbosity of functions just to call a method on an object in a closure!).

like image 887
em70 Avatar asked Sep 29 '10 11:09

em70


3 Answers

As Dario points out, a feature like _.Foo() syntax in Scala is not currently available in F#, so you'll have to write a lambda function explicitly using fun a -> a.Foo().

As far as I know, a question like this appears every now and then on F# discussions, so it was surely considered by the F# team. It is a bit tricky (e.g. do you want to allow just member uses or other uses e.g. _ + 10, and what would be the scope of the lambda expression?) Also, the value of the feature is relatively low compared to other things that could be done... Anyway, it would be nice to have it!

This may be also a reason why many F# types expose operations as both members (usable in the OO style) and functions in a module (for the functional style). I think you could consider it as a good F# practice (but it depends on your design preferences). E.g.:

type Foo(a:int) = 
  member x.Add(b:int) = a + b

// Attribute allows you to define module with the same name
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Foo = 
  let add b (v:Foo) = v.Add(b)

Then you can use both fun v -> v.Add(10) and Foo.add 10 to create a function value. Unfortunately, the String type doesn't have corresponding module with all the functions in the core F# libraries, so you'd have to write it yourself.

like image 138
Tomas Petricek Avatar answered Oct 31 '22 11:10

Tomas Petricek


Just use fun s -> s.ToUpper() as the parameter, which creates the same kind of anonymous function Scala does.

Note that this nifty _ trick from Scala doesn't (currently) exist in F#, but if you weren't calling an object method, you could just use partial evaluation like in

filter ((=) 42) list
like image 43
Dario Avatar answered Oct 31 '22 12:10

Dario


See this forum thread:

http://cs.hubfs.net/forums/permalink/13313/13319/ShowThread.aspx#13319

The F# team has been over this territory a number of times, but nothing workable has ever come of it. At this point I think it's doubtful we'll introduce a new syntactic form that saves less than 10 characters.

like image 33
Brian Avatar answered Oct 31 '22 11:10

Brian