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
def myFold[A](seq: IndexedSeq[A])(z: A)(f: (A, A) => A): A = { | |
// Divide the sequence into to halfes and then combine the results | |
def recur(from: Int, to: Int): A = { | |
if (from > to) { | |
z | |
} else if (from == to) { | |
seq(from) | |
} else { | |
f(recur(from, from + (to - from) / 2), recur(from + (to - from) / 2 + 1, to)) | |
} |
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
public class Main { | |
public static void main(String[] args) { | |
System.out.println(example()); | |
} | |
public static String example() { | |
try { | |
throw match(); | |
} catch (Int x) { |
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
import java.util.concurrent.ForkJoinPool; | |
import java.util.concurrent.ForkJoinTask; | |
import java.util.concurrent.RecursiveTask; | |
import java.util.concurrent.TimeUnit; | |
import java.util.function.Supplier; | |
import com.google.common.base.Stopwatch; | |
import com.google.common.util.concurrent.Uninterruptibles; | |
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
// Тайпкласс Animal (для полноты картины добавил метод hello, иначе был бы совсем тривиальный пример) | |
// Animal полностью абстрагирован от низлежашего типа A | |
trait Animal[A] { | |
def word: String | |
def talk() { println(word) } | |
def hello(a: A): String | |
} | |
// Обертка над тайпклассом Animal, чтобы можно было писать cat.hello, а не animal.hello(cat) | |
// AnimalOps полностью дублирует методы, определённые в Animal, но также может содержать еще и дополнительные методы. |
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
import scalaz.NonEmptyList | |
import scalaz.syntax.monad._ | |
import scalaz.syntax.equal._ | |
import scalaz.syntax.foldable._ | |
import scalaz.std.option._ | |
import scalaz.std.list._ | |
import scalaz.std.list.listSyntax._ | |
import scalaz.std.anyVal._ | |
import scalaz.std.string._ | |
import scalaz.effect.IO |
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
import scalaz.Applicative | |
import scalaz.syntax.enum._ | |
import scalaz.syntax.show._ | |
import scalaz.std.anyVal._ | |
import scalaz.effect.IO | |
import scalaz.effect.IO._ | |
object Main extends App { | |
def fizzBuzz(i: Int): String = if (i % 3 === 0 && i % 5 === 0) "FizzBuzz" |
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
-- Usage: | |
-- runhaskell newyear.hs n | |
-- This will print a nice christmas tree. For n=5: | |
-- | |
-- * | |
-- * * | |
-- * * * | |
-- * * * * | |
-- * * * * * | |
-- Happy New Year! |
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
// Java 1.8 | |
roles.addAll(authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList())); | |
// Java 1.5 | |
for (GrantedAuthority authority : authentication.getAuthorities()) { | |
roles.add(authority.getAuthority()); | |
} |
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
import Data.Array.ST | |
import Data.Array.Base | |
import Control.Monad.ST | |
sortPair arr (i, j) = do x <- readArray arr i | |
y <- readArray arr j | |
if (x > y) then do writeArray arr i y | |
writeArray arr j x | |
else return () |
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
import java.util.ArrayList | |
fun main(args : Array<String>) { | |
val a: ArrayList<Int> = ArrayList() | |
a.add(null) // WTF?? | |
println(a) | |
} |
OlderNewer