Scala: why are parameters renamed? - scala

I am studying rxScal example at https://github.com/ReactiveX/RxScala/blob/0.x/examples/src/test/scala/examples/RxScalaDemo.scala
This example contains following code:
o.subscribe(
n => println(n),
e => e.printStackTrace(),
() => println("done")
)
But the signature of subscribe is:
def foreach(onNext: T => Unit, onError: Throwable => Unit): Unit = {
asJavaObservable.subscribe(onNext, onError)
}
I do not understand who renames onNext to n?

n => println(n) is an anonymous function (a function with no name) that takes an argument and acts on it. That argument is labeled n but it could have been x or wwz, or whatever.
When you pass that function as an argument to another function, the receiving function will give the received argument a name. The first argument to foreach is labeled onNext but it could have been x or zzw or whatever. It is the name of the received function. It doesn't matter what the name of that function was when it was passed to foreach, maybe it was anonymous and had no name, inside the foreach code it now has the name onNext.
Inside foreach, onNext is the name of a function that takes an argument and acts on it. The name of that argument is unknown and immaterial to the foreach code.
So, in short, onNext hasn't been renamed to n. They are, essentially, unrelated.

You have given the signature of foreach, not subscribe. It looks like the signature of subscribe is:
def subscribe(onNext: T => Unit, onError: Throwable => Unit, onComplete: Unit => Unit): Subscription
this means that
n => println(n)
is a function which is executed when the next value of T arrives. Therefore n has type T and is bound to the incoming value. Similarly
e => e.printStackTrace()
is a function Throwable => Unit so e has type Throwable and will be bound to the error parameter given to onError of the Observable.

Perhaps it will be clearer if we remove some of the Scala magic so we can think about the code in more deliberate steps:
def subscribe(onNext: AnyRef => Unit, onError: Throwable => Unit, onComplete: Unit => Unit): Subscription = ...
val myOnNext: AnyRef => Unit = n => println(n)
val myOnError: Throwable => Unit = e => e.printStackTrace()
val myOnComplete: Unit => Unit = _ => println("done")
o.subscribe(myOnNext, myOnError, myOnComplete)

Related

what is wrong with this partially applied function attempt?

I am trying to write a partially applied function. I thought the below would work but it doesn't. Grateful for any help.
scala> def doSth(f: => Unit) { f }
doSth: (f: => Unit)Unit
scala> def sth() = { println ("Hi there") }
sth: ()Unit
scala> doSth(sth)
Hi there
scala> val b = sth _
b: () => Unit = <function0>
scala> doSth(b)
<console>:11: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses
doSth(b)
^
Thanks!
The difference is subtle. sth is a method, so you can call it without the parentheses, which is what is happening here:
doSth(sth)
But b is just a function () => Unit, which you must use the parentheses to invoke.
doSth(b())
Otherwise you would not be able to assign b to another identifier.
val c: () => Unit = b
If we automatically invoked b here, c would be Unit instead of () => Unit. The ambiguity must be removed.
Let me also clarify that doSth is not a method that accepts only functions. f: => Unit means it accepts anything that evaluates to Unit, which includes methods that return Unit when they are invoked. doSth(sth) is not passing the message sth to doSth, it's invoking sth without parentheses and passing the result.
I think the difference is that b has type () => Unit, and doSth expects an expression that returns Unit (minor difference). If you want to store sth as a variable without the side effects, you could do:
lazy val b = sth
doSth(b)
The other option would be to make it so that doSth takes in () => Unit:
def doSth(f: () => Unit) { f() }
def sth() = { println ("Hi there") }
doSth(sth)
val b = sth _
doSth(b)

scala -- syntax to indicate any kind of anonymous function, whatsoever

I'd like to be able to pass in callback functions as parameters to a method. Right now, I can pass in a function of signature () => Unit, as in
def doSomething(fn:() => Unit) {
//... do something
fn()
}
which is fine, I suppose, but I'd like to be able to pass in any function with any parameters and any return type.
Is there a syntax to do that?
Thanks
To be able to execute a function passed as a callback you have to be able to call it with the arguments it requires. f: A => R must be called as f(someA), and g: (A,B) => R must be called as f(someA, someB).
Implementing a callback you want a function that accepts a context and returns something (you don't care what the something is). For example foreach in scala.Option is defined as:
def foreach[U](f: (A) => U): Unit
The function is called with a value of type A but the result is discarded so we don't care what type it has. Alternatively if you don't care what was completed the callback function could be implemented as:
def onComplete[U](f: () => U): Unit
It would then allow being called with any function that takes no arguments:
val f: () => Int = ...
val g: () => String = ...
onComplete(f) // In result is discarded
onComplete(g) // String result is discarded
If you really wanted a function that accepted any possible function there are some tricks you could use (but you really shouldn't). For example you could define a view bound with implicit conversions:
// Type F is any type that can be converted to () => U
def onComplete[F <% () => U, U](f: F): Unit
// Define conversions for each Function type to Function0
implicit def f1tof0[A,U](f: A => U): () => U = ...
implicit def f2tof0[A,B,U](f: (A,B) => U): () => U = ...
implicit def f3tof0[A,B,C,U](f: (A,B,C) => U): () => U = ...
.
.
etc.
Now onComplete accepts any function:
val f: Int => String = ...
val g: (List[Int], String) => String = ...
onComplete(f)
onComplete(g)
Defining the types for the above conversions is relatively simple, but there is no rational way to implement them so is pretty much entirely useless.

Confused about this Scala syntax

Could someone please explain this piece of code:
val ns: Iterator[Int] = (1 to 3).iterator
ns.map(_ => () => block)
where
block: => Future[T]
Basically I'm just confused about what's going on with the _, =>, and Unit () syntax. The way I am trying to read it is 'we map a function which returns a function that takes no params and returns a Future[T] over the list of integers'. Is this correct? I'm always confused when I see multiple => operators in a row..
Yes, you are correct.
It's parsed like this:
ns.map(_ => (() => block))
And the () => ? syntax is just defining a parameterless function:
val f = () => 1
f() // 1
So the integer in ns is ignored, and you just get an iterator of functions that take no parameters and return Futures: Iterator[() => Future[T]].
I think this block: => Future[T] maybe the signature of the method. It means call-by-name. Basically, you can understand that it is just a type Future[T] which is lazy evaluated but not a function
val ns: Iterator[Int] = (1 to 3).iterator
ns.map(_ => () => block)
This will return Iterator[() => Future], so yes, you are right, this transform to a function which takes nothing to Future. The good thing of this is that it will not evaluate the Future immediately, it will be called when you invoked.
val result = ns.map(_ => () => block)
result.foreach(_())

Implementing a higher order function that performs currying in scala

A coworker of mine sent me a question as follows:
Implement a HOF(higher order function) that performs currying, the
signature of your function is as follows:
def curry[A,B,C](f:(A,B) => C) : A => B => C
Similarly, implement a function that performs uncurrying as follows:
def uncurry[A,B,C](f:A => B => C): (A,B) => C
The way I understand currying is that if you have a function that takes multiple parameters, you can repeatedly apply the function to each one of the paramaters until you get the result.
So something along the lines of f:(A,B) => C turns into A => f(A,_) => f(B)????
And uncurrying would be to consolidate this application into one function as follows:
f:A=>B=>C would be f(A,B)?
Maybe I am just being confused by the syntax here but it would be great if somebody could point out what I am missing here.
Thanks
Hopefully this fully worked example with a bunch of comments is easy to understand. Please reply if you have questions.
You can execute this code by dropping it in a Scala interpreter.
// Here's a trait encapsulating the definition your coworker sent.
trait Given {
def curry[A,B,C](f:(A,B) => C) : A => B => C
def uncurry[A,B,C](f:A => B => C): (A,B) => C
}
object Impl extends Given {
// I'm going to implement uncurry first because it's the easier of the
// two to understand. The bit in curly braces after the equal sign is a
// function literal which takes two arguments and applies the to (i.e.
// uses it as the arguments for) a function which returns a function.
// It then passes the second argument to the returned function.
// Finally it returns the value of the second function.
def uncurry[A,B,C](f:A => B => C): (A,B) => C = { (a: A, b: B) => f(a)(b) }
// The bit in curly braces after the equal sign is a function literal
// which takes one argument and returns a new function. I.e., curry()
// returns a function which when called returns another function
def curry[A,B,C](f:(A,B) => C) : A => B => C = { (a: A) => { (b: B) => f(a,b) } }
}
def add(a: Int, b: Long): Double = a.toDouble + b
val spicyAdd = Impl.curry(add)
println(spicyAdd(1)(2L)) // prints "3.0"
val increment = spicyAdd(1) // increment holds a function which takes a long and adds 1 to it.
println(increment(1L)) // prints "2.0"
val unspicedAdd = Impl.uncurry(spicyAdd)
println(unspicedAdd(4, 5L)) // prints "9.0"
How about a less numerical example?
def log(level: String, message: String) {
println("%s: %s".format(level, message))
}
val spicyLog = Impl.curry(log) // spicyLog's type is String => Unit
val logDebug = spicyLog("debug") // This new function will always prefix the log
// message with "debug".
val logWarn = spicyLog("warn") // This new function will always prefix the log
// message with "warn".
logDebug("Hi, sc_ray!") // prints "debug: Hi, sc_ray!"
logWarn("Something is wrong.") // prints "warn: Something is wrong."
Update
You replied asking "How does the compiler evaluate expressions such as a => b => f(a,b)." Well it doesn't. At least the way things are defined in your coworker's snippet, that wouldn't compile. In general, though, if you see something of the form A => B => C that means "a function which takes an A as an argument; it returns a function which takes a B as an argument and returns a C."
I'm not sure I really understand your question - what would you like to know, besides the actual implementation? As described, it should be quite trivial:
def curry[A,B,C](f:(A,B) => C): A => B => C =
a => b => f(a,b)
What a => b => f(a,b) means is, "a function of one argument, a, whose return value is b => f(a,b) which is again, a function of one argument, b, whose return value is what you get of you execute f(a,b) (whose type is C)"
a => b => f(a, b) can be written slightly more verbosely if it helps?
{ (a: A) => { // a function of *one* argument, `a`
(b: B) => { // a function of *one* argument, `b`
f(a, b) // whose return value is what you get of you execute `f(a,b)` (whose type is `C`)
}
}
}
and
def uncurry[A,B,C](f:A => B => C): (A,B) => C =
(a, b) => f(a)(b)
Where (a, b) => f(a)(b) means, "A function of two arguments (a, b), whose return value is what you get when you first apply a to the HoF f, which returns a function that in turn consumes the b to return a C".
Does that help?

Help me understand this Scala code: scalaz IO Monad and implicits

This is a followup to this question.
Here's the code I'm trying to understand (it's from http://apocalisp.wordpress.com/2010/10/17/scalaz-tutorial-enumeration-based-io-with-iteratees/):
object io {
sealed trait IO[A] {
def unsafePerformIO: A
}
object IO {
def apply[A](a: => A): IO[A] = new IO[A] {
def unsafePerformIO = a
}
}
implicit val IOMonad = new Monad[IO] {
def pure[A](a: => A): IO[A] = IO(a)
def bind[A,B](a: IO[A], f: A => IO[B]): IO[B] = IO {
implicitly[Monad[Function0]].bind(() => a.unsafePerformIO,
(x:A) => () => f(x).unsafePerformIO)()
}
}
}
This code is used like this (I'm assuming an import io._ is implied)
def bufferFile(f: File) = IO { new BufferedReader(new FileReader(f)) }
def closeReader(r: Reader) = IO { r.close }
def bracket[A,B,C](init: IO[A], fin: A => IO[B], body: A => IO[C]): IO[C] = for { a <- init
c <- body(a)
_ <- fin(a) } yield c
def enumFile[A](f: File, i: IterV[String, A]): IO[IterV[String, A]] = bracket(bufferFile(f),
closeReader(_:BufferedReader),
enumReader(_:BufferedReader, i))
I'm now trying to understand the implicit val IOMonad definition. Here's how I understand it. This is a scalaz.Monad, so it needs to define pure and bind abstract values of the scalaz.Monad trait.
pure takes a value and turns it into a value contained in the "container" type. For example it could take an Int and return a List[Int]. This seems pretty simple.
bind takes a "container" type and a function that maps the type that the container holds to another type. The value that is returned is the same container type, but it's now holding a new type. An example would be taking a List[Int] and mapping it to a List[String] using a function that maps Ints to Strings. Is bind pretty much the same as map?
The implementation of bind is where I'm stuck. Here's the code:
def bind[A,B](a: IO[A], f: A => IO[B]): IO[B] = IO {
implicitly[Monad[Function0]].bind(() => a.unsafePerformIO,
(x:A) => () => f(x).unsafePerformIO)()
}
This definition takes IO[A] and maps it to IO[B] using a function that takes an A and returns an IO[B]. I guess to do this, it has to use flatMap to "flatten" the result (correct?).
The = IO { ... } is the same as
= new IO[A] {
def unsafePerformIO = implicitly[Monad[Function0]].bind(() => a.unsafePerformIO,
(x:A) => () => f(x).unsafePerformIO)()
}
}
I think?
the implicitly method looks for an implicit value (value, right?) that implements Monad[Function0]. Where does this implicit definition come from? I'm guessing this is from the implicit val IOMonad = new Monad[IO] {...} definition, but we're inside that definition right now and things get a little circular and my brain starts to get stuck in an infinite loop :)
Also, the first argument to bind (() => a.unsafePerformIO) seems to be a function that takes no parameters and returns a.unsafePerformIO. How should I read this? bind takes a container type as its first argument, so maybe () => a.unsafePerformIO resolves to a container type?
IO[A] is intended to represent an Action returning an A, where the result of the Action may depend on the environment (meaning anything, values of variables, file system, system time...) and the execution of the action may also modify the environment. Actually, scala type for an Action would be Function0. Function0[A] returns an A when called and it is certainly allowed to depend on and modify the environment. IO is Function0 under another name, but it is intended to distinguish (tag?) those Function0 which depends on the environment from the other ones, which are actually pure value (if you say f is a function[A] which always returns the same value, without any side effect, there is no much difference between f and its result). To be precise, it is not so much that function tagged as IO must have side effect. It is that those not so tagged must have none. Note however than wrapping impure functions in IO is entirely voluntary, there is no way you will have a guarantee when you get a Function0 that it is pure. Using IO is certainly not the dominant style in scala.
pure takes a value and turns it into a value contained in the
"container" type.
Quite right, but "container" may mean quite a lot of things. And the one returned by pure must be as light as possible, it must be the one that makes no difference. The point of list is that they may have any number of values. The one returned by pure must have one. The point of IO is that it depends on and affect the environment. The one returned by pure must do no such thing. So it is actually the pure Function0 () => a, wrapped in IO.
bind pretty much the same as map
Not so, bind is the same as flatMap. As you write, map would receive a function from Int to String, but here you have the function from Int to List[String]
Now, forget IO for a moment and consider what bind/flatMap would mean for an Action, that is for Function0.
Let's have
val askUserForLineNumber: () => Int = {...}
val readingLineAt: Int => Function0[String] = {i: Int => () => ...}
Now if we must combine, as bind/flatMap does, those items to get an action that returns a String, what it must be is pretty clear: ask the reader for the line number, read that line and returns it. That would be
val askForLineNumberAndReadIt= () => {
val lineNumber : Int = askUserForLineNumber()
val readingRequiredLine: Function0[String] = readingLineAt(line)
val lineContent= readingRequiredLine()
lineContent
}
More generically
def bind[A,B](a: Function0[A], f: A => Function0[B]) = () => {
val value = a()
val nextAction = f(value)
val result = nextAction()
result
}
And shorter:
def bind[A,B](a: Function0[A], f: A => Function0[B])
= () => {f(a())()}
So we know what bind must be for Function0, pure is clear too. We can do
object ActionMonad extends Monad[Function0] {
def pure[A](a: => A) = () => a
def bind[A,B](a: () => A, f: A => Function0[B]) = () => f(a())()
}
Now, IO is Function0 in disguise. Instead of just doing a(), we must do a.unsafePerformIO. And to define one, instead of () => body, we write IO {body}
So there could be
object IOMonad extends Monad[IO] {
def pure[A](a: => A) = IO {a}
def bind[A,B](a: IO[A], f: A => IO[B]) = IO {f(a.unsafePerformIO).unsafePerformIO}
}
In my view, that would be good enough. But in fact it repeats the ActionMonad. The point in the code you refer to is to avoid that and reuse what is done for Function0 instead. One goes easily from IO to Function0 (with () => io.unsafePerformIo) as well as from Function0 to IO (with IO { action() }). If you have f: A => IO[B], you can also change that to f: A => Function0[B], just by composing with the IO to Function0 transform, so (x: A) => f(x).unsafePerformIO.
What happens here in the bind of IO is:
() => a.unsafePerformIO: turn a into a Function0
(x:A) => () => f(x).unsafePerformIO): turn f into an A => Function0[B]
implicitly[Monad[Function0]]: get the default monad for Function0, the very same as the ActionMonad above
bind(...): apply the bind of the Function0 monad to the arguments a and f that have just been converted to Function0
The enclosing IO{...}: convert the result back to IO.
(Not sure I like it much)