Skip to content

Instantly share code, notes, and snippets.

View sofoklis's full-sized avatar

Sofoklis Papasofokli sofoklis

View GitHub Profile
val s = Right(1)
val l = Left("Error")
val seq: Seq[(Int, Either[String, Int])] = Seq((2, s), (3, l))
for {
(id, Right(res)) <- seq
} yield (id, res)
for {
@sofoklis
sofoklis / topological2.cpp
Created March 19, 2013 18:32
topological search
#include <iostream>
#include <bitset>
using namespace std;
bool fastHasIncomingArrows(bool graph[20][20], int size, bitset<100> usedNodes, int node)
{
bool found = false;
for(int j = 0; !found && j < size; j++)
{
if(!usedNodes.test(j))
@sofoklis
sofoklis / 8puzzle.scala
Created February 21, 2013 22:32
breathFirstSearch
@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 / heuristic.scala
Created February 21, 2013 22:13
switching heuristic in Eight puzzle
// Creating a different solution with exactly the same code but a different
// ordering in my queue, so one can easily add solutions with better
// heuristic functions
def bestSolution(ordering: Ordering[(State, List[Move])]) =
aStarSearch(PriorityQueue((initialState, List[Move]()))(ordering), Set())
lazy val bestSolutionManhatan: Option[List[Move]] =
bestSolution(manhatanOrdering)
lazy val bestSolutionMisplaced: Option[List[Move]] =
@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 {
@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 / 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 / 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 / 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 / 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
}