Skip to content

Instantly share code, notes, and snippets.

View crabmusket's full-sized avatar

Daniel Buckmaster crabmusket

View GitHub Profile
@crabmusket
crabmusket / Serial.hs
Created October 1, 2013 08:27
Infinite serial listener in Haskell.
import Control.Monad (forever)
import Control.Concurrent (forkIO, killThread)
import qualified Data.ByteString.Char8 as B
import System.Hardware.Serialport
(openSerial, recv, closeSerial, defaultSerialSettings)
port = "COM1" -- Windows
--port = "/dev/ttyUSB0" -- Unix
main = do
@crabmusket
crabmusket / SerialThreepenny.hs
Last active December 24, 2015 09:39
Monitoring a serial connection with a Threepenny UI frontend.
-- Imports for serial port
import qualified Data.ByteString.Char8 as B
import System.Hardware.Serialport
(openSerial, recv, closeSerial, defaultSerialSettings)
-- Imports for threading and stuff
import Control.Monad (void, forever, mapM_)
import Control.Concurrent (forkIO, killThread)
import Control.Concurrent.Chan
(Chan, newChan, dupChan, writeChan, getChanContents)
@crabmusket
crabmusket / recvLn.hs
Last active December 24, 2015 23:49
Working on a function to receive a line of serial data in Haskell.
import qualified Data.ByteString.Char8 as B
import System.Hardware.Serialport (SerialPort, recv)
recvLn :: SerialPort -> IO B.ByteString
recvLn s = do
-- Receive one byte at a time.
first <- recv s 1
rest <- if first == B.singleton '\n'
then return $ B.empty
else recvLn s
import qualified Data.ByteString.Char8 as B
import Criterion.Main
import Data.IORef
import Data.Vector
import Data.ByteString.Char8 (ByteString)
import Control.Monad (forever)
import Pipes
import qualified Pipes.Prelude as P
import Data.Char (intToDigit)
import qualified Data.ByteString.Builder as Builder
@crabmusket
crabmusket / ConstantArithmetic.hs
Last active December 27, 2015 01:29
Convenient constant arithmetic for Haskell's Dimensional package.
module Numeric.Units.Dimensional.ConstantArithmetic where
import qualified Prelude
import qualified Numeric.Units.Dimensional
import Numeric.Units.Dimensional.Prelude
a *. b = a * (b *~ one)
a .* b = (a *~ one) * b
a .*. b = (a *~ one) * (b *~ one)
@crabmusket
crabmusket / negLogDet.hs
Last active January 1, 2016 00:39
Inspecting the -logdet function for convexity along lines.
import Numeric.LinearAlgebra
import Control.Monad (replicateM)
import System.Random (randomIO)
import Graphics.Gnuplot.Simple (plotFunc)
-- Test 100 random lines for convexity.
main = replicateM 100 $ do
-- Two 5x5 symmetric positive definite matrices that determine the line.
a <- randomSnPD 5
v <- randomSnPD 5
@crabmusket
crabmusket / Cheryl.hs
Last active August 29, 2015 14:19
A translation of Norvig's solution to the Cheryl's Birthday problem from Python to Haskell. Original: http://nbviewer.ipython.org/url/norvig.com/ipython/Cheryl.ipynb
-- A list of possible dates Cheryl's birthday might be on.
possibilities = [(May, 15), (May, 16), (May, 19),
(June, 17), (June, 18),
(July, 14), (July, 16),
(August, 14), (August, 15), (August, 17)]
-- We say we know the actual date when the list of possibilities is singular.
know ps = length ps == 1
-- Telling someone the month or day will reduce the possibilities.
@crabmusket
crabmusket / mercuryTree.js
Last active August 29, 2015 14:20
Rendering a recursive tree of items with mercury
'use strict';
// Mercury is a 'truly modular frontend framework'. This helpful import gets
// us a lot of top-level symbols from submodules it re-exports.
var hg = require('mercury');
// Like this one - h is a short constructor for HTML elements.
var h = require('mercury').h;
// And since we'll be running this in a browser using beefy, we need a reference
// to the document.
var document = require('global/document');
@crabmusket
crabmusket / keybase.md
Created May 18, 2017 11:45
Keybase verification

Keybase proof

I hereby claim:

  • I am crabmusket on github.
  • I am crabmusket (https://keybase.io/crabmusket) on keybase.
  • I have a public key ASC8gXuUziw0CsDZ1i5l0XQoHZmIdciVmtULjMDwc5qeowo

To claim this, I am signing this object:

JSON-RPC pipeline batches

Typical RPC implementation

Say we want to implement an RPC service for basic maths operations. For example, let's calculate the value of ln(e^2). This calculation has several steps in our maths API:

  1. Get the value of e
  2. Square e