Skip to content

Instantly share code, notes, and snippets.

View yingy4's full-sized avatar

Yuan Ying yingy4

  • Shanghai
View GitHub Profile
@yingy4
yingy4 / Scala Case class, Companion object, Apply method and more.md
Last active February 10, 2020 08:29
Scala: Case class, Companion object, Apply method and more

Scala Case class, Companion object, Apply method and more

Let’s take a look at some examples:

Example1:

object Tests extends App {
  println(Rating("PG",13))
  println(Rating.apply("PG",13)) //This is equivalent, so you don't need to explicitly call apply
}
@yingy4
yingy4 / Scala Syntactic Sugar and Desugar in IntelliJ IDEA.md
Created February 7, 2018 20:19
Scala Syntactic Sugar and Desugar in IntelliJ IDEA

Scala Syntactic Sugar and Desugar in IntelliJ IDEA

Syntactic sugar is just a way that we re-write our code to a simple and human-readable form, it does not change any function of a method.

IntelliJ IDEA offers a Desugar Scala Code function, you can use that to discover what this syntactic sugar actually stands for.

Here is an example:

def lift[T1,T2](f: T1 => T2):Try[T1]=>Try[T2] = _ map f
@yingy4
yingy4 / Scala A More General Sum Using Tail Recursion.md
Last active February 8, 2018 04:01
Scala A More General Sum Using Tail Recursion

Scala A More General Sum Using Tail Recursion

My solution is:

  def mySum[T: Numeric](l: List[T]): T = {
    val s = implicitly[Numeric[T]].zero

    @tailrec
    def inner[T: Numeric](l: List[T], s: T): T = l match {
 case Nil => s