Skip to content

Instantly share code, notes, and snippets.

View sidharthkuruvila's full-sized avatar

Sidharth Kuruvila sidharthkuruvila

View GitHub Profile
@sidharthkuruvila
sidharthkuruvila / TextToImage.scala
Created December 17, 2010 17:59
Convert some text to an image
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import java.awt.Color
import java.io.File
import java.awt.Rectangle
import java.awt.font.LineBreakMeasurer
import java.awt.Graphics2D
import java.text.AttributedString
import io.Source
@sidharthkuruvila
sidharthkuruvila / NTripleTraversable.scala
Created July 19, 2011 17:00
Scala Traversable for jena's TurtleParser
import com.hp.hpl.jena.{ n3, graph }
import n3.turtle.TurtleEventHandler
import n3.turtle.parser.TurtleParser
import graph.Triple
import java.io.Reader
/**
* Wrapper to convert a TurtleParser into a Traversable
*/
@sidharthkuruvila
sidharthkuruvila / gist:1160196
Created August 21, 2011 05:42
Wrap method calls that return null in Option
def canReturnNull() = "hello world"
println(Option(canReturnNull) getOrElse "I'm saying nothing")
//Prints: hello world
def canReturnNull() = null
println(Option(canReturnNull) getOrElse "I'm saying nothing")
//Prints: I'm saying nothing
@sidharthkuruvila
sidharthkuruvila / gist:1160504
Created August 21, 2011 11:43
Handling null in java
String value = canReturnNull();
if(value != null)
println(value);
else
println("I'm saying nothing");
@sidharthkuruvila
sidharthkuruvila / gist:1161848
Created August 22, 2011 07:22
Prepend a nullable value to a linked list
Integer i = canReturnNull();
LinkedList<Integer> l = new LinkedList<Integer>(Arrays.asList(2,3,4));
if(i != null){
l.addFirst(i);
}
@sidharthkuruvila
sidharthkuruvila / gist:1161849
Created August 22, 2011 07:23
An Option in scala can prepended to a List using the ++: concatenation operator.
def canReturnNone = Some(1)
println(canReturnNone ++: List(2,3,4))
//prints: List(1, 2, 3, 4)
def canReturnNone = None
println(canReturnNone ++: List(2,3,4))
//prints: List(2, 3, 4)
@sidharthkuruvila
sidharthkuruvila / gist:1164529
Created August 23, 2011 07:08
Convert an array of chars into a String
char[] chars = {72, 101, 108, 108, 111};
String s = new String(chars);
@sidharthkuruvila
sidharthkuruvila / gist:1164534
Created August 23, 2011 07:10
Convert a list of Ints into a list of Chars
println(List(72, 101, 108, 108, 111).map(_.toChar))
//prints: List(H, e, l, l, o)
@sidharthkuruvila
sidharthkuruvila / gist:1164536
Created August 23, 2011 07:11
Convert a List of Ints into a String
println(List(72, 101, 108, 108, 111).map(_.toChar)(collection.breakOut))
//prints: hello
@sidharthkuruvila
sidharthkuruvila / gist:1164540
Created August 23, 2011 07:12
Convert a List of Ints into an Array of Chars
val a:Array[Char] = List(72, 101, 108, 108, 111).map(_.toChar)(collection.breakOut)
println(a)
//prints: [C@957cec