Skip to content

Instantly share code, notes, and snippets.

View luciferous's full-sized avatar
🏠
Working from home

Neuman Vong luciferous

🏠
Working from home
View GitHub Profile
@luciferous
luciferous / chunk.hs
Last active January 3, 2016 10:39
HTTP server that responds with a stream of the letter "a" in 128-byte chunks forever.
module Main where
import Control.Concurrent (forkIO, threadDelay)
import Control.Exception (catch, IOException(..))
import Numeric (showHex)
import Network (accept, listenOn, PortID(PortNumber))
import System.IO (hClose, hPutStr, hSetBuffering, BufferMode(NoBuffering))
import System.Environment (getArgs)
size = 10 * (1024 ^ 3) -- gigabyte multiplier
@luciferous
luciferous / Free.scala
Last active January 3, 2016 04:29
Free monad in Scala
import language.higherKinds
import language.implicitConversions
object Main {
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B): F[B]
}
trait Monad[F[_]] extends Functor[F] {
@luciferous
luciferous / Main.scala
Created January 9, 2014 19:11
Actors in Scala.
package com.twitter.finagle.actor
import com.twitter.concurrent.AsyncQueue
import com.twitter.util.Future
trait Actor[A] extends (A => Future[Unit]) {
val mailbox = new AsyncQueue[Any]
def send(msg: Any) = mailbox.offer(msg)
def receive = mailbox.poll
}
import scala.concurrent._
import ExecutionContext.Implicits.global
import scala.util.parsing.combinator._
import scala.util.matching.Regex
object Scout {
object UrlParser extends RegexParsers {
def digits: Parser[Int] = """-?\d+""".r ^^ (_.toInt)
}
object Main {
case class NFA[Q,S](
initialState: Q,
isAccepting: Q => Boolean,
transition: (Q, S) => Seq[Q]
)
// foldM specialized for Seq
def foldM[A,B](f: (A,B) => Seq[A], a: A, b: Seq[B]): Seq[A] = b match {
case Nil => Seq(a)
@luciferous
luciferous / Main.scala
Last active January 1, 2016 08:39
Alternative to Dep Injection
import java.io.{File, PrintWriter}
import scala.language.higherKinds
object Main {
type Logger = String => Unit
def nullLogger(msg: String) { }
def stdErrLogger(msg: String) {
System.err.println(msg)
@luciferous
luciferous / README.md
Last active May 10, 2025 06:30
Distributed Javascript

distributed.js

An example of how an (Erlang inspired) distributed actor framework could work in Node.js.

Running the example

Start slave nodes.

$ node example.js slave localhost:3000 &

$ node example.js slave localhost:3001 &

@luciferous
luciferous / thrift.md
Last active December 23, 2015 00:49
Get Haskell example from Thrift to compile.

See: 06b3e824c.

Add fromIntegral to convert GHC.Int.Int32 to Int.

HaskellServer.hs:73:60:
    Couldn't match type GHC.Int.Int32'
    Expected type: Maybe GHC.Int.Int32
      Actual type: Maybe Int
    In the ($)', namely

Data.Text.Lazy.Internal.Text'

@luciferous
luciferous / multinet.hs
Created August 27, 2013 06:49
Multicast Telnet. Telnet into multiple hosts. Sends the same message to each host and prints all their responses to stdout.
module Main where
import Control.Arrow ((>>>), second)
import Control.Concurrent (forkFinally)
import Control.Concurrent.MVar (newMVar, modifyMVar_, readMVar)
import Network (connectTo, HostName, PortID(..), withSocketsDo)
import System.Environment (getArgs)
import System.IO (Handle, hClose, hGetContents, hPutStrLn)
toAddr :: String -> (HostName, PortID)
@luciferous
luciferous / imgdl.js
Created August 7, 2013 08:00
Concurrent image downloader.
if (typeof Image == "undefined") {
var Image = (function() {
function Image() { }
Image.prototype.addEventListener = function(name, fn, bubble) {
setTimeout(function() { if (name === "load") { fn(); } }, 0);
};
return Image;
})();
}