Skip to content

Instantly share code, notes, and snippets.

@reitzig
reitzig / StreamUtils.java
Last active January 16, 2020 13:56
Head and tail variants on (finite) Java streams (Java 11, JUnit 5)
import com.google.common.base.Preconditions;
import javax.annotation.Nonnull;
import java.util.Iterator;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;
public class StreamUtils {
/**
@reitzig
reitzig / assemble-cacerts.sh
Created May 7, 2019 15:09
Add custom root authority to Gradle, e.g. to access internal Maven repos with self-signed certificates
#!/usr/bin/env bash
# Following https://discuss.gradle.org/t/gradle-with-self-signed-certificate/23036/9
# Script assumes it's located in a subdirectory one level below the project root.
set -ue
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cacerts_pwd="changeit" # JDK default
@reitzig
reitzig / secret_santa.rb
Last active December 5, 2018 10:11
Invite people to a Secret Santa without you knowing who draws whom (sketch)
#!/usr/bin/env ruby
participants = [
{ :name => "Santa", :email => "santa@north.mas" },
{ :name => "Rudolf", :email => "rednose@north.mas" },
{ :name => "Ruprecht", :email => "knecht77@north.mas" }
]
def assign_gifts(folks)
perm = (0...folks.size).to_a.shuffle
@reitzig
reitzig / jsonschema_ref_vs_siblings.rb
Created November 12, 2018 14:26
JSON Schema MWE: $ref vs sibling keywords (Ruby)
gem 'json-schema'
require 'json-schema'
schema = <<~SCHEMA
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "A test schema",
"type": "object",
"oneOf": [
{
@reitzig
reitzig / GuardDsl.kt
Last active November 6, 2018 15:14
Implements a guard function for Kotlin that acts similar to the Swift keyword
import java.lang.RuntimeException
inline fun guard(predicate: Boolean, orElse: () -> Nothing) {
contract {
returns() implies predicate
callsInPlace(orElse, InvocationKind.AT_MOST_ONCE) // probably redundant
}
if (!predicate) {
orElse()
@reitzig
reitzig / Jenkinsfile
Created July 17, 2018 16:55
Report Jenkins build status to Upsource
pipeline {
agent any
environment {
UPSOURCE_URL = "http://your.upsource"
UPSOURCE_PROJECT = "your-project"
UPSOURCE_AUTH = credentials('upsource-auth')
// ^^ set this up as "secret text" credential in Jenkins;
// use the authentication token from Upsource project settings > integration
}
{
"b": {
"c": {
"foo": "bar"
}
}
}
@reitzig
reitzig / Camelizer.swift
Created February 22, 2018 17:26
Convert Swift strings to camel case
fileprivate let badChars = CharacterSet.alphanumerics.inverted
extension String {
var uppercasingFirst: String {
return prefix(1).uppercased() + dropFirst()
}
var lowercasingFirst: String {
return prefix(1).lowercased() + dropFirst()
}
@reitzig
reitzig / swift-mlsl.rb
Created February 6, 2018 13:13
Transforms a (long) string into a Swift multi-line string literal
#!/usr/bin/ruby
if ARGV.count < 2
puts "Usage: swift-mlsl.rb line-length string"
Process.exit(0)
end
blocksize = ARGV[0].to_i
parts = ARGV[1].scan(/.{1,#{blocksize}}/)
puts "\"\"\"\n" + parts.join("\\\n") + "\n\"\"\""
module Domain.SomeTitle exposing (..)
--
import Json.Decode as Decode
exposing
( succeed
, fail
, map
, maybe