Skip to content

Instantly share code, notes, and snippets.

@andrewrlee
andrewrlee / codegen.kt
Last active December 5, 2022 10:19
Transform json file into the kotlin code that defines the same data structure
package uk.gov.justice.digital.hmpps.createandvaryalicenceapi.service
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import uk.gov.justice.digital.hmpps.createandvaryalicenceapi.model.policy.LicencePolicy
import java.io.File
import java.nio.charset.StandardCharsets.UTF_8
import kotlin.reflect.full.memberProperties
val seenTypes = mutableSetOf<Class<*>>()
@andrewrlee
andrewrlee / pixies.sh
Last active August 21, 2022 16:52
What Pixies can do in the time it takes Pearl Jam to play Rockin in the free world
#!/bin/bash
set -e
CLIENT_ID=…
CLIENT_SECRET=…
# ID for Pixies
ARTIST=6zvul52xwTWzilBZl6BUbT
DURATION_MS=$((15 * 60 * 1000))
@andrewrlee
andrewrlee / ExampleService.kt
Created January 11, 2021 17:18
Representing appointments as a timeline
import java.time.LocalTime
fun isAvailable(request: Request) {
val timeLine = createTimeLine(LocalTime.of(8, 0), LocalTime.of(18, 0), 5)
val bookings = getTodaysBookings() // listOf(RoomBooking(Room.ONE, TimeRange("2007-12-03T10:10:00", "2007-12-03T10:40:00")))
bookings.filter { notPartOf(request.bookingToAmmend) }. forEach { timeLine.add(it) }
return timeLine.isAvailable(request)
@andrewrlee
andrewrlee / AA_Example.java
Last active May 5, 2018 07:47
Result handling approaches
static class Service {
enum GetChildError {NOT_FOUND, OBSCURE_ERROR}
public Result<Double, GetChildError> getChild(String id, String childKey) {
if (id.equalsIgnoreCase("notfound")) {
return Result.failure(GetChildError.NOT_FOUND);
}
if (id.equalsIgnoreCase("obscure")) {
@andrewrlee
andrewrlee / Huffman.kt
Created August 11, 2017 13:07
Implementing Huffman encoding/decoding in Kotlin based on the simple algorithm described here: https://en.wikipedia.org/wiki/Huffman_coding
import java.util.SortedSet
fun main(args: Array<String>) {
val input = "A_DEAD_DAD_CEDED_A_BAD_BABE_A_BEADED_ABACA_BED"
val huffmanTree = createHuffmanTree(input)
val encoder = encoder(huffmanTree)
val decoder = decoder(huffmanTree)
@andrewrlee
andrewrlee / Caesar.kt
Created August 11, 2017 09:23
Playing with kotlin: Implementing the Caesar Cipher
package aaa
import java.util.concurrent.ThreadLocalRandom
private fun caesar(key: Int) : (Char) -> Char {
val seq = generateSequence('a' + key) { last -> if (last + 1 > 'z') 'a' else last + 1}
return { c -> seq.elementAt(c - 'a') }
}
private fun unCaesar(key: Int) : (Char) -> Char {
package uk.co.optimisticpanda.functionutils.pairs;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.tuple.Triple;
@andrewrlee
andrewrlee / Predicates.java
Created November 16, 2016 22:48
Predicates
package uk.co.optimisticpanda.functionutils.predicates;
import java.util.function.Function;
import java.util.function.Predicate;
public class Predicates {
public static <T, U> Predicate<T> has(Function<T, U> f, Predicate<U> p) {
return t -> p.test(f.apply(t));
}
@andrewrlee
andrewrlee / GenericTree.java
Last active October 18, 2016 22:18
Generic Task Tree - type safe chaining functions
package uk.co.optimisticpanda.configtaskchain.model;
import static java.util.stream.Collectors.toList;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.function.Supplier;
package uk.co.optimisticpanda.test;
import java.util.HashMap;
import java.util.Map;
/**
* Supporting subset of map operations based on the key containing type information.
*/
public class TypeSafeMapTest {