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
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."); | |
} | |
} | |
} |
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
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)); |
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
// 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) | |
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
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 |
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 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 |
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 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..." |
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 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) | |
} | |
} |
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 scala.util._ | |
import scala.concurrent._ | |
import ExecutionContext.Implicits.global | |
val f1: Future[String] = Future { | |
Thread.sleep(200) | |
"done 1..." | |
} | |
val f2: Future[String] = Future { |
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 scala.util._ | |
import scala.concurrent._ | |
import ExecutionContext.Implicits.global | |
val f1: Future[String] = Future { | |
Thread.sleep(200) | |
"done 1..." | |
} | |
val f2: Future[String] = Future { |
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 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..." | |
} |
OlderNewer