Skip to content

Instantly share code, notes, and snippets.

package main
import (
"bufio"
"fmt"
"io"
"net"
"strings"
)
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._
@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
@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) }
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 / 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 = []
@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 / 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 / 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 / LICENSE
Last active April 10, 2018 15:33
This license applies to all public gists https://gist.github.com/anthonynsimon
MIT License
Copyright (c) 2018 Anthony Najjar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: