码迷,mamicode.com
首页 > 其他好文 > 详细

Scala基础入门-代码碎片

时间:2015-11-08 22:26:52      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

import scala.util.control._
import java.util.Date
object Test {
   def main(args: Array[String]) {
      // var a = 10;
      // var b = 20;
      // var c = 25;
      // var d = 25;
      // println("a + b = " + (a + b) );
      // println("a - b = " + (a - b) );
      // println("a * b = " + (a * b) );
      // println("b / a = " + (b / a) );
      // println("b % a = " + (b % a) );
      // println("c % a = " + (c % a) );
      // var a = 10
      // a=a - 10;
      // if(a > 5){
      //    println("go to the test  area....")
      
      // }else{
      //    println("not go to the test area"+a)
      // }

      // Local variable declaration:
      //var a = 10;

      // while loop execution
      // while( a < 20 ){
      //    println( "Value of a: " + a );
      //    a = a + 1;
      // }

      //  do{
      //    println( "Value of a: " + a );
      //    a = a + 1;
      // }while( a < 20 )


      // for( a <- 1 until 10){
      //    println( "Value of a: " + a );
      // }


      //val numList = List(true,false,true);

      // for loop execution with a collection
      // for( a <- numList ){
      //    println( "Value of a: " + a );
      // }


      var a = 0;
      val numList = List(1,2,3,4,5,6,7,8,9,10);
      // for loop execution with a yield
      // var retVal = for{ a <- numList ;if a != 3; if a < 8
      //                 }yield a

      // Now print returned values using another loop.
      // for( x <- retVal){
      //    println( "Value of x: " + x );
      // }


      //函数可以随便定义位置,可以在main方法里面,亦或外面都可以执行
      // val loop = new Breaks;
      // loop.breakable {
      //    for( a <- numList){
      //       println( "Value of a: " + a );
      //       if( a == 4 ){
      //          loop.break;
      //       }
      //    }
      // }
      // println( "After the loop" );

   //    def addInt( a:Int, b:Int ) : Int = {
   //    var sum:Int = 0
   //    sum = a + b

   //    return sum
   // }


   //  def factorial(n: BigInt): BigInt = {  
   //    if (n <= 1)
   //       1  
   //    else    
   //    n * factorial(n - 1)
   // }

   // PrintUtil("this is the function result:"+addInt(1,10));

   // for (i <- 1 to 10)
   //       PrintUtil( "Factorial of " + i + ": = " + factorial(i) )


   // def apply(f: Int => String, v: Int) = f(v)

   // def layout[A](x: A) = "[" + x.toString() + "]"


   // println( apply( layout, 10) )


   // var userDir = () => { System.getProperty("user.dir") }

   // println( userDir )

    val date = new Date
      val logWithDateBound = log(date, _ : String)

      logWithDateBound("message1" )
      Thread.sleep(1000)
      logWithDateBound("message2" )
      Thread.sleep(1000)
      logWithDateBound("message3" )

      var dates = new Date
      log(dates, "message1hh" )
      Thread.sleep(1000)
      log(dates, "message2hh" )
      Thread.sleep(1000)
      log(dates, "message3hh" )

      val fruit = "apples" :: ("oranges" :: ("pears" :: Nil))
      val nums = Nil

      println( "Head of fruit : " + fruit.head )
      println( "Tail of fruit : " + fruit.tail )
      println( "Check if fruit is empty : " + fruit.isEmpty )
      println( "Check if nums is empty : " + nums.isEmpty )
      
   }

    def log(date: Date, message: String)  = {
       val datebak = new Date
     println(datebak + "----" + message)
   }

   def PrintUtil(strings : String) : Unit = {
      println("======================top======================");
      println(strings);
      println("======================bottom======================"); 
   }
}

  

Scala基础入门-代码碎片

标签:

原文地址:http://www.cnblogs.com/spring87/p/4948339.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!