Skip to content

Instantly share code, notes, and snippets.

View kretes's full-sized avatar

Tomasz Bartczak kretes

View GitHub Profile
package pl.japila.scalania.s99
object S99_P05 {
def reverse[T](ts: Seq[T]): Seq[T] = {
reverseAsList(ts.toList)
}
def reverseAsList[T](ts: List[T]): List[T] = {
ts match {
case List() => List()
@kretes
kretes / FizzBuzz.scala
Created February 23, 2014 16:27
my first scala fizzbuzz solution
package katas
object FizzBuzz {
def fizzBuzz(input:Int):String = {
class Solution(val result:String, val filter:Int) {
def isSatisfied(input:Int) = {
input % filter == 0 || input.toString.contains(filter.toString)
}
}
def isPalindrome[T](ts: Seq[T]): Boolean = ts.zip(ts.reverse).map((pair) => pair._1 == pair._2).contains(false) == false
// def isPalindrome[T](ts: Seq[T]): Boolean = ts.reverse.eq(ts)
def isPalindrome[T](ts: Seq[T]): Boolean = ts.zip(ts.reverse).map((pair) => pair._1 == pair._2).contains(false) == false
def takeWhileRec(function: (Int) => Boolean, list:List[Int]):List[Int] = list match {
case Nil => Nil
case head :: tail => if (function(head)) head +: takeWhileRec(function,tail) else Nil
}
"takeWhile" should {
"work the same" in {
val function: (Int) => Boolean = _ < 3
val list: List[Int] = List(1, 2, 3)
takeWhileRec(function,list) === list.takeWhile(function)
def drop[T](n: Int, ts: Seq[T]): Seq[T] = {
def dropO[T](current: Int, ts: Seq[T]): Seq[T] = ts match {
case Nil => Nil
case head +: tail => if(current==0) dropO(n-1,tail) else head +: dropO(current-1,tail)
}
dropO(n-1,ts)
}
def drop[T](n: Int, ts: Seq[T]): Seq[T] = {
def dropO[T](current: Int, ts: Seq[T]): Seq[T] = ts match {
case Nil => Nil
case head +: tail => if(current==0) dropO(n-1,tail) else head +: dropO(current-1,tail)
}
dropO(n-1,ts)
}
@kretes
kretes / ThreadTest.java
Created April 17, 2014 18:17
Simple switching the flag from other thread that will stop loop in thread
import org.junit.Test;
public class ThreadTest {
private boolean flag = true;
@Test
public void testName() throws Exception {
new Thread(new Runnable() {
private Function<Color, Color> filter;
@Test
public void j8Test() throws Exception {
Optional<Integer> optional = stream().filter((a) -> a > 10).findFirst();
optional.
Integer result = optional.map(i -> i * i).orElse(-1);
private Function<Color, Color> filter;
@Test
public void j8Test() throws Exception {
Optional<Integer> optional = stream().filter((a) -> a > 10).findFirst();
optional.
Integer result = optional.map(i -> i * i).orElse(-1);