1

Here is another stupid scala question regarding functions as first class objects in Scala. I'm very sorry if this is a repeat, as it probably is.

In Python, Lisp, Perl, Scheme, etcetera I'm used to creating function values and assigning them names and passing them around to other functions, like this in python:

>>> def yyy(c):
...     return c.upper()
... 
>>> yyy
<function yyy at 0x1032965f0>
>>> yyy('a')
'A'
>>> def compose(f,g):
...     def composition(x):
...         return f(g(x))
...     return composition
... 
>>> compose(ord, yyy)('a')
65

In scala I don't know how to do this because scala always wants to evaluate the function as far as I can tell. How do you refer to an unevaluated function in scala and pass it around, save it in data structures, etcetera?

For example, I can't do it this way, apparently:

scala> def yyy(c: Char) = {
     |     c.toUpper
     | }
    yyy: (c: Char)Char

scala> yyy('a')
res5: Char = A

scala> yyy
<console>:9: error: missing arguments for method yyy;
follow this method with `_' if you want to treat it as a partially
applied function
          yyy
          ^

scala> yyy_
<console>:8: error: not found: value yyy_
          yyy_
          ^

As you can see, I tried and failed to make sense of the "_" hint. Any clues? How do you refer to a function without evaluating it?

Aaron Watters
  • 2,784
  • 3
  • 23
  • 37
  • 2
    you have to add a whitespace before _ ("yyy _" and not "yyy_") – volia17 Jun 15 '15 at 13:50
  • `yyy` is not a function, it's a method. You have to either convert it to a function using η-expansion (`yyy _`), or use a function in the first place (`val yyy = (c: Char) => c.toUpper`). – Jörg W Mittag Jun 15 '15 at 14:02
  • possible duplicate of [Difference between method and function in Scala](http://stackoverflow.com/questions/2529184/difference-between-method-and-function-in-scala) – Jörg W Mittag Jun 15 '15 at 14:03

1 Answers1

4

yyy is not a function, it's a method. You have to either convert it to a function using η-expansion

yyy _

or use a function in the first place

val yyy = (c: Char) => c.toUpper
// or
val yyy: Char => Char = c => c.toUpper
// or
val yyy: Char => Char = _.toUpper
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • Thanks. So as I understand it one of the critical distinctions between a method and a function is that a method can be overloaded, whereas a function cannot be overloaded, and this is why the distinction is needed. Is this right? So you can't pass methods around because if the method is overloaded it is not clear which implementation to pass... – Aaron Watters Jun 15 '15 at 14:12
  • 5
    @AaronWatters The critical distinction here is that methods *belong* to objects (and, as such, have access to `self`/`this`), whereas functions don't belong to objects and can be freely passed around. – dcastro Jun 15 '15 at 14:21
  • 1
    //or `val yyy = (_: Char).toUpper` – senia Jun 15 '15 at 14:31
  • 2
    The other critical distinction is that functions are first-class (they are mere objects that can be passed around), while methods are not. – Régis Jean-Gilles Jun 15 '15 at 15:12