Skip to content

Instantly share code, notes, and snippets.

View charles-cooper's full-sized avatar

Charles Cooper charles-cooper

View GitHub Profile

Keybase proof

I hereby claim:

  • I am charles-cooper on github.
  • I am charlescooper (https://keybase.io/charlescooper) on keybase.
  • I have a public key ASBc3MIg-ZmRXFd8_NXIsH6FfxvXAzpf8lEZLwVBNnf3mgo

To claim this, I am signing this object:

@charles-cooper
charles-cooper / filtered_query.plan
Last active July 15, 2016 18:33
Row based access query plans
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Nested Loop (cost
@charles-cooper
charles-cooper / shutdown_idle.sh
Created June 30, 2016 14:10
Cron job to shut down an idle computer
#!/bin/sh
# This script, intended to be run as a cron job, checks if there are any logins or recent logins and shuts down if there are none. It is not intended to be portable and was written on a Ubuntu 16.04 machine.
last_login_time=$(grep "systemd-logind" /var/log/auth.log | perl -pe "s/ ip.*//" | tail -1 | date --file=/dev/stdin +%s)
if [ $(($cur_time - $last_login_time)) -ge 3600 ] && [ $(who | wc -l) -eq 0 ]
then
init 0
else
@charles-cooper
charles-cooper / .profile
Created June 10, 2016 14:52
Default .profile for ubuntu
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
@charles-cooper
charles-cooper / Main.hs
Last active July 6, 2016 17:47
waitForProcess bug
module Main where
import System.Process
import Control.Concurrent
main :: IO ()
main = do
(_, _, _, p) <- createProcess ((shell "sleep 1") { create_group = True })
forkIO $ waitForProcess p >>= print
threadDelay 10
@charles-cooper
charles-cooper / Lists.hs
Created March 26, 2016 15:56
fun with lists
{-# LANGUAGE DeriveFunctor #-}
module Main where
import Data.Monoid
import Control.Comonad
import Data.Fix
import Data.Foldable as Foldable
newtype Cell a b = Cell { runCell :: Either () ((,) a b) } deriving (Functor)
@charles-cooper
charles-cooper / dataKinds.hs
Created January 31, 2016 18:45
fun with data kinds
{-# LANGUAGE TypeFamilies, DataKinds, PolyKinds, TypeOperators #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# OPTIONS_GHC -fwarn-unticked-promoted-constructors #-}
import Prelude hiding (lookup)
import qualified Data.Map as Map
type family Equal a b :: Bool where
Equal a a = 'True
@charles-cooper
charles-cooper / vectors.hs
Created January 31, 2016 17:03
vectorised numeric instances in haskell
-- These are straightforward. Just map or zipWith all operations.
-- The only interesting one is fromInteger, typically when people
-- do an operation on a vector and a scalar, they mean to run it
-- over the vector. E.g. [1,2,3] * 1.
instance Num a => Num [a] where
(+) = zipWith (+)
(*) = zipWith (*)
abs = map abs
signum = map signum
data Val a = Val Int deriving Show
instance Functor Val where
fmap _ (Val x) = Val x
coerce :: Val a -> Val b
coerce = fmap undefined
data Void
x :: Val Int
x = Val 1
$ cat turing.hs
-- this is a finite memory tape with dereference and assign
import Control.Monad.State
import qualified Data.Map as Map
import Data.Word
type Ptr = Word64
-- a C-like array (char *) which maps int64 to int8