Skip to content

Instantly share code, notes, and snippets.

View alexwestphal's full-sized avatar

Alex Westphal alexwestphal

  • OnPoint Digital, Inc.
  • New Zealand
  • 19:16 (UTC +13:00)
View GitHub Profile
@alexwestphal
alexwestphal / quicksort.scala
Created October 6, 2011 10:57
Functional style quicksort in scala
def sort(xs: Array[Int]): Array[Int] = {
if (xs.length <= 1) xs
else {
val pivot = xs(xs.length / 2)
Array.concat(
sort(xs filter (pivot >)),
xs filter (pivot ==),
sort(xs filter (pivot <)))
}
}
@alexwestphal
alexwestphal / NotRandom.java
Created October 27, 2011 11:40
Not So Random
import java.util.Random;
public class NotRandom {
public static void main(String[] args) {
System.out.println(randomString(0xf24ab354)+' '+randomString(0xf72f13ef));
//Prints "hello world"
}
public static String randomString(int seed) {
@alexwestphal
alexwestphal / .inputrc
Created April 18, 2013 08:57
Search through your history using the up and down arrows i.e. type "ls /" and press the up arrow and you'll search through everything in your history that starts with "ls /".
"\e[A": history-search-backward
"\e[B": history-search-forward
set show-all-if-ambiguous on
set completion-ignore-case on
@alexwestphal
alexwestphal / ferry.scala
Created June 2, 2015 02:42
Exploring the Ferryman (Cabbage/Goat/Wolf) problem
sealed abstract class Side
case object Left extends Side
case object Right extends Side
sealed abstract class Thing
case object Cabbage extends Thing
case object Goat extends Thing
case object Wolf extends Thing
@alexwestphal
alexwestphal / multiple_return.kt
Last active January 31, 2018 22:14
Return multiple values using a data class
// Define a data class to hold the result
data class FoundThings(val x: Int, val y: String, val z: Double)
// Your function returns an instance of the data class
fun findThings(): FoundThings {
// Do lookups
return FoundThings(2, "hello", 4.0)
}
@alexwestphal
alexwestphal / method_use.kt
Created January 31, 2018 23:09
The `use` method
// Write to a file using the `use` method to close the PrintWriter when finished or if an error occurs.
// Semantics are pretty much the same as Java's try-with-resource
File("hello.txt").printWriter().use { out ->
out.println("Hello World")
}
// Which is roughly equivalent to
val file = File("hello.txt")
val outputStreamWriter = OutputStreamWriter(FileOutputStream(file), Charsets.UTF_8)
@alexwestphal
alexwestphal / function_buildString.kt
Created February 14, 2018 19:57
The buildString function
// The buildString function simplifies the use of StringBuilder
val s = buildString {
append("Hello")
append(" ")
append("World")
}
// Is equivalent to
val stringBuilder = StringBuilder()
stringBuilder.append("Hello")
@alexwestphal
alexwestphal / sealed_classes.kt
Created February 19, 2018 20:54
Using sealed classes
// A sealed class is a special kind of abstract class. The difference being that for a sealed class, all subclasses must
// be declared in the same file. This means that the compiler knows about the fixed set of subclasses.
sealed class Shape {
abstract val area: Double
}
data class Circle(val radius: Double): Shape() {
override val area = Math.PI * radius * radius
}
@alexwestphal
alexwestphal / renaming_imports.kt
Created February 20, 2018 22:14
Renaming imports for simplicity or disambiguation
// Rename the import to TFConfig
import com.example.config.TraversalFailureConfiguration as TFConfig
// Can now use it anywhere in the file as if TFConfig was it's actual name
fun main(args: Array<String>) {
val config: TFConfig = TFConfig()
}