views:

124

answers:

4

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!).

A: 

This isn't really the answer you wanted, but currently I work around this by creating dummy static functions (if I'm going to be needing the same thing several times), as it's prettier than the lambda syntax:

let toupper (s:string) = s.ToUpper()
aStringArray.map toupper
Benjol
`(fun s -> s.toupper())`
Brian
...or `let toUpper (s:string) = s.ToUpper()` if you want to define a named function.
Tomas Petricek
@Tomas, yes, that's what I was aiming for, forgot the type definition.
Benjol
+3  A: 

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
Dario
+3  A: 

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.

Tomas Petricek
+1  A: 

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.

Brian
It is a small number of characters but makes it conceptually much easier, at least for me. Of course it would be simpler if `a.b` is thought of as applying a dot operator. Then `.b` would be a natural syntax.
Muhammad Alkarouri
Besides the save in terms of characters, which given the frequency this code appears when using classic Oo libraries would be massive anyway, there's also another huge advantage: it reads much better.
emaster70