Skip to content

Instantly share code, notes, and snippets.

View Ahnfelt's full-sized avatar

Joakim Ahnfelt-Rønne Ahnfelt

View GitHub Profile
# 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,
// System.OpenCL.Private.Synchronization:
module System.OpenCL.Private.Synchronization (
enqueueMarker, enqueueBarrier, enqueueWaitForEvents, waitForEvents
) where
// [..snip..]
waitForEvents :: [Event] -> IO ()
waitForEvents events =
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
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);
}
package com.infopaq.research.flow.common;
import java.util.Arrays;
public class ByteId {
private final byte[] bytes;
public ByteId(byte[] bytes) {
this.bytes = bytes;
}
{-
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:
@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();
}
@Ahnfelt
Ahnfelt / compiler.fnk
Created October 13, 2017 16:16
Funk in Funk (very much unfinished)
;;;;;;;;;;
; Prelude
;;;;;;;;;;
if := {
|True body _| body()
|False _ body| body()
}
when := {
// WIP, untested attachable. A sort of generalized loader that should support infinite scroll and more.
// Example:
// val requester = Requester(this, Signal(1)) { (old, pageNumber) =>
// fetchPage(pageNumber).map(_ ++ old.toList.flatten)
// }
// Later, request page 2:
// requester.request(2)
import com.github.ahnfelt.react4s.Loader.{Loaded, Loading, Result, Error}
html <- Http.fetchJson {url: "https://reqres.in/api/users?page=2"}
people : List {id: Int, "first_name": String, "last_name": String, avatar: String} =
Json.toAny html.data
htmlImage = url -> Html.tag "img" [Html.attribute "src" url]
peopleWithImages = people |> List.map (
p -> {image: htmlImage p.avatar, name: p."first_name" + " " + p."last_name"}
)