Showing posts with label underscore. Show all posts
Showing posts with label underscore. Show all posts
Friday, December 4, 2009
Yet more instances of '_'
A few more placeholder instances that I have remembered or was reminded of.
- scala> class MyClass {
- | var a:Int = _ // 1
- | def countDown = 10 to 5 by -1
- | }
- defined class MyClass
- scala> val obj = new MyClass()
- obj: MyClass = MyClass@6ff0239
- scala> val countDownMethod = obj.countDown _ // 2
- countDownMethod: () => Range = < function>
- scala> def multiple(a:Int)(b:Int) = a*b
- multiple: (Int)(Int)Int
- scala> val triple = multiple(3) _ // 3
- triple: (Int) => Int = < function>
- scala> List(1,2,3) foreach { _ => Console.println("Hello") } // 4
- Hello
- Hello
- Hello
- initialize a variable to its default value. If the = _ is left out then the definition will be an abstract declaration and will have to be defined in subclasses
- create a reference to the method (rather than invoking the method and having a reference to the resulting value) (Thanks Daniel)
- This is an example of currying; a new function is created with a single parameter.
- where the underscore is used as an ignored and unnamed parameter (Thanks Alex)
Labels:
intermediate,
Scala,
underscore
Wednesday, December 2, 2009
What the @*!% is with the '_'
- scala> import java.io._ // 1
- import java.io._
- scala> import java.io.{ File => _ } // 2
- import java.io.{File=>_}
- scala> object MyObj{ def count=(1 to 10) }
- defined module MyObj
- scala> import MyObj._ // 3
- import MyObj._
- scala> class MyClass { def countDown= 10 to 5 by -1}
- defined class MyClass
- scala> val instance = new MyClass()
- instance: MyClass = MyClass@69ebcd0
- scala> import instance._ // 4
- import instance._
- scala> def multiply(by:Int,x:Int)=2*x
- multiply: (Int,Int)Int
- scala> val double=multiply(2, _:Int) // 5
- double: (Int) => Int
- scala> count foreach {i => double(i)}
- scala> val double2:Int=>Int = multiply(2,_)
- double2: (Int) => Int = & function>
- scala> count foreach {i => double2(i)}
- scala> count reduceLeft {_+_} // 6
- res3: Int = 55
- class Generic[T](val t:T)
- val generic:Generic[_] = new Generic(2) // 7
While at a glance the underscores do not seem related, in fact the general rule is fill in the blank. Sca_la what goes in the blank?
Going through the examples:
- import anything/everything in package
- this seems to be an exception. Assign File to oblivion. (It is no longer imported)
- import all methods in object
- same thing. Import all methods in instance object
- creat a new method where the first parameter is defined but all others will be defined later. In this case a special syntax is required so the compiler knows that a new method is desired and the missing parameters was not simply a mistake. There is another syntax for defining methods that do not require the _ for currying
- reduceLeft takes method with 2 parameters.
_ + _
creates a function of 2 parameters: the first '_' represents the first parameter and the second '_' represents the second parameter. - the '_' represents any value. This syntax is frowned upon since Scala has such a rich type system but can be useful when interoperating with Java. Perhaps a better declaration would be
Generic[Any]
Labels:
import,
Scala,
underscore
Subscribe to:
Comments (Atom)