I suggest we ...

Add syntactic sugar for functions ala Scala/Clojure

Add in shorthand syntax for anonymous functions, even if it is only for single argument functions.

For example:

[1; 2; 3; 4] |> List.map (_ + 1)

As opposed to

[1; 2; 3; 4] |> List.map (fun i -> i + 1)

It would be great to have this shorthand so that our anonymous functions are shorter than the C#ers' :).

37 votes
Vote
Sign in
(thinking…)
Password icon
Signed in as (Sign out)
You have left! (?) (thinking…)
Khan Thompson shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

4 comments

Sign in
(thinking…)
Password icon
Signed in as (Sign out)
Submitting...
  • Bryan Edds commented  ·   ·  Flag as inappropriate

    This is silly. You have many alternative options -

    [1; 2; 3; 4] |> List.map ((+) 1)

    [1; 2; 3; 4] |> List.map (add 1) // where add = (+)

    [1; 2; 3; 4] |> List.map incr // where incr n = n + 1

    These are all equivalent and good enough.

  • Jon Harrop commented  ·   ·  Flag as inappropriate

    Mathematica can also do multivariate anonymous parameters in anonymous functions. So (fun (x, y, z) -> x*y+z) can be written #1*#2+#3& in Mathematica.

  • Jon Harrop commented  ·   ·  Flag as inappropriate

    @Gustavo: I think this is a separate idea. That idea was just a shorthand for a lambda that just invokes a property like (fun foo -> foo.Name) could be #Name. This idea is more general. You could do (_.Name) but with this you can also do (_ + 1) as a shorthand for ((+) 1).

    FWIW, Mathematica has the shorthand notation #+1& for this where # is an anonymous argument in an anonymous function that ends with &. The syntax really is quite hideous!

F# Language

Feedback and Knowledge Base