Skip to content

Instantly share code, notes, and snippets.

View martijndwars's full-sized avatar

Martijn Dwars martijndwars

View GitHub Profile
@martijndwars
martijndwars / README.md
Created November 27, 2017 14:36
Java 8 stream are lazy. Or not?

I've always learned that Java 8 streams are lazy. As such, I would expect that:

Optional<Integer> intOpt = Stream
  .of(1, 2, 3)
  .flatMap(i -> Stream.of(4, 5, 6))
  .filter(i -> {System.out.println(i); return true;})
  .findAny();
@martijndwars
martijndwars / Instructions.md
Last active December 12, 2017 10:34
Parse a string in a .spoofax-language using Scala's REPL

Create an empty project with SBT:

sbt new sbt/scala-seed.g8

Update build.sbt to contain the Metaborg repository:

import Dependencies._
@martijndwars
martijndwars / build.gradle
Last active January 23, 2019 11:16
Gradle incrementality/task dependency example
task C {
doLast {
println("Do C")
}
}
task A(dependsOn: C) {
doLast {
println("Do A")
}
@martijndwars
martijndwars / gist:5e9c2cc72820f00699d1e3388072ef4f
Created June 11, 2019 11:25
Compile on Java 8, run on Java 11
martijn@dhcp-10-175-42-124:/p/t/javatest$ cat Main.java
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws Exception {
Unsafe unsafe = getUnsafe();
System.out.println("Page size = " + unsafe.pageSize());
}
@martijndwars
martijndwars / demo.md
Created July 1, 2019 07:06
PGX JShell demo

Demo

Start shell

$ ./jshell/pgx.sh

Show predefined variables

plugins {
id 'java'
}
repositories {
jcenter()
}
dependencies {
compileOnly 'com.google.code.findbugs:jsr305:3.0.2'
@martijndwars
martijndwars / Foo.java
Last active September 12, 2019 07:50
ThreadDeath example. The main thread starts a new thread, then stops the new thread. The new thread catches the ThreadDeath exception, giving it a chance to clean up, before rethrowing the exception and terminating.
package nl.martijndwars.oshell;
public class Foo {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Bar());
thread.start();
Thread.sleep(3000);
thread.stop();
}
@martijndwars
martijndwars / build.gradle
Created September 25, 2019 12:07
Gradle exclusion
/*
* Depend on Spoofax and replace org.graalvm:graal-sdk by org.graalvm.skd:graal-sdk. The exclusion
* resolves a conflict during compilatin and also shows up in the generated .pom.
*/
plugins {
id 'java'
id 'maven-publish'
}
group = 'nl.martijndwars'
@martijndwars
martijndwars / Asm.scala
Created October 30, 2019 10:47
Rewrite assembly addresses
import scala.io.Source
import scala.collection.mutable.Map
object Asm {
def main(args: Array[String]): Unit = {
val lines = Source.fromFile("input.txt").getLines.toList
val map = Map.empty[String, Int]
// First, map all addresses to integers
lines.filter(_.startsWith("0x")).map(_.take(18)).zipWithIndex.foreach {