Skip to content

Instantly share code, notes, and snippets.

View sofoklis's full-sized avatar

Sofoklis Papasofokli sofoklis

View GitHub Profile
@sofoklis
sofoklis / firstclassfunctions.scala
Created January 27, 2013 17:46
firstclassfunctions.scala
// 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
@sofoklis
sofoklis / liftedOptions.scala
Last active December 11, 2015 19:38
lifted options
// 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]) =>
@sofoklis
sofoklis / partialfunctions.scala
Last active December 11, 2015 20:48
partialfunctions.scala
// 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
@sofoklis
sofoklis / pforelse.scala
Last active December 11, 2015 20:48
partialfunctionsorelse
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
@sofoklis
sofoklis / typeclass.scala
Last active December 12, 2015 04:18
typeclassexample
// 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
}
@sofoklis
sofoklis / typeclasses.scala
Last active December 12, 2015 04:18
typeclasses
// 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
}
@sofoklis
sofoklis / BinarySearchTrees.scala
Last active December 12, 2015 08:18
BinarySearchTrees
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 {
@sofoklis
sofoklis / bfs8puzzle.scala
Created February 21, 2013 21:23
BFS loop 8-puzzle
@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)
}
@sofoklis
sofoklis / astar.scala
Created February 21, 2013 21:38
astar
@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
@sofoklis
sofoklis / full8puzzle.scala
Last active December 14, 2015 01:49
full 8 puzzle
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 {