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
// Simplest way to create a function | |
def f1(value: Int) = 4 * value //> f1: (value: Int)Int | |
// And call | |
f1(4) //> res0: Int = 16 | |
// and save it in a val | |
val valuef1 = f1 _ //> valuef1 : Int => Int = <function1> | |
//and call it from the val |
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
// A lift function that takes a regular function that cannot handle options | |
// and uses for to call it only with Some(...) cases | |
def lift1[A, B](f: Function1[A, B]): Function1[Option[A], Option[B]] = { | |
(oa: Option[A]) => | |
for (a <- oa) yield f(a) | |
} //> lift1: [A, B](f: A => B)Option[A] => Option[B] | |
// Same for a 2 argument function, if any of the arguments is None, the function simply returns None | |
def lift2[A, B, C, D](f: Function2[A, B, C]): Function2[Option[A], Option[B], Option[C]] = { | |
(oa: Option[A], ob: Option[B]) => |
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
// Full definition by defining the abstract methods | |
val pf1 = new PartialFunction[Int, Int]{ | |
def apply(x: Int): Int = x match { case t if isDefinedAt(t) => t + 1 } | |
def isDefinedAt(x: Int): Boolean = x > 0 && x < 10 | |
} //> pf1 : PartialFunction[Int,Int] = <function1> | |
pf1.isDefinedAt(20) //> res0: Boolean = false | |
//pf1(20) //scala.MatchError | |
//pf1.apply(20) // same as above using aply method | |
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
val positivePF: PartialFunction[Int, String] = { case i if i > 0 => "Positive"} | |
//> positivePF : PartialFunction[Int,String] = <function1> | |
val negativePF: PartialFunction[Int, String] = { case i if i < 0 => "Negative"} | |
//> negativePF : PartialFunction[Int,String] = <function1> | |
val zeroPF: PartialFunction[Int, String] = { case 0 => "Zero"} | |
//> zeroPF : PartialFunction[Int,String] = <function1> | |
// combine the indivitual partial functions to form one that covers all of them |
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
// Start with a typed trait, which will form a small "concept" | |
trait Concept[T]{ | |
def describe : String | |
} | |
// Then the idea is to create models implementing this concept | |
// for the various types we want to use it | |
implicit class IntModel(n: Int) extends Concept[Int]{ | |
def describe : String = "The integer " + n | |
} |
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
// Start with a typed trait, which will form a small "concept" | |
trait Concept[T]{ | |
def describe : String | |
} | |
// Then the idea is to create models implementing this concept | |
// for the various types we want to use it | |
implicit class IntModel(n: Int) extends Concept[Int]{ | |
def describe : String = "The integer " + n | |
} |
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 org.example | |
import scala.annotation.tailrec | |
// Made it a case class so you get the extras with no code | |
// changed the names to conform with the scala style | |
case class TreeNode[T](value: T, leftChild: Option[TreeNode[T]], rightChild: Option[TreeNode[T]]) { | |
// Simple recursive traversal using pattern matching | |
def mkString(sep: String = ", "): String = this match { |
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
@tailrec | |
final def breathFirstSearch(queue: List[(State, List[Move])], explored: Set[State], sol: List[(State, List[Move])]): List[(State, List[Move])] = | |
queue match { | |
case Nil => sol | |
case (state, history) :: xs => | |
if (!explored(state)) { | |
breathFirstSearch(xs ++ Move.availableStates(state, moves, history, explored), explored + state, (state, history) :: sol) | |
} else { | |
breathFirstSearch(xs, explored, sol) | |
} |
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
@tailrec | |
final def aStarSearch(queue: PriorityQueue[(State, List[Move])], explored: Set[State]): Option[List[Move]] = | |
if (queue.length == 0) return None | |
else { | |
val (state, history) = queue.dequeue | |
if (state == goalState) Some(history) | |
else { | |
if (!explored(state)) { | |
val children = Move.availableStates(state, moves, history, explored) | |
queue ++= children |
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 org.example | |
import scala.annotation.tailrec | |
import scala.collection.mutable.PriorityQueue | |
import scala.concurrent.duration.Duration | |
/** | |
* Represents the state of the puzzle with a vector of integers | |
*/ | |
case class State(board: Vector[Int]) extends Equals { |