I can't see where is the syntax problem here :
x = ()->new TranformService()
angular.module('rcMovable').factory ( "transformService", x)
When there is no problem there
x = ()->new TranformService()
angular.module('rcMovable').factory "transformService", x
Forget about Angular stuff for the moment, I have :2:58: error: unexpected ','
You cannot put a space between a method name and the parenthesis used to invoke it. If you do, the parenthesis are no longer part of the function invocation, they're for order-of-operations on the arguments to the function.
Consider a few examples of valid and invalid syntax:
x() is a valid invocation with no arguments
x () is an invalid invocation of x with one argument: (). Because () is not a valid expression, this is a syntax error.
x (name: "bob") or x (->3 * 3), 4 are both valid invocations, because the (...) is a valid expression
x(a, b) is again a valid invocation of x with two arguments
x (a, b) is an invalid invocation of x with one argument: (a, b). Again, (a, b) is not itself a valid expression, so this is a syntax error.
In your case, you're trying to invoke .factory with one argument: ("transformService", x), which is not a valid expression.
What you've written is essentially this:
a = ("transformSerice", x) # invalid syntax
factory(a)
Related
Folding list in scala using /: and :\ operator
I tried to to look at different sites and they only talk about foldRight and foldLeft functions.
def sum(xs: List[Int]): Int = (0 /: xs) (_ + _)
sum(List(1,2,3))
res0: 6
The code segment works as described. But I am not able to completely understand the method definition. What I understand is that the one inside the first parenthesis -> 0 /: xs where /: is a right associate operator. The object is xs and the parameter is 0. I am not sure about the return type of the operation (most probably it would be another list?). The second part is a functional piece which sums its two parameters. But I don't understand what object invokes it ? and the name of function. Can someone please help me to understand.
The signature of :/ is
/:[B](z: B)(op: (B, A) ⇒ B): B
It is a method with multiple argument lists, so when it is just invoked with on argument (i.e. 0 /: xs in your case) the return type is (op: (B, A) ⇒ B): B. So you have to pass it a method with 2 parameters ( _ + _ ) that is used to combine the elements of the list starting from z.
This method is usually called foldLeft:
(0 /: xs)(_ + _) is the same as xs.foldLeft(0)(_ + _)
You can find more details here: https://www.scala-lang.org/api/2.12.3/scala/collection/immutable/List.html
Thanks #HaraldGliebe & #LuisMiguelMejíaSuárez for your great responses. I am enlightened now!. I am just summarisig the answer here which may benefit others who read this thread.
"/:" is actually the name of the function which is defined inside the List class. The signature of the function is: /:[B](z: B)(op: (B, A) ⇒ B): B --> where B is the type parameter, z is the first parameter; op is the second parameter which is of functional type.
The function follows curried version --> which means we can pass less number of parameters than that of the actual number. If we do that,
the partially applied function is stored in a temporary variable; we can then use the temporary variable to pass the remaining parameters.
If supplied with all parameters, "/:" can be called as: x./:(0)(_+_) where x is val/var of List type. OR "/:" can be called in two steps which are given as:
step:1 val temp = x./:(0)(_) where we pass only the first parameter. This results in a partially applied function which is stored in the temp variable.
step:2 temp(_+_) here using the partially applied function temp is passed with the second (final) parameter.
If we decide to follow the first style ( x./:(0)(_+_) ), calling the first parameter can be written in operator notion which is: x /: 0
Since the method name ends with a colon, the object will be pulled from right side. So x /: 0 is invalid and it has to be written as 0 /: x which is correct.
This one is equivalent to the temp variable. On following 0 /: x, second parameter also needs to be passed. So the whole construct becomes: (0/:x)(_+_)
This is how the definition of the function sum in the question, is interpreted.
We have to note that when we use curried version of the function in operator notion, we have to supply all the parameters in a single go.
That is: (0 /: x) (_) OR (0 /: x) _ seems throwing syntax errors.
Hi I am new in scala, and of course have a newbie question,
why the underscore character not work in this cases
my undertanding about underscore in this case it represent one element of the list
val listOftuple = List(("cat","tom"),("mouse","jerry"),("dog","spike"))
val listOfarray = List(Array("cat","tom"),Array("mouse","jerry"),Array("dog","spike"))
tuplelist.map(it=>it._1+it._2) //Ok
tuplelist.map(_._1+_._2) //Not work, give me: error: missing parameter type for expanded function
listOfarray.map(_.length) //Here work, like array.length
listOfarray.map(it=>it(0)+it(1)) //Ok
listOfarray.map(_=>_(0)+_(1))//Not work, give me: error: missing parameter type for expanded function
listOfarray.map(_(0)+_(1))//Not work, give me: error: missing parameter type for expanded function
may be i fall in a lexical misunderstanding ?
how to fix the error msg?
greetings
tuplelist.map(_._1+_._2)
Each time you use _, it stands for another parameter. That is, _ + _ is equivalent to (x,y) => x+y, not x => x+x. So the above is equivalent to tuplelist.map((x, y) => _._1 + _._2), which does not work because map expects a function that takes only one argument.
listOfarray.map(_=>_(0)+_(1))
Using _ as the name of a function parameter just means that the parameter will be ignored. When you use _ inside the function's body, it is still the function shortcut operator, not a reference to the ignored parameter. So the above lambda is equivalent to ignoredParam => (x, y) => x + y.
listOfarray.map(_(0)+_(1))
(x,y) => x(0) + y(1), but you want x => x(0) + x(1).
how to fix the error msg?
In all of these cases, using an explicit parameter list is the correct solution. You simply can't use the shortcut notation if you want to use the same parameter more than once.
In Scala, if you have an expression containing an underscore, this is an anonymous function with the expression as its body and the underscore as as its parameter, e.g. 2*_ is the anonymous function that doubles its argument. But how far does the function body extend? I'm missing a clear rule here that disambiguates cases like e.g. the following (tested with the Scala 2.11.7 REPL):
scala> (_: Int)+2-1 // function body up to 1 - OK
res7: Int => Int = <function1>
scala> ((_: Int)+2)-1 // function body up to 2, - applied to function is an error
<console>:11: error: value - is not a member of Int => Int
((_: Int)+2)-1
^
The definition is given in http://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#placeholder-syntax-for-anonymous-functions, and it's... not that simple.
An expression e of syntactic category Expr binds an underscore section u, if the following two conditions hold: (1) e properly contains u, and (2) there is no other expression of syntactic category Expr which is properly contained in e and which itself properly contains u.
If an expression e binds underscore sections u_1 , \ldots , u_n, in this order, it is equivalent to the anonymous function (u'_1, ... u'_n) => e' where each u_i' results from u_i by replacing the underscore with a fresh identifier and e' results from e by replacing each underscore section u_i by u_i'.
And if you look at the grammar in the beginning of the section, (_: Int)+2 in (_: Int)+2-1 is not an Expr, but in ((_: Int)+2)-1 it is.
((_: Int)+2)-1 // function body up to 2, - applied to function is an error
error: value - is not a member of Int => Int
((_: Int)+2)-1
The error message from the compiler is sensible. Your additional parens have created a function literal that adds '2' to a wildcard/placeholder parameter. The compiler reads your code to mean that you have a this function value and you are trying to subtract '1' from it.
And this doesn't make sense. You can subract '1' from other numbers, but certainly not a function value. Thus, the compiler is telling you that it doesn't make sense to subtract one from a function value. Or, in compiler terms, a function of type Int => Int doesn't have a '-' function.
value - is not a member of Int => Int
Understanding this error message requires that you know that all operators in Scala ( -, *, +, etc ) are implemented as methods of types. If you look at the Scala API docs for Int, you'll see that it defines a long list of methods with common mathematical and logical operator symbols as function names.
1 to 5 foldRight (0)(x:Int, y:Int => x+y)
I am trying to add all the values from right to left with 0 as the initial parameter. I get the following error
Int(0) does not take parameters
Can anyone explain to me what this error even means?
Thanks.
It's just the parser getting "confused", so it's trying to apply (x: Int, y: Int ...) as argument of (0).
Specifically, what you're using is a scala syntactic feature that allows to use
a.f(b)
as
a f b
This works with any method that has a single parameter. However when your method has multiple parameter lists (like foldRight), you have to use extra care.
This is what the parser sees
1 to 5 foldRight (0)(x: Int, y: Int => x + y)
|__a__| |___f___| |____________b_____________|
So when evaluating b, it treats 0 as a function with (x: Int, ...) as an argument.
Clearly this can't work, because "Int(0) does not take parameters".
Instead, you can use
(1 to 5).foldRight(0)((x,y) => x + y)
or even
(1 to 5).foldRight(0)(_ + _)
There are two things wrong with your code. The first is that you need to use brackets around 1 to 5, the second is the syntax of your anonymous function. You need brackets around the parameters there.
(1 to 5).foldRight(0)((x,y) => x + y)
Can anyone please explain me why I can do:
a.mapValues(_.size)
instead of
a.mapValues(x => x.size)
but I can't do
a.groupBy(_)
instead of a
a.groupBy(x => x)
When you write a.groupBy(_) the compiler understands it as an anonymous function:
x => a.groupBy(x)
According to Scala Specifications §6.23, an underscore placeholder in an expression is replaced by a anonymous parameter. So:
_ + 1 is expanded to x => x + 1
f(_) is expanded to x => f(x)
_ is not expanded by itself (the placeholder is not part of any expression).
The expression x => a.groupBy(x) will confuse the compiler because it cannot infer the type of x. If a is some collection of type E elements, then the compiler expects x to be a function of type (E) => K, but type K cannot be inferred...
It isn't easy to see it here:
a.groupBy(_)
But it's easier to see it in something like this:
a.mkString("<", _, ">")
I'm partially applying the method/function. I'm applying it to some parameters (the first and last), and leaving the second parameter unapplied, so I'm getting a new function like this:
x => a.mkString("<", x, ">")
The first example is just a special case where the sole parameter is partially applied. When you use underscore on an expression, however, it stands for positional parameters in an anonymous function.
a.mapValues(_.size)
a.mapValues(x => x.size)
It is easy to get confused, because they both result in an anonymous function. In fact, there's a third underscore that is used to convert a method into a method value (which is also an anonymous function), such as:
a.groupBy _