This file contains hidden or 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
sealed trait Sequence[+T] | |
case class Cons[+T](head: T, tail: Sequence[T]) extends Sequence[T] | |
case object Empty extends Sequence[Nothing] | |
object Main { | |
def last2[T](list: Sequence[T]): Sequence[T] = list match { | |
case result@Cons(_, Cons(_, Empty)) => result | |
case Empty => | |
sys.error("list should not be empty") | |
case Cons(_, Empty) => |
This file contains hidden or 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
table { | |
row { | |
cell("A") | |
cell("B") | |
cell("C") | |
} | |
row { | |
cell("1") | |
cell("2") | |
} |
This file contains hidden or 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
fn test() -> Box<MFunBox<i32, Box<Vec<i32>>>> { | |
let mut v = Box::new(vec![1, 2, 3]); | |
return Box::new(move |x: i32|{ v.push(x); v}); | |
} | |
trait MFunBox<A, B> { | |
fn mcall(self: Box<Self>, arg: A) -> B; | |
} | |
impl<A, B, T:FnOnce(A) -> B> MFunBox<A, B> for T { |
This file contains hidden or 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 Text(body: String) | |
object Text { | |
def pack(body: String): Text = Text(body) | |
} | |
trait Show[A] { | |
def show(a: A): String | |
} |
This file contains hidden or 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 G { | |
def main(args: Array[String]): Unit = { | |
println( | |
List(1, 2, 3) | |
.map{x => x * 2} | |
.filter{x => x > 2} | |
) | |
} | |
} |
This file contains hidden or 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 add(x: Int, y: Int): Int = { | |
x + y | |
} |
This file contains hidden or 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
fun main(args: Array<String>) { | |
val message: String? = null | |
println(message?.length) | |
} |
This file contains hidden or 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
‘try’ (‘{’ Block ‘}’ | Expr) [‘catch’ ‘{’ CaseClauses ‘}’] [‘finally’ Expr] |
This file contains hidden or 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 when[A](cond: Boolean)(th: => A)(el: => A): A = if(cond) th else el | |
var i = 1 | |
when(i < 2) { | |
println("i < 2") | |
} { | |
println("i >= 2") | |
} | |
// i < 2 |
This file contains hidden or 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
class Foo { | |
var myList: List<String>? = null | |
fun hoge(list: List<String>) { | |
myList = list | |
} | |
} |