Skip to content

Instantly share code, notes, and snippets.

import java.util.Arrays;
import java.util.List;
import static java.lang.String.join;
import static java.util.Collections.emptyList;
import static java.util.Collections.nCopies;
import static java.util.stream.Collectors.toList;
/**
def f4(implicit v: T) = ...
def f5(implicit v: T) = ...
for {
v1 <- f1
v2 <- f2
implicit v3 <- f3
v4 <- f4
v5 <- f5
} yield { ... }
# To reproduce this Haskell function in Python we need to define some helper
# functions first. But Python's list comprehensions support nested loops just
# like Haskell.
#
# combinations :: Int -> [a] -> [[a]]
# combinations 0 _ = [ [] ]
# combinations n xs = [ y:ys | y:xs' <- tails xs, ys <- combinations (n-1) xs']
def headtail(lst):
"""
@haroldl
haroldl / Helpers.scala
Created January 22, 2014 15:33
Android Helper code to move code back and forth between an AsyncTask background thread and the main UI thread, and to implicitly convert a block of code into an OnClickListener.
object Helpers {
def backgroundThread(code: => Unit) {
val task = new AsyncTask[AnyRef,AnyRef,AnyRef]() {
override def doInBackground(params: AnyRef*) = {
code
null
}
}
task.execute()
val t = findViewById(R.id.textView1).asInstanceOf[TextView]
val b = findViewById(R.id.button1).asInstanceOf[Button]
b setOnClickListener {
view: View =>
backgroundThread {
// Do things like network requests here in the background thread...
Thread.sleep(3000)
uiThread {
// Back on the main thread, update the UI...