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.
  1. scala> class MyClass {             
  2.      | var a:Int = _    // 1
  3.      | def countDown = 10 to 5 by -1
  4.      | }
  5. defined class MyClass
  6. scala> val obj = new MyClass()
  7. obj: MyClass = MyClass@6ff0239
  8. scala> val countDownMethod = obj.countDown _  // 2
  9. countDownMethod: () => Range = < function>
  10. scala> def multiple(a:Int)(b:Int) = a*b
  11. multiple: (Int)(Int)Int
  12. scala> val triple = multiple(3) _ // 3
  13. triple: (Int) => Int = < function>
  14. scala> List(1,2,3) foreach { _ => Console.println("Hello") } // 4
  15. Hello
  16. Hello
  17. Hello

  1. 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
  2. create a reference to the method (rather than invoking the method and having a reference to the resulting value) (Thanks Daniel)
  3. This is an example of currying; a new function is created with a single parameter.
  4. where the underscore is used as an ignored and unnamed parameter (Thanks Alex)

Wednesday, December 2, 2009

What the @*!% is with the '_'

  1. scala> import java.io._ // 1
  2. import java.io._
  3. scala> import java.io.{ File => _ } // 2
  4. import java.io.{File=>_}
  5. scala> object MyObj{ def count=(1 to 10) }
  6. defined module MyObj
  7. scala> import MyObj._ // 3
  8. import MyObj._
  9. scala> class MyClass { def countDown= 10 to 5 by -1} 
  10. defined class MyClass
  11. scala> val instance = new MyClass()
  12. instance: MyClass = MyClass@69ebcd0
  13. scala> import instance._ // 4
  14. import instance._
  15. scala> def multiply(by:Int,x:Int)=2*x
  16. multiply: (Int,Int)Int
  17. scala> val double=multiply(2, _:Int// 5 
  18. double: (Int) => Int 
  19. scala> count foreach {i => double(i)}
  20. scala> val double2:Int=>Int = multiply(2,_)
  21. double2: (Int) => Int = & function>
  22. scala> count foreach {i => double2(i)}
  23. scala> count reduceLeft {_+_} // 6
  24. res3: Int = 55
  25. class Generic[T](val t:T)
  26. 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:
  1. import anything/everything in package
  2. this seems to be an exception. Assign File to oblivion. (It is no longer imported)
  3. import all methods in object
  4. same thing. Import all methods in instance object
  5. 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
  6. 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.
  7. 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]