Scala map cannot resolve mapper function - scala

Scala noob here.
I'm trying to apply a map in a class method:
class Miner(args: Args) extends Job(args) {
def fetchUrl(url:String) = {
...
}
TextLine(args("input")).map(url: String => fetchUrl(url))
.write(args("output"))
}
this codes breaks complaining about not being able to resolve the symbol fetchUrl.
I've thought that, fetchUrl being a one argument function, I could just omit the argument and do something like:
TextLine(args("input")).map(fetchUrl)
.write(args("output"))
This now breaks saying that I'm missing arguments for the method fetchUrl.
What gives?

Isn't mapTo a curried function?
I imagine you use this function from this object: (google redirects me to that)
mapTo[U](out: Fields)(mf: (String) ⇒ U)(implicit flowDef: FlowDef, mode: Mode, setter: TupleSetter[U]): Pipe
Perhaps you would use it like this: Textline.mapTo(args("input"))(fetchUrl)
You have some examples of mapTo usage at this page, but based on the Pipe object:
https://github.com/twitter/scalding/wiki/Fields-based-API-Reference#map-functions
Excerpt:
val savings =
items.mapTo(('price, 'discountedPrice) -> 'savings) {
x : (Float, Float) =>
val (price, discountedPrice) = x
price - discountedPrice
}
So not based on TextLine in this example, but also curried...this might be a good hint for you.

Related

Is it possible in scala to have a generic method taking a method as an argument?

I would like to have:
def myMethod[T < ???](f: T): Unit = {
f()
}
The rest of the method is not really important but is it possible to replace ??? by somethind which would make sure T is a method
and if possible to go even further and make sure the return type of T is something defined ?
Like [T < (_*) => Int]
Thank you.
Would defining a type, like in the following trivial example, address your need?
type Fcn = String => Int
def myMethod(s: String, f: Fcn): Unit = {
println(f(s))
}
myMethod("hello", (s: String) => s.length)
// 5
You can use function literals:
def myMethod[A, T](f: A => T) {
f(someAValue)
}
or if you want functions that take no arguments:
def myMethod[T](f: () => T) {
f()
}
But judging by your comments, it seems like you specifically want to reference methods, and query the method for information about itself, which is not a feature of basic Scala. You may be able to do some of that with reflection, but reflection is an area best avoided.
Technically, you can't directly pass a method, since a method is not a value that you can instantiate and pass-around. What is usually passed around is a function instance. And when it appears that you're passing a method, the compiler is actually creating a function instance for you (method lifting or eta-expansion).
But that is not going to work if you're looking to inspect meta data about that method (name, deprecation). For that, you're probably looking for reflection. You can have your method take an instance of java.reflect.Method but that will require that you obtain that at call-site.

Scala use of underscore as an object placeholder

Trying to wrap my head around the varying uses of the _. Right now I'm struggling with this example:
object Chapter9 extends App {
FileMatcher.filesEnding(".scala").foreach(println)
}
object FileMatcher {
private def filesHere = (new java.io.File(".")).listFiles
private def filesMatching(matcher: String => Boolean) = {
for (file <- filesHere; if matcher(file.getName))
yield file
}
def filesEnding(query: String) =
filesMatching(_.endsWith(query))
def filesContaining(query: String) =
filesMatching(_.contains(query))
def filesRegex(query: String) =
filesMatching(_.matches(query))
}
So clearly we want to abstract away the common work of looping/filtering/yielding for the varying types of matchers, makes sense to put it in a helper function.
I'm getting hung up on the _.endsWith part. My understanding is that this underscore (being the first and only one used in the method body) will be filled in by the first parameter, which in this case is query. I tried to test this theory by doing:
def filesEnding(query: String) = {
println(_: String)
}
But the program doesn't print anything. So what is _ here? How does Scala know what object to to search for an endsWith method on?
It looks like from output of the program that somehow file gets filled in for this underscore but have no idea how. Maybe the underscore remains a "wildcard" until it is used inside filesMatching's body and by that point the nearest enclosing scope is the for and the first "parameterisfile`?
Look at the signature for filesMatching(). Notice that it takes one argument of type String => Boolean. So its argument is a function that itself take a String argument and turns it into a Boolean.
Now remember that an anonymous function often looks something like this:
{ x => /* do something with x */ }
And in cases where x is used only once, then that can be abbreviated to a single _. So, working backwards, this
filesMatching(_.endsWith(query))
can be rewritten as this
filesMatching(x => x.endsWith(query))
So the filesMatching() code has its argument, a function that takes a string (which in the anonymous function I've called x). That function, matcher, is invoked with the string file.getName to get a Boolean. That boolean value is tested in an if clause:
if matcher(file.getName)
TL;DR: The underscore is shorthand for the file.getName string.
The canonical answer is What are all the uses of an underscore in Scala?
But -Xprint:parser shows
((x$1: String) => println((x$1: String)))
which is uninteresting except for the redundantly typed expression in the body of the function.
It doesn't seem to generate any extra code. The param is already a String.
I don't think your example compiles? Or I don't know what you're asking.
Explicit types can help debug when type of an anonymous function aren't inferred as you wish.
Edit: I gave this a try:
object Chapter9 extends App {
FileMatcher.filesEnding(".scala").foreach(println)
}
object FileMatcher {
private def filesHere = (new java.io.File(".")).listFiles
private def filesMatching(matcher: String => Boolean) = {
for (file <- filesHere; if matcher(file.getName))
yield file
}
def filesEnding(query: String) = {
println(_: String)
}
}
An expression with an underscore as an anonymous function needs its expected type to tell it what type the underscore is, unless explicitly annotated as you did. But that is not common usage.
Instead of (_: Int) * 2, (i: Int) => i * 2, but that's a style question.

Seemingly spurious "does not take arguments" error with case class constructor

I have a case class that has a few parameters for its constructor, and I define an accompanying class object that defines an alternative constructor that takes a different set of arguments, like so:
case class MyClass (c: Char, mc: List[MyClass])
object MyClass {
def apply(c: Char, mc: MyClass): MyClass = {
MyClass(c, List(mc))
}
}
I want to use the original case class constructor in a foldRight:
object SomeStuff {
def problem (s: String) {
assert(!s.isEmpty)
val zero = MyClass('a', Nil)
val mc2 = "Pillsy Pillsy Pillsy" foldRight(zero) {
MyClass(_, List(_))
}
}
}
When I do this, I get an error message from the compiler: "MyClass does not take parameters." If I comment out the section val mc2 = ... this problem goes away, but MyClass is clearly taking parameters in the definition of zero. I feel like I must be missing something really basic, but I have no idea what it is. I've tried a couple workarounds, like defining a helper method or a function value to use as the second argument of foldRight, but none of it helps, which is not too surprising since I'm basically fumbling around randomly.
First, foldRight takes two arguments, so you can't use operator notation (in its current form), but you can:
("foo" foldRight zero) { ... }
or alternatively,
("foo" :\ zero) { ... }
Second, { MyClass(_, List(_)) } will desugar into
{ x => MyClass(x, y => List(y)) }
so, you should name arguments and use new keyword to ensure that constructor is called instead of apply method:
"Pillsy Pillsy Pillsy".foldRight(zero) { (c, mc) =>
new MyClass(c, List(mc))
}
or use supplemental apply method if you want to go with underscores:
"Pillsy Pillsy Pillsy".foldRight(zero) { MyClass(_, _) }

Why do I need a curried function to be able to pass function literals with short placeholder syntax?

Given this definition:
class Foo(var x: String) {}
object Helper {
def model[T](get: ⇒ T, set: T ⇒ Unit) : Model[T] = new Model[T] {
override def getObject(): T = get
override def setObject(obj: T) { set(obj) }
}
}
I try to call model like this:
val f = new Foo("initial")
val stringModel = model(f.x, f.x = _)
But that doesn't work, the compiler gives me this, complaining about the underscore:
missing parameter type for expanded function ((x$1) => f.x = x$1)
If I change the definition of model to use two parameter lists like this:
def model[T](get: ⇒ T)(set: T ⇒ Unit) // rest is unchanged
Then I can call it like this:
model(f.x)(f.x = _)
Which I find nice and concise. I don't really mind doing it like this, though it makes method overloading harder. I would like to understand, however, why the second variant works and the first one doesn't?
The second variant works because Scala refines its types parameter-block by parameter-block. If you don't specify the type of your input parameter for the function, it's possible that it would change the type T that it has inferred based on the first parameter. If you push it to a separate parameter block, Scala's already decided what T must be by the time it hits that block, so it fills in the only possible value for the function argument type.

How to define a method in Scala that returns a Java object?

I want to define a private method in scala singleton class that looks like;
private def createDomNode(tag: String, attrs: Map[String , String]): DomNode {
}
DomNode is Java type, not scala type. attrs is scala Map with both key and value being of type String.
But above gives error. What is the correct format?
Thanks Easy Angel for the answer. There is still some confusion. According to Programming in Scala book written by the inventor of the language, the below is a function:
def max(x: Int, y: Int): Int = {
if (x > y) x
else y
}
But your answer says it is method and not function. Can you kindly explain?
What is REPL?
You should just put =:
private def createDomNode(tag: String, attrs: Map[String , String]): DomNode = {
// ...
}
If you will not put = between method signature and body, then return type is Unit, so this:
def m(i: Int) {}
is the same as
def m(i: Int): Unit = {}
Response to the comment: What I described earlier is actually method, if you define it within object, class or trait definition. Function syntax would look like this:
val createDomNode: (String, Map[String , String]) => DomNode = { (tag, attrs) =>
// ...
}
As You can see I define val with name createDomNode of function type. It can also be written like:
val createDomNode: Function2[String, Map[String , String], DomNode] = { (tag, attrs) =>
// ...
}
here is another example. In this case I define method that generates new function each time you call it:
def createDomNode = (tag: String, attrs: Map[String , String]) => new DomNode
But it's important to understand, that method returns a "function that returns DomNode" in this case, but not DomNode itself.
About Programming in Scala reference. I think you are talking about Chapter 2 - Step 3 (in the intro)
As you can see max function is defined in REPL, and it's really function. Actually you can also write something like this:
class MyClass {
def myMethod(i: Int): Int = {
def myInnerFn(x: Int) = x * x
myInnerFn(i)
}
}
In this case myMethod is method and myInnerFn is function. So as you can see, it highly depends on the context. I believe this syntax for myInnerFn is just syntactic sugar for (I need to look in spec in order to say for sure):
val myInnerFn = (x: Int) => x * x
The same happens in REPL. And by the way that's because I wrote at the beginning:
if you define it within object, class or trait definition
Sorry, I need to be more clear about this and describe it in more detail in my second update.
I looked in Scala spec. Seems that I'm not totally correct when I say that myInnerFn is syntactic sugar for the function. but seems that it's called Method Type. You can find it in spec section 3.3.1 Method Type:
http://www.scala-lang.org/docu/files/ScalaReference.pdf
hope it will give you some clue, if you want to dive deeper in this. I think it's easy to get lost in terminology. You can function in 2 contexts. In first we have
Function - returns some value
Procedure - returns no value (or in Scala context it returns Unit)
And in second context:
Function - executable piece of code that can be passes around and treated as value
Method - belongs to the class
And it's sometimes not clear in what context it meant. For example I can tell you that myMethod is as function just because it has return value (or in other words: myMethod it's not procedure). I believe it's the same case in book.
One more thing. Sections 8.1, 8.2, 8.3 from Programming in Scala can help you to understand this terminology. And if I'm correct in my assumptions, what you think as Function is called First-class function in the book (it's described in section 8.3).
Hope this helps