Skip to content

Instantly share code, notes, and snippets.

View adamw's full-sized avatar

Adam Warski adamw

View GitHub Profile
implicit def readerDistributive[I]: Distributive[({type R[X]=Reader[I, X]})#R] = new Distributive[({type R[X]=Reader[I, X]})#R] {
override def distributeImpl[G[_], A, B](fa: G[A])(f: (A) => Reader[I, B])(implicit gf: Functor[G]): Reader[I, G[B]] =
Reader { i =>
fa.map(a => f(a)(i))
}
override def map[A, B](fa: Reader[I, A])(f: (A) => B): Reader[I, B] = fa.map(f)
}
/Library/Java/JavaVirtualMachines/jdk1.8.0_72.jdk/Contents/Home/bin/java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.8.0_72.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_72.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_72.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_72.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_72.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_72.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_72.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_72.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_72.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/
package com.softwaremill.kmq;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.processor.Processor;
import org.apache.kafka.streams.processor.ProcessorContext;
import org.apache.kafka.streams.processor.StateStoreSupplier;
import org.apache.kafka.streams.processor.TopologyBuilder;
import org.apache.kafka.streams.state.Stores;
@adamw
adamw / 1.java
Created October 12, 2017 12:30
@Path("user/{userId}")
public String getUser(@PathParam("userId") userId) {
...
}
@Path("/hello")
public class Hello {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String helloWorld() {
return "Hello World";
}
}
@adamw
adamw / 3.java
Created October 12, 2017 12:36
// Hello.java
public class Hello {
public String helloWorld() {
return "Hello World";
}
}
// HelloEndpoints.java
public class HelloEndpoints {
private Endpoint helloWorldEndpoint(Hello hello) {
class MyApplicationBootstrap {
public static void main(String[] args) {
// 1. create the object graph. Manually, by using "new". Like in the stone age.
Hello h = new Hello();
// 2. create a list of all endpoints our application will expose
List<Endpoint> endpoints = new ArrayList<Endpoint>();
endpoints.addAll(new HelloEndpoints().endpoints(h));
endpoints.addAll(...);
import com.softwaremill.sttp._
val sort: Option[String] = None
val query = "http language:scala"
// the `query` parameter is automatically url-encoded
// `sort` is removed, as the value is not defined
val request = sttp.get(
uri"https://api.github.com/search/repositories?q=$query&sort=$sort")
<K, V> void addToMultimap(ConcurrentHashMap<K, Set<V>> map, K key, V value) {
map.compute(key, (k, v) -> {
if (v == null) {
v = ConcurrentHashMap.newKeySet();
}
v.add(value);
return v;
});
}
val logDuration: Directive0 = extractRequestContext.flatMap { ctx =>
val start = System.currentTimeMillis()
mapResponse { resp =>
val took = System.currentTimeMillis() - start
logger.info(s"[${resp.status.intValue()}] ${ctx.request.method.name} " +
s"${ctx.request.uri} took: ${d}ms")
resp
}
}