Skip to content

Instantly share code, notes, and snippets.

View diversit's full-sized avatar

Joost den Boer diversit

View GitHub Profile
@diversit
diversit / ThrushOperator.scala
Created February 25, 2020 09:43
Thrust operator to move monad conversion to right to get more readable code
package utils
/**
* From 'How to unsuck Future[Option[T]]' by Erik Bakker.
* Operator taken from Scalaz which allow to specify a function in a different order.
*
* This operator allows to move transformations in for-comprehension to the right
* which makes the code easier to read.
*/
object ThrushOperator {
@diversit
diversit / JavaCompletableFuturesSequencer.java
Last active July 16, 2020 12:18
In Java, sequence a List<CompletableFuture<T>> to a CompletableFuture<List<T>> fully non-blocking
/**
* True non-blocking sequence implementation.
* Does _NOT_ use 'join' or 'get'
* Creates a new CompletableFuture (aka a promise) which is only completed
* once all futures have completed either successfully or with failures.
* Failures are ignored, but could also be accumulated if the result type would not be List
* but a SequenceResult for example which would contain both the list of result and list of errors
* and, this class could even extend List.
*/
package eu.allego.ag.authoriser;
@diversit
diversit / count files per folder
Created August 23, 2018 07:16
count-files-per-folder.sh
# Loop over folders and count the files in the folder
find . -maxdepth 1 -mindepth 1 -type d | while read dir; do
printf "%-25.25s : " "$dir"
find "$dir" -type f | wc -l
done
@diversit
diversit / seq-distinct-on.scala
Created June 19, 2018 13:34
Scala Seq distinct on an item property
object SeqWithDistinctOn {
/** Utility class to run a distinct function on */
case class DistinctResult[A, B](val items: Seq[A] = Seq.empty, val keys: Seq[B] = Seq.empty) {
/**
* Add item with given key.
* If key already exists, the item will not be added.
*
* @param item Item to be added if #key has not been added yet.
* @param key Key of item.
* @return A [[DistinctResult]] with added item of key did not exist, otherwise current [[DistinctResult]].
@diversit
diversit / graphiql.html
Created May 5, 2018 13:58
Single page GraphiQL editor with JWT token and working auto-complete and documentation
<!--
* Copyright (c) Facebook, Inc.
* All rights reserved.
*
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
-->
<!-- Custom graphiql with jwt header
@diversit
diversit / SeqWithSplitOn.scala
Created April 17, 2018 09:26
Split a Seq[A] into (Seq[B], Seq[C]) so can retrieve multiple Seq's while just looping over the Seq just once.
/**
* Extend [[Seq]] with a function to seperate an element into multiple parts.
* This allows to go over the [[Seq]] only once and retrieve multiple [[Seq]]s as a result.
* Convenient for changing a Seq of [[Tuple2]] into 2 Seq's of the tuple elements.
*/
implicit class SeqWithSplitOn[A](val seq: Seq[A]) extends AnyVal {
/**
* Split an element into 2 parts using function `f`.
* Note: simply use the [[identity()]] function for splitting a [[Tuple2]] element.
*
@diversit
diversit / ISO8601-json.scala
Created April 11, 2018 09:13
Proper deserialisation of Java 8 Time
/**
* Java 8 does not full support ISO8601 date/time format. (https://en.m.wikipedia.org/wiki/ISO_8601)
* It does not support timezone '±HHMM' or '±HH'.
* This has been reported in issue https://bugs.openjdk.java.net/browse/JDK-8032051,
* but has only been fixed in Java 9, not in Java 8!
*
* This object provides an alternative [[Reads]] instance which fully supports ISO8601 formats,
* which should be used instead of Play Json's [[play.api.libs.json.EnvReads.DefaultInstantReads]].
*/
object ISO8601 {
@diversit
diversit / slickCodeGeneratorInsertDef.scala
Created March 1, 2018 12:37
Slick Code Generator adding an extra sub generator
/**
* In custom Table implementation add a new `Def` or `TypeDef` implementation
* and add it to the `definitions`.
*
* The custom `Def` can conditionally be applied to tables by overriding the `enabled` function.
*
* This `InsertDef` adds some helper functions to only insert a row based on a predicate.
* See https://stackoverflow.com/a/31352126/3309859 for an example.
* Since defining the `insert` query is quite cumbersome, I created the generator to generate it.
*/
@diversit
diversit / PostConstructModule.java
Created December 8, 2017 13:01
Guice PostConstructModule
package flexc.graph.guice.module;
import com.google.inject.AbstractModule;
import com.google.inject.TypeLiteral;
import com.google.inject.matcher.Matchers;
import com.google.inject.spi.InjectionListener;
import com.google.inject.spi.ProvisionListener;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;
@diversit
diversit / AWSDeserializer.scala
Created November 1, 2017 19:50
Deserialize json into a DynamodbEvent
package json;
import com.amazonaws.services.dynamodbv2.model.OperationType;
import com.amazonaws.services.dynamodbv2.model.StreamViewType;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;