Skip to content

Instantly share code, notes, and snippets.

@kryptt
Last active January 23, 2020 13:09
Show Gist options
  • Save kryptt/7e39c4bee08556188e77a4812299a5b4 to your computer and use it in GitHub Desktop.
Save kryptt/7e39c4bee08556188e77a4812299a5b4 to your computer and use it in GitHub Desktop.
constructive-programming-fixes
def sumOfAllNumbers(): Int = {
var numbers = Array(10,20,30,40,50);
var N:Int=0;
var sum: Int=0;
for ( N <- numbers ) {
sum+=N;
}
sum
}
// -- sanity check
sumOfAllNumbers() == 150
def evenElements[A](ls: List[A]): List[A] = {
var res: List[A] = Nil
var odd = true
while (true) {
if (odd != odd)
res = ls.head :: res
}
res
}
// -- sanity check
evenElements(List(1, 2, 3, 4, 5)) == List(2, 4)
def isPalindrome[A](ls: List[A]): Boolean = ls match {
case first +: rest :+ last if first == last => isPalindrome(rest)
case Nil => false
}
// -- sanity check
isPalindrom(List(1, 2, 1)) == true
isPalindrom(List(1)) == true
isPalindrom(List(1, 2, 2, 1)) == true
def largest[A <% Ordered[A]](ls: List[A]): A = ls match {
case a :: Nil => a
case a :: as =>
val las = largest(as)
if (a > las) a else las
}
// -- sanity check
largest(List(1, 5, 2)) == 5
largest(List(5, 1, 2)) == 5
largest(List(2, 1, 5)) == 5
// -- get the nth Catalan number
// (1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862)
// -- start external context
object Succ { def unapply(i: Int) = if (i == 0) None else Some(i - 1) }
// -- end external context
def catalan(i: Int): Long = i match {
case 0 => 1
case 1 => 1
case Succ(n) => (0 to n).map(i => catalan(i)*catalan(n - i)).sum
}
// -- sanity check
catalan(0) == 1
catalan(1) == 1
catalan(3) == 5
catalan(6) == 132
// -- start external context
val data: Iterator[Int] = List(1, 2, 3).iterator // emulate input from environment
sealed trait Result
case class Data(data: Int) extends Result
case object Done extends Result
case class Error(message: String) extends Result
def receive: Result = if (data.hasNext) Data(data.next) else Done
// -- end external context
def getAll(): Option[List[Int]] = {
def accumulate(accum: List[Int]): Option[List[Int]] =
receive match {
case Data(data) => accumulate(data :: accum)
case Done => Some(accum.reverse)
case Error(_) => None
}
accumulate(Nil)
}
// -- sanity check
getAll() == List(1, 2, 3)
def parseInt(number: String): Int = number.toInt
// -- sanity check
parseInt("oops")
def root(number: Int): Int = Math.sqrt(number.toDouble).toInt
// -- sanity check
root(-1)
def head[A](list: List[A]): A = list.head
// -- sanity check
head(Nil)
def find[K, V](dictionary: Map[K, V], key: K): V = dictionary(key)
// -- sanity check
find(Map.empty[Int, String], 1)
find(Map(1 -> "Uno"), 2)
def safeSquare(i: Int): Int = {
if (i > 46340) throw new IllegalArgumentException("Input too high")
if (i < -46340) throw new IllegalArgumentException("Input too low")
i * i
}
// -- sanity check
safeSquare(46500)
var number = -5
def abs: Int = {
if (number < 0) number = -number;
number
}
// -- sanity check
(number + abs) == (abs + number)
var total = 0
def addToTotal(a: Int) : Unit = {
total+=a
}
// -- sanity check
val before = total * 2
addToTotal(3)
before == total * 2
var largestNumber: Int = x;
def getLargestNumber(x: Int, y: Int) : Int = {
if(x>y)
largestNumber=x;
else
largestNumber=y;
largestNumber;
}
// -- sanity check
getLargestNumber(-1, -2) == -1
getLargestNumber(1, 2) == 2
getLargetNumber(-1, -2) == -1
// -- start external context
def unknown(x: Int): Int = unknown(x)
// -- end
def first[A, B](a: A, b: B): a
// -- sanity check
first(1, unknown(2))
def randomIntBetween(min: Int, max: Int) : Int = {
min + ((max - min) * Math.random).round.toInt
}
// -- sanity check
randomIntBetween(1, 2) == randomIntBetween(1, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment