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
class Content | |
def type | |
self.class.name | |
end | |
end | |
class TwitterContent | |
def type | |
"twitter" | |
end |
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
var str = 'allleee #psG !! tudu'; | |
var regex = /#(\w+)/; | |
str = str.replace(regex, "<b>#$1</b>"); |
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.util.Random.nextInt | |
def choosePivot(xs: List[Int]): Int = { | |
if (xs.isEmpty) 0 | |
else xs(nextInt(xs.length)) | |
} | |
def quicksort(xs: List[Int], p: Int): List[Int] = { | |
if (xs.isEmpty) List() | |
else if (xs.length == 1) xs |
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.annotation.tailrec | |
def sum(f: Int => Int)(a: Int, b: Int) : Int = { | |
@tailrec | |
def iter(a: Int, result: Int) : Int = { | |
if (a > b) result | |
else iter(a + 1, f(a) + result) | |
} | |
iter(a, 0) | |
} |
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.annotation.tailrec | |
def product(f: Int => Int)(a: Int, b: Int) : Int = { | |
@tailrec | |
def iter(a: Int, result: Int) : Int = { | |
if (a > b) result | |
else iter(a + 1, f(a) * result) | |
} | |
iter(a, 0) | |
} |
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.annotation.tailrec | |
def compute(op: (Int, Int) => Int)(f: Int => Int)(a: Int, b: Int) : Int = { | |
@tailrec | |
def iter(a: Int, result: Int) : Int = { | |
if (a > b) result | |
else iter(a + 1, op(f(a), result)) | |
} | |
iter(a + 1, f(a)) | |
} |
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
abstract class IntSet { | |
def isEmpty: Boolean | |
def contains(x: Int): Boolean | |
def include(x: Int): IntSet | |
def union(other: IntSet): IntSet | |
} | |
class EmptySet extends IntSet { | |
def isEmpty = true | |
def contains(x: Int): Boolean = false |
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
trait List[T] { | |
def isEmpty: Boolean | |
def head: T | |
def tail: List[T] | |
} | |
class Cons[T](val head: T, val tail: List[T]) extends List[T] { | |
def isEmpty: Boolean = false | |
} |
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 List { | |
// List() | |
def apply[T]() = new Nil | |
// List(1) | |
def apply[T](x1: T): List[T] = new Cons(x1, new Nil) | |
// List(1, 2) | |
def apply[T](x1: T, x2: T): List[T] = new Cons(x1, new Cons(x2, new Nil)) | |
} |
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
// Peano numbers | |
abstract class Nat { | |
def isZero: Boolean | |
def predecessor: Nat | |
def successor: Nat = new Succ(this) | |
def + (that: Nat): Nat | |
def - (that: Nat): Nat | |
} |
OlderNewer