Skip to content

Instantly share code, notes, and snippets.

View norswap's full-sized avatar

norswap norswap

View GitHub Profile
@norswap
norswap / jaffar.rb
Created September 8, 2012 14:50
Jaffar, the Evil Advisor Library
# Jaffar, the Evil Advisor Library
#
# This is a simple experiment in Ruby metaprogmming, to see how to implement
# a lisp-style advising construct. Advising is basically augmenting existing
# functions with your own code. See the exemple below.
#
# Advising is very much related to context-oriented programming. I fortunately
# have no use for either an advising or context-oriented programming right now
# and this is why I did not expand upon this simple implementation.
#
@norswap
norswap / Multi.java
Created March 25, 2013 09:54
Multi.java (Multimethods in Java)
package util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* This class implements multiple dispatch (aka multimethods) using reflection.
* The idea is to choose the method based not only on the runtime type of the
* receiver, but also on the runtime type of the arguments. Normally Java
* selects amongst overloaded alternatives using the static type.
@norswap
norswap / PluginInterface.kt
Created January 18, 2016 16:36
Plugin Interface Proposal Example for Kotlin
// Basic setup
interface Exp
data class Lit(val x: Int): Exp
data class Add(val x: Exp, val y: Exp): Exp
val exp0 = Add(Lit(42), Lit(0x52))
// Adding a print operation

As VirtualBox Guest

Networking

In the VM properties, make two connections: a "NAT" for regular internet connectivity, and a "host-only" for ssh connectivity between host and guest with a predictable IP. For this to work, both a NAT adapter and a host-only adapter have to be created in the host via the VirtualBox global preferences.

@norswap
norswap / InlineReceiver.kt
Created November 8, 2016 13:50
Showcasing the usefulness of inline receivers in Kotlin
inline fun <T, U> (() -> T?).map (crossinline f: (T) -> U): (() -> U?)
{
return { this()?.let(f) }
}
inline fun <T> (() -> T?).filter (crossinline pred: (T) -> Boolean): (() -> T?)
{
return {
var out = this()
while (out != null && !pred(out)) out = this()
### Keybase proof
I hereby claim:
* I am norswap on github.
* I am norswap (https://keybase.io/norswap) on keybase.
* I have a public key whose fingerprint is CB7E 4E0F F8C0 2860 6A62 55F2 A7E2 9E43 45FB 1D8E
To claim this, I am signing this object:
@norswap
norswap / ObjectAlgebra.kt
Created January 18, 2016 16:30
Object Algebras in Kotlin
// Basic setup
interface Exp
data class Lit(val x: Int): Exp
data class Add(val x: Exp, val y: Exp): Exp
interface IntAlg<A>
{
fun lit(x: Int): A
fun add(x: A, y: A): A
@norswap
norswap / Visitors.java
Created May 25, 2019 18:38
Visitors in Java 8
public final class Visitors
{
// ---------------------------------------------------------------------------------------------
// 1. Initial setup.
interface Visitor {
void visit (A object);
void visit (B object);
}
@norswap
norswap / ! Visitors Comparison
Last active June 22, 2020 21:56
Visitors Comparison
See https://norswap.com/expression-problem-java/
@norswap
norswap / AddNodeGen.java
Last active June 27, 2020 16:16
Generated Files (listings for https://norswap.com/truffle-tutorial/)
// CheckStyle: start generated
package exni.nodes;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.dsl.GeneratedBy;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeCost;
import exni.nodes.AddNode;
import exni.nodes.ExniNode;