Git Product home page Git Product logo

scala's People

scala's Issues

总结的一些快速熟练技巧

object GaiNian {

def main(args: Array[String]): Unit = {

//1. 多参数列表-> 推断参数类型  -> 函数作为参数Function -> 使用偏函数
def m2[A](a:A)(f:A=>String)=f(a)
println(m2(100)(i=>s"$i+$i"))

//2.
def joiner(string:String*) = string.mkString(" ")
def joiner2(list:List[String]) = joiner(list:_*)
1::List(0)
1+:Seq(0)

//3. case类匹配
for(person <- Seq(Person("name",1,1))){
  person match {
    case Person(name,_,_) => println(name)
    case p @ Person(name,age,_) => println(p)    //变量绑定
    case _=> println()
  }
}

//注意:  集合的类型擦除
for(tp <- Seq(Seq(0),Seq("0"))){
  tp match {
    case head+:tail => head match {
      case _:Int => println("int")
      case _:String => println("string")
    }
  }
}


//4. implicit  隐式转换 相当于自动执行
//implicit val string2Int:String=>Int = (s:String)=> s.toInt

implicit class GetPerson(person:Person){
  def get[T](field:String)(implicit toT:(Person,String)=>T):T= toT(person,field)
}

implicit val field2String: (Person, String) => Any = (person:Person, field:String) => field match {
  case n @ name if n=="name" => person.name
  case n @ name if n=="age" => person.age
}
//implicit的作用域在这里
val field = new GetPerson(Person("name2",2,2))
println(field.get("age"))


//5. for循环使用
val list = List("asdSSD","DDwwA")
 for{
  s <- list
  c <- s
  if c.isLower
  c2 = s"$c+${c.toUpper}"
}yield c2

list.flatMap(_.toSeq.withFilter(_.isLower).map(_.toUpper))

for{
  Some(i) <- list.map(Option(_))withFilter{
    case Some(i) => true
    case None => false
  }
}yield i


//6.
//引用类型  AnyRef                  值类型  AnyVal
//协变 +T    ? extends T
//逆变 -T    ? super T

//元编程  是用来操控程序 而非代码, 例如反射, 就是一种自省
val clazz: Class[_ <: Person.type] = Person.getClass
val clazz2: Class[Person] = classOf[Person]

//ClassTag 类标签  用于保留集合的类型,  构建数组或集合
def mkArray[T:ClassTag](elem:T*)=Array[T](elem:_*)

}

case class Person(name:String,age:Int,id:Int)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.