Skip to content

Instantly share code, notes, and snippets.

func submit() {
guard let name = nameField.text else {
show("No name to submit")
return
}
guard let address = addressField.text else {
show("No address to submit")
return
}
@file:Suppress("NOTHING_TO_INLINE")
import java.io.BufferedReader
import java.io.File
import java.io.InputStream
import java.io.InputStreamReader
import kotlin.system.measureTimeMillis
// Kotlin 1.2.0 (probably 1.6.0 will work as well)
//
@apatrida
apatrida / DexlibAdapter.kt
Last active February 11, 2016 18:25
borisf/dex-method-dumper more idiomatic
package com.google.classyshark.silverghost.translator.dex
object DexlibAdapter {
val primitiveTypes = mapOf(
"I" to "int",
"V" to "void",
"C" to "char",
"D" to "double",
"F" to "float",
"J" to "long",
@apatrida
apatrida / java-code-involved-in-int-summarization.java
Last active January 7, 2016 23:09
Kotlin brevity as seen when comparing the implementation internally of JDK 8 Stream.collect summarizingInt and a custom Kotiln version. See many examples comparing Java 8 Stream.collect vs. Kotlin stdlib (http://stackoverflow.com/questions/34642254)
// Shown from the Java 8 JDK source code, only comments removed.
public static <T>
Collector<T, ?, IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper) {
return new CollectorImpl<T, IntSummaryStatistics, IntSummaryStatistics>(
IntSummaryStatistics::new,
(r, t) -> r.accept(mapper.applyAsInt(t)),
(l, r) -> { l.combine(r); return l; }, CH_ID);
public class IntSummaryStatistics implements IntConsumer {
private long count;
@apatrida
apatrida / AutoclosingKotlin.kt
Last active October 1, 2015 18:46
How to make your own support for AutoCloseable
class Fred : AutoCloseable {
override fun close() {
}
}
public fun foo() {
Fred().use {
// do something then autoclose
@apatrida
apatrida / VertxHttpServletRequest.java
Last active November 25, 2022 18:45
Vert-x3 wrappers for HttpServletRequest and HttpServletResponse
package com.collokia.webapp.routes;
import io.netty.handler.codec.http.HttpHeaders;
import io.vertx.core.net.SocketAddress;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.Session;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import javax.servlet.*;
@apatrida
apatrida / TimeStuff.kt
Created September 20, 2014 17:04
More with expressive Time things and Kotlin
enum class TimeUnit(val name: String, val divisorFromNanos: Long, val precision: Int) {
Seconds : TimeUnit("seconds", 1000000000, 2)
Millis : TimeUnit("millis", 1000000, 0)
Micros : TimeUnit("micros", 1000, 0)
Nanos : TimeUnit("nanos", 1, 0)
fun toHumanReadable(nanoTime: Long): String = "%.${precision}f ${name}".format(nanoTime.toFloat() / divisorFromNanos.toFloat())
}
class Duration(val duration: Long, val units: TimeUnit, private val durationInNanos: Long = duration * units.divisorFromNanos) : Comparable<Duration> {
class frank {
if (i= == 4) {
do something
}
}