Skip to content

Instantly share code, notes, and snippets.

@anthonynsimon
anthonynsimon / example.py
Created April 12, 2018 15:47
Dummy file backed job runner
import time
import os
class JobDoneError(Exception):
pass
class Job:
"""A file backed job, completed steps are commited to disk so it's resumable"""
@anthonynsimon
anthonynsimon / Interpreter.scala
Last active May 28, 2018 15:23
Simple Interpreter
// Program output:
// original: Power(Multiply(Variable(x),Sum(Variable(y),Multiply(Constant(5.0),Constant(7.0)))),Power(Sum(Constant(1.0),Constant(2.0)),Power(Sum(Constant(0.0),Constant(1.0)),Constant(2.0))))
// optimized: Power(Multiply(Variable(x),Sum(Variable(y),Constant(35.0))),Constant(3.0))
// show: ((x * (y + 35.0)) ^ 3.0)
// evaluates to: 216000.0 for variables: Map(x -> 1.5, y -> 5.0)
sealed trait Expr
case class Constant(a: Double) extends Expr
case class Variable(a: String) extends Expr
@anthonynsimon
anthonynsimon / BooleanQueryDSL.scala
Last active June 22, 2018 22:59
BooleanQueryDSL.scala
package com.anthonynsimon.boolquery
sealed trait BooleanQuery[A]
case class Pure[A](a: A) extends BooleanQuery[A]
case class And[A](x: BooleanQuery[A], y: BooleanQuery[A]) extends BooleanQuery[A]
case class Or[A](x: BooleanQuery[A], y: BooleanQuery[A]) extends BooleanQuery[A]
@anthonynsimon
anthonynsimon / signal_pool.py
Created April 6, 2018 12:30
Signal pool thread pool example
import threading
from threading import Thread, Lock
import sys
import time
class Q:
def __init__(self):
self._items = []
trait Functor[F[_], A] {
def fmap[B](f: (A) => B): F[B]
}
trait Applicative[F[_], A, B] {
def liftA(f: F[A]): F[B]
}
def fmap[A, B](f: (A) => B): (Option[A]) => Option[B] = {
case Some(x) => Option(f(x))
@anthonynsimon
anthonynsimon / main.kt
Last active June 24, 2018 12:30
Skynet benchmark
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.channels.Channel
import java.util.concurrent.atomic.AtomicLong
import kotlin.concurrent.thread
fun main(args: Array<String>) {
// bench("threads") { skynetThread(0, 1000000, 10) } // Out of Memory error
bench("sync") { skynetSync(0, 1000000, 10) }
bench("coroutines-async") { skynetCoroutinesAsync(0, 1000000, 10) }
bench("coroutines-launch") { skynetCoroutinesLaunch(0, 1000000, 10) }
@anthonynsimon
anthonynsimon / dping.ex
Last active June 27, 2018 12:34
Try elixir / OTP
defmodule PingPong do
def ping(pid) do
send pid, {:ping, self()}
receive do
:pong -> :ok
end
end
def pong do
receive do
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.{ChannelOption, ChannelHandlerContext, ChannelInitializer, ChannelInboundHandlerAdapter}
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.SocketChannel
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.channel.group.{ChannelGroup, DefaultChannelGroup}
import io.netty.handler.codec.http.{HttpObjectAggregator, HttpServerCodec}
import io.netty.handler.logging.{LogLevel, LoggingHandler}
import io.netty.channel.ChannelHandler.Sharable
import io.netty.handler.codec.http._
package main
import (
"bufio"
"fmt"
"io"
"net"
"strings"
)
@anthonynsimon
anthonynsimon / Pipelines.scala
Created July 26, 2018 13:05
Pipelines.scala
import scala.collection.mutable
trait Source[+A] {
def output(): A
}
trait Flow[A, B] extends Sink[A] with Source[B]
trait Sink[-A] {
def input(a: A): Unit