Skip to content

Instantly share code, notes, and snippets.

View Ahnfelt's full-sized avatar

Joakim Ahnfelt-Rønne Ahnfelt

View GitHub Profile
@Ahnfelt
Ahnfelt / MinimalDI
Last active August 29, 2015 14:13
Frameworkless, minimal, typesafe Dependency Injection for Java.
interface ShopService {
void buy();
void sell();
}
interface DatabaseService {
void beginTransaction();
void commit();
void abort();
}
{-
This code parses a subset of JavaScript and detects usages of undeclared variables.
It's an example of a Parsec parser; it's by no means complete.
This source code primarily uses the do-syntax for writing the parser,
but that's not the only way to write code with Parsec.
This code was written in a hurry by Joakim Ahnfelt-Rønne during the meetup at 2014-21-10.
USAGE:
package com.infopaq.research.flow.common;
import java.util.Arrays;
public class ByteId {
private final byte[] bytes;
public ByteId(byte[] bytes) {
this.bytes = bytes;
}
public void writeTag(int tag) throws SerializationException {
try {
if(tag < 128) {
stream.writeByte(tag - 128);
} else {
stream.writeInt(tag);
}
} catch(IOException e) {
throw new SerializationException(e);
}
data Sound
= Silence
| Audio AudioData
| Volume Double Sound
| Sequence Sound Sound
data SoundM a = SoundM Sound a
instance Monad SoundM where
return a = SoundM Silence a
// System.OpenCL.Private.Synchronization:
module System.OpenCL.Private.Synchronization (
enqueueMarker, enqueueBarrier, enqueueWaitForEvents, waitForEvents
) where
// [..snip..]
waitForEvents :: [Event] -> IO ()
waitForEvents events =
# This is an implementation of Software Transactional Memory.
# The idea is that when a variable is first used in an atomic block,
# we save a snapshot of its current value and version.
# When we reach the end of the transaction, we lock all variables
# that have been read or written to inside the atomic block. If any
# of the variables we read has been changed from the outside, we
# reset the transaction and rerun it from the beginning. Otherwise
# we write the new values back into the shared memory with a new
# version. For each variable, we maintain information on what other
# variables have been updated to what versions at the same time,