Skip to content

Instantly share code, notes, and snippets.

@AndyBowes
AndyBowes / TreeNode.kt
Created August 2, 2017 11:20
Kotlin TreeNode (Draft)
fun idSequence(seed:Int=0) : Sequence<Int>{
return generateSequence(seed,{it + 1})
}
class TreeNode<T>(val id: T){
val childNodes = mutableListOf<TreeNode<T>>()
fun addChild(node: TreeNode<T>) = childNodes.add(node)
fun removeChild(node: TreeNode<T>) = childNodes.remove(node)
fun children() = childNodes.asIterable()
@AndyBowes
AndyBowes / Queue.kt
Created August 1, 2017 16:53
Kotlin Queue Implementation
class Queue<T> {
val elements: MutableList<T> = mutableListOf()
fun isEmpty() = elements.isEmpty()
fun count() = elements.size
fun enqueue(item: T) = elements.add(item)
fun dequeue() = if (!isEmpty()) elements.removeAt(0) else null
fun peek() = if (!isEmpty()) elements[0] else null
override fun toString(): String = elements.toString()
}
@AndyBowes
AndyBowes / Stack.kt
Created August 1, 2017 16:52
Kotlin Stack Implementation
class Stack<T>{
val elements: MutableList<T> = mutableListOf()
fun isEmpty() = elements.isEmpty()
fun count() = elements.size
fun push(item: T) = elements.add(item)
fun pop() : T? {
val item = elements.lastOrNull()
if (!isEmpty()){
elements.removeAt(elements.size -1)
}
@AndyBowes
AndyBowes / BreadthFirstSearch.kt
Created December 13, 2016 22:33
Kotlin - Breadth First Search
import java.util.*
fun Pair<Int,Int>.isWall(offset: Int) = isWall(this.first, this.second, offset)
fun isWall(x: Int, y: Int, offset:Int) = Integer.bitCount(x*x + 2*x*y + y*y + 3*x + y + offset) % 2 == 1
data class Move(val pos: Pair<Int,Int>, val visited: List<Pair<Int,Int>>, val depth: Int)
fun Move.adjacentCells(offset: Int): List<Pair<Int,Int>>{
return listOf(Pair(pos.first-1, pos.second),
@AndyBowes
AndyBowes / integration_test.gradle
Last active September 26, 2016 10:14
Gradle - Create Integration Tests
// Assumes 'java', 'groovy' or 'scala' plugins have been applied before
// Add integration test source sets
sourceSets {
integrationTest { sourceSet ->
["java", "groovy", "scala", "resources"].each {
if (!sourceSet.hasProperty(it)) return
sourceSet."$it".srcDir file("src/integration-test/${it}")
}
}
@AndyBowes
AndyBowes / LZWCodePointStream.java
Created February 15, 2016 10:58
Read LZW Compressed Stream to get 12-bit numbers as Code Points.
package org.nhs.mesh.util.lzw;
import java.io.IOException;
import java.io.InputStream;
/**
* Wrapper around an Input Stream which allows the Code Points to be read sequentially from
* an LZW compressed file.
*
* @author Andy Bowes
@AndyBowes
AndyBowes / 0_reuse_code.js
Created February 15, 2016 10:39
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@AndyBowes
AndyBowes / RomanNumeral.scala
Created July 4, 2012 16:07
Scala implementation of Roman Numeral conversion
class RomanNumeral {
def toRomanNumerals( number: Int) : String = {
toRomanNumerals( number, List( ("M", 1000),("CM", 900), ("D", 500), ("CD", 400), ("C", 100), ("XC", 90),
("L", 50), ("XL",40), ("X", 10), ("IX", 9), ("V", 5), ("IV", 4), ("I", 1) ))
}
private def toRomanNumerals( number: Int, digits: List[(String, Int)] ) : String = digits match {
case Nil => ""
case h :: t => h._1 * ( number / h._2 ) + toRomanNumerals( number % h._2, t )
}
@AndyBowes
AndyBowes / mongo-app-context.xml
Created July 4, 2012 13:43
Mongo Spring Application Context
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
@AndyBowes
AndyBowes / AlbumRepository.java
Created July 3, 2012 16:27
MongoDB Reposity Definition
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.RepositoryDefinition;
import uk.co.jfactory.database.music.domain.Album;
import java.util.List;
@RepositoryDefinition( domainClass = Album.class, idClass = ObjectId.class )
public interface AlbumRepository extends MongoRepository<Album, ObjectId> {