Skip to content

Instantly share code, notes, and snippets.

@gholker
gholker / JpgToPdf.java
Last active February 18, 2024 02:08
JPG to PDF in Java
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
@eicnix
eicnix / MockResponse.scala
Created July 14, 2015 07:54
Mocking complete Response using Scalamock and dispatch
// Example for lukaseichler.de/how-to-test-dispatch-request/
import java.io.InputStream
import java.net.URI
import java.nio.ByteBuffer
import java.util
import com.ning.http.client.cookie.Cookie
import com.ning.http.client.{ FluentCaseInsensitiveStringsMap, Response }
@retronym
retronym / indylambda.md
Last active February 5, 2022 10:47
indylambda: Putting invokedynamic to work for Scala

indylambda: Putting invokedynamic to work for Scala

Java 8 introduced lambdas to the Java language. While the design choices differ in many regards from Scala's functions, the underlying mechanics used to represent Java lambdas is flexible enough to be used as a target for the Scala compiler.

Lambdas in Java

Java does not have canonical heirarchy of generic function types (ala scala.FunctionN), but instead allows a lambda to be used as a shorthand for an anonymous implementation of an Functional Interface

Here's an example of creating a predicate that closes over one value:

@agemooij
agemooij / customformat.scala
Last active November 28, 2022 13:50
More advanced example of a custom spray-json format
implicit object ProductItemFormat extends RootJsonFormat[ProductItem] {
// some fields are optional so we produce a list of options and
// then flatten it to only write the fields that were Some(..)
def write(item: ProductItem) = JsObject(
List(
Some("product_number" -> item.productNumber.toJson),
item.ean.map(ean ⇒ "ean" -> ean.toJson),
Some("title" -> item.title.toJson),
item.description.map(description ⇒ "description" -> description.toJson),
Some("price" -> item.price.toJson),
@nicolashery
nicolashery / elasticsearch.md
Last active December 30, 2023 19:03
Elasticsearch: updating the mappings and settings of an existing index

Elasticsearch: updating the mappings and settings of an existing index

Note: This was written using elasticsearch 0.9.

Elasticsearch will automatically create an index (with basic settings and mappings) for you if you post a first document:

$ curl -X POST 'http://localhost:9200/thegame/weapons/1' -d \
'{
  "_id": 1,
@kevinwright
kevinwright / ThrottledExecutionContext.scala
Last active October 5, 2018 08:09
Limit number of futures running in parallel (now updated with seize/release implementations suggested by roland)
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicInteger
import scala.concurrent.ExecutionContext
import scala.util.control.NonFatal
import scala.annotation.tailrec
//inspired by https://gist.github.com/viktorklang/4552423
object ThrottledExecutionContext {
def apply(maxConcurrents: Int)(implicit context: ExecutionContext): ExecutionContext = {