This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class User(username:String, password: String) | |
trait UserRepositoryComponent { | |
val userRepository: UserRepository | |
class UserRepository { | |
def authenticate(user: User): User = { | |
println("authenticating user: " + user) | |
user | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class User(username:String, password: String) | |
class UserRepository { | |
def authenticate(user: User): User = { | |
println("authenticating user: " + user) | |
user | |
} | |
def create(user: User) = println("creating user: " + user) | |
def delete(user: User) = println("deleting user: " + user) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# one way (older scala version will be installed) | |
# sudo apt-get install scala | |
#2nd way | |
sudo apt-get remove scala-library scala | |
wget http://www.scala-lang.org/files/archive/scala-2.11.1.deb | |
sudo dpkg -i scala-2.11.1.deb | |
sudo apt-get update |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// The method `hello` in trait `A` and `B` are exactly same | |
// | |
object SameMethodInDifferentTraits { | |
trait A { | |
def hello() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object TypesOfCompanion { | |
class User | |
object User | |
def helloClass(user: User) = println(user) | |
def helloObject(user: User.type) = println(user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int led = 13; | |
int x = 4; | |
void setup() { | |
pinMode(led, OUTPUT); | |
pinMode(x, INPUT); | |
} | |
void loop() { | |
int sta = digitalRead(x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// curry的一个意义是: 方便scala根据第一组参数的内容,推断后面的类型 | |
// scala采用基于流的局部推断,只会从第一组参数中获取类型信息,再用到其它组参数里 | |
// 下面只有sort2不用写Int类型 | |
def sort[T](f: (T,T)=>Boolean)(list:List[T]):List[T] = list | |
sort[Int]((x,y)=>true)(List(1,2,3)) | |
def sort2[T](list:List[T])(f: (T,T)=>Boolean):List[T] = list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// curry的一个意义是: 方便scala根据第一组参数的内容,推断后面的类型 | |
// scala采用基于流的局部推断,只会从第一组参数中获取类型信息,再用到其它组参数里 | |
// 下面只有sort2不用写Int类型 | |
def sort[T](f: (T,T)=>Boolean)(list:List[T]):List[T] = list | |
sort[Int]((x,y)=>true)(List(1,2,3)) | |
def sort2[T](list:List[T])(f: (T,T)=>Boolean):List[T] = list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def double(number:Int) = 2*number | |
def add10(number:Int) = 10 + number | |
def negative(number:Int) = -1 * number | |
def myconvert(n:Int) = Some(n).map(double).map(add10).map(negative).get | |
myconvert(5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.io.Source.stdin | |
object App { | |
def main(args: Array[String]) { | |
stdin.getLines foreach { line => | |
println(myconvert(line.toInt)) | |
} | |
} | |
val double = (number:Int) => 2*number |
OlderNewer