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
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 { |
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
#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)) |
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
@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 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
// 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]] = |
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
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 { |
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
@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 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 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 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 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 | |
} |
NewerOlder