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 / fibcpsbench.js
Last active March 9, 2021 19:56
Simple CPS fib benchmark
function fib(n) {
if(n < 2) return n;
return fib(n - 1) + fib(n - 2);
}
async function afib(n) {
if(n < 2) return n;
return await afib(n - 1) + await afib(n - 2);
}
async function mapA(array, f) {
let result = [];
for(let i = 0; i < array.length; i++) {
result.push(await f(array[i]));
}
return result;
}
function map(array, f) {
let result = [];
@Ahnfelt
Ahnfelt / TopShellSyntax.scala
Created February 17, 2020 07:32
The entire syntax tree for all of TopShell
case class Location(file : String, line : Int, column : Int) {
override def toString = "_"
def toSuffix = "in " + file + " " + toShortString
def toShortString = "at line " + line + ", column " + column
}
case class Binding(at : Location, name : String, scheme : Option[Scheme], value : Term)
sealed abstract class TopBlock {
def name : String
class Decoder {
constructor(dataView, options) {
options = options || {};
this._dataView = dataView;
this._offset = 0;
this._staticDictionary = options.staticDictionary || [];
this._textDecoder = new TextDecoder('utf-8');
if(

Safe Java

Controlling effects in Java via capabilities. Early sketch.

How it works

class Main {
    static void main(System system) {
        byte[] bytes = loadFile(system.getFileSystem());
@Ahnfelt
Ahnfelt / CurlyBraceML
Last active October 5, 2019 09:48
Curly brace ML
type Functor[T[_]] {
map[A, B](x : T[A], f : A -> B) : T[B];
}
type Monad[T[_]](functor : Functor[T]) {
flatten[A](x : T[T[A]]) : T[A];
pure[A](value : A) : T[A];
flatMap[A, B](x : T[A], f : A -> T[B]) : T[B] =
flatten(functor.map(x, f))
}
serversText <- File.readText "../../servers.txt"
servers = String.lines serversText
|> List.filter (x -> x != "")
hostTask = host ->
Ssh.do {user: "root", host: host} (
result <- Process.shell {command: "hostname"},
Task.of {name: String.trim result.out, host: host}
)
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"}
)
// 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}
@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 := {