question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

shortcut syntax for partial function application

See original GitHub issue

Currently, it’s easy enough, but kinda verbose, to partially apply a function:

value formatHex = (Integer n) => Integer.format(n, 16);

The discussion in #6615 raised the possibility of having a shortcut syntax for the special but very common case where we’re partially applying a function of multiple parameters and obtaining a function with exactly one parameter.

Basic proposal

The proposed syntax would be, approximately:

value formatHex = Integer.format(it, 16);

Where it is a “magic identifier” or “soft” keyword. (That is, if there is nothing named it declared in the current scope, it would be given this special interpretation. Furthermore, a warning would be produced wherever the name it is declared.)

A reasonable objection to this proposal is that it is much too much of a special case:

  • it doesn’t handle arbitrary expressions, and
  • it only handles the limited case of exactly one argument.

I think these objections are reasonably dispatched by noting that:

  • Any attempt to make this work for arbitrary expressions runs into the problem of nesting, as discussed in #7190.
  • The single-argument case is, I believe, overwhelmingly the most common case, especially in the context of pipelining discussed in #6615.

Possible extension

An open question is whether this feature could be extended to the receiving instance of a method invocation, in contexts where the receiving instance type can be inferred, for example:

strings.map(it.initial(3));

would mean:

strings.map((s) => s.initial(3))

and:

string |> it.initial(3)) |> list.add

would mean:

string |> ((s) => s.initial(3)) |> list.add

I’m not sure about that, but it’s worth considering.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
xkr47commented, Apr 24, 2018

For the record, I dislike the keyword it, since I use it for iterators 😃

2reactions
luolongcommented, Apr 15, 2018

After seeing the version with soft keyword ‘it’, I tend to agree to a degree with @jensli here that the keyword ‘it’ does not really bring out the fact that we are dealing with a partial application here.

If we stay with keyword approach, I would much prefer it if ‘value’ could be used - along with the keyword like treatment in the rest of the language it is much less likely that it would be confused with a local variable name than ‘it’.

Other than that I do have a preference for using ‘_’ (underscore) as a partial application wildcard. I’ve always seen it as a meaningless sigil to replace some variable name whenever I mean to say “I don’t care what is the input; Just do this”

Read more comments on GitHub >

github_iconTop Results From Across the Web

Syntax Shortcuts | SuperCollider 3.12.2 Help
Syntax Shortcuts : Filter: Introduction. Defining functions and classes. shorter argument lists in definitions. partial application. defining instance variable ...
Read more >
How to create and use partial functions in Scala
A partial function is a function that does not provide an answer for every possible input value it can be given. It provides...
Read more >
Partial Function Application in JavaScript and Flow - Medium
Partial function application is a set of useful techniques which come up frequently in functional programming. This post is a detailed introduction into ......
Read more >
Closures in Q# - Azure Quantum | Microsoft Learn
Partial application is a convenient shorthand for applying some, but not all, of a callable's arguments. The syntax is the same as a...
Read more >
Partial Application with Infix Functions - haskell - Stack Overflow
Yes, you can partially apply an infix operator by specifying either its left or right operand, just leaving the other one blank (exactly...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found