Skip to content

Instantly share code, notes, and snippets.

View betandr's full-sized avatar
🦄
Vague, but exciting...

Beth Anderson betandr

🦄
Vague, but exciting...
View GitHub Profile
@betandr
betandr / gist:9377142
Created March 5, 2014 21:36
Java: Get color from a solid image
Color getImageColor(File imagePath) {
BufferedImage image = ImageIO.read(imagePath);
int color = image.getRGB(0, 0);
for (int r = 0; r < image.getHeight(); r += 1) {
for (int c = 0; c < image.getWidth(); c += 1) {
if (image.getRGB(c, r) != color) {
throw new IllegalArgumentException("Image: " + imagePath + " is not a solid color.");
}
}
}
private class DownloadJsonTask extends AsyncTask<String, Void, JSONObject> {
protected JSONObject doInBackground(String... strings) {
JSONObject jsonObject = null;
try {
InputStream is = new URL(strings[0].toString()).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
jsonObject = new JSONObject(readAll(rd));
// Nth Item
import scala.annotation.tailrec
def find(list: List[Int], item: Int) : Int = {
@tailrec def f(l: List[Int], i: Int, acc: Int): Int =
if(l.head == i) acc
else f(l.tail, i, acc+1)
class Rational(n: Int, d: Int) {
require(d!= 0)
private val g = gcd(n.abs, d.abs)
val numer = n / g
val denom = d / g
@betandr
betandr / gist:4aaad0ab750ea46ece9e
Created February 2, 2015 18:09
CoffeeMachine.scala
import scala.concurrent.future
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.util.Random
import scala.util.{Success, Failure}
class CoffeeMachine {
type CoffeeBeans = String
import scala.util.{Success, Failure}
import scala.concurrent.Future
import scala.util.Random
import scala.concurrent.ExecutionContext.Implicits.global
object Futures extends App {
val f: Future[String] = Future {
Thread.sleep(Random.nextInt(200))
"done..."
@betandr
betandr / gist:389f1e8ab4a1d38762a6
Last active August 29, 2015 14:14
Calculate number of items in a pyramid from number in its base
def pyramid(base: Int): Int = {
def p(b: Int, acc: Int): Int = {
if (b < 1) {
return acc
} else {
val n = acc + b - 1
p(b - 1, n)
}
}
@betandr
betandr / gist:a28a5948a52137bf22ef
Last active August 29, 2015 14:15
Serialized Futures using flatMap
import scala.util._
import scala.concurrent._
import ExecutionContext.Implicits.global
val f1: Future[String] = Future {
Thread.sleep(200)
"done 1..."
}
val f2: Future[String] = Future {
@betandr
betandr / gist:7893846319b2ba91c654
Last active August 29, 2015 14:15
Serialized Futures using for comprehension
import scala.util._
import scala.concurrent._
import ExecutionContext.Implicits.global
val f1: Future[String] = Future {
Thread.sleep(200)
"done 1..."
}
val f2: Future[String] = Future {
@betandr
betandr / gist:188f927e7e3ee7f29290
Created February 10, 2015 12:32
BLOCKING Futures with Awaits
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Success, Failure}
val f1: Future[String] = Future {
Thread.sleep(200)
"done 1..."
}