Labeled Arguments to List.map?

24 views
Skip to first unread message

James Swaine

unread,
Mar 26, 2015, 9:29:29 AM3/26/15
to ocaml...@googlegroups.com
Is there any particular reason why we need an `f` label for the function argument here?  It seems silly to have this.  I can omit it when I apply List.map, but then the compiler will warn me about label omission.  

In my experience it is almost always obvious in a List.map application which argument is the function and which one is the list.

David House

unread,
Mar 26, 2015, 9:35:35 AM3/26/15
to ocaml...@googlegroups.com
It's really nice for laying code out. E.g. sometimes you have code like this:

  List.map ~f:short_function_name
    (... some large block of code that computes a list)

And sometimes like this:

  List.map list_name ~f:(fun element ->
    ... large block of code)

Being able to swap the order of parameters is a good trick to make the code much easier to read. You don't end up with a "hanging parameter" halfway down the page.

It also plays nicely with |>, i.e. you can write:

  foo
  |> bar
  |> List.map ~f:baz

Of course, in both these examples there are some alternative solutions (using a let binding in the first case, and using a lambda in the second case). But the syntactic overhead is tiny, so it's a "nice to have".

Ps I'd encourage you to make most compiler warnings be fatal errors. Some, like this, are just useful to keep your coding style consistent. Others, like the unused value warnings, have caught far more bugs in my code than I care to admit ;)

On 26 March 2015 at 13:29, James Swaine <james....@getbraintree.com> wrote:
Is there any particular reason why we need an `f` label for the function argument here?  It seems silly to have this.  I can omit it when I apply List.map, but then the compiler will warn me about label omission.  

In my experience it is almost always obvious in a List.map application which argument is the function and which one is the list.

--
You received this message because you are subscribed to the Google Groups "ocaml-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ocaml-core+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Malcolm Matalka

unread,
Mar 26, 2015, 9:37:35 AM3/26/15
to ocaml...@googlegroups.com

As a user of core I like it for consistency with the rest of the api as well.  And I dont have to look up the param order which is nice.

James Swaine

unread,
Mar 26, 2015, 10:44:11 AM3/26/15
to ocaml...@googlegroups.com
Ah, that makes sense.   Thanks!
Reply all
Reply to author
Forward
0 new messages