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
| trait Invert extends AddOperation { | |
| override abstract def add(a: Int, b: Int): Int = super.add(a * -1, b * -1) | |
| } | |
| trait MultDiv extends AddOperation { | |
| override abstract def add(a: Int, b: Int): Int = super.add(a * 2, b / 2) | |
| } | |
| trait Inc2Dec extends AddOperation { |
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 cyclic(players: List[Player], teams: List[Team], pick: Int = 1): List[Team] = { | |
| if(players.isEmpty) { | |
| teams | |
| } else { | |
| val team = teams.head | |
| val player = choosePlayer(players) | |
| team.copy(players = team.players + player) | |
| val movedLast = teams.tail :+ team | |
| if(pick % numberOfTeams == 0) { | |
| cyclic(players.filterNot(_ == player), movedLast.reverse, pick + 1) |
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 Test1 { | |
| val fib: Any => Stream[Int] = { | |
| case () => fib(1) | |
| case (a: Int) => fib(0, a) | |
| case (a: Int, b: Int) => Stream.cons(a, fib(b, a+ b)) | |
| } | |
| val greet: Any => Unit = { | |
| case () => println("Hello, world.") | |
| case (name: String) => println("Hello, " + name + ".") |
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
| (defn foo | |
| ([a b] (println "bar")) | |
| ([a b c] (println "foobar"))) | |
| ;#'user/foo | |
| (foo 1 2) | |
| ;bar | |
| ;nil | |
| (foo 1 2 3) | |
| ;foobar | |
| ;nil |
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
| import java.util.ArrayList; | |
| import java.util.List; | |
| public class EmptyList { | |
| //We can try and define a constant for empty List in Java too... | |
| //But we have a problem. What type should the list be? | |
| //We don't want to create a constant for each kind of empty list. | |
| //The only choice seems to be Object... that can't be a good sign. | |
| public static final List<Object> el = new ArrayList<Object>(); |
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 P26 extends App { | |
| def combinations[T](n: Int, l: List[T]): List[List[T]] = { | |
| if(l.size < n) { | |
| Nil | |
| } else { | |
| n match { | |
| case 0 => Nil | |
| case 1 => l.map{List(_)} | |
| case _ => combinations(n-1,l.tail).map{ l.head :: _} ::: combinations(n, l.tail) | |
| } |
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
| abstract class Tree[A] | |
| case class Leaf[A](a: A) extends Tree[A] | |
| case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A] | |
| object TreeTest extends App { | |
| val t1 = Branch(Branch(Leaf(1), Leaf(2)), Leaf(3)) | |
| println(t1) | |
| } |
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
| package problems | |
| trait Comparable[T] { | |
| def compareTo(t: T): Int | |
| } | |
| case class ComparableInt(val a: Int) extends Comparable[ComparableInt] { | |
| override def compareTo(b: ComparableInt): Int = { | |
| if(a < b.a) { | |
| -1 |
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 Program { | |
| def run(): Unit = println("I pass a Turing test.") | |
| } | |
| class Person { | |
| def run(): Unit = println("But I don't.") | |
| } | |
| class Engine { | |
| def run(): String = "I run in a totally different way" |
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
| abstract class Tree[+A] | |
| case class Node[A](l: Tree[A], e: A, r: Tree[A]) extends Tree[A] | |
| case object Tip extends Tree[Nothing] | |
| abstract class MONOID { | |
| type t | |
| val append: t => t => t | |
| val identity: t | |
| } | |