Skip to content

Instantly share code, notes, and snippets.

View wpcarro's full-sized avatar

William Carroll wpcarro

View GitHub Profile
@wpcarro
wpcarro / quick-sort.hs
Created October 14, 2018 21:32
Naive Haskell implementation of Quick Sort.
--------------------------------------------------------------------------------
import Data.Function ((&))
--------------------------------------------------------------------------------
import qualified Data.List as List
--------------------------------------------------------------------------------
main :: IO ()
main =
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3] & quickSort & show & putStrLn
-- | FP implementation of QuickSort. Intended as an exercise in a high-level
@wpcarro
wpcarro / Main.hs
Created October 24, 2018 20:18
WebSockets attempt
--------------------------------------------------------------------------------
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
--------------------------------------------------------------------------------
module Main
( main
) where
--------------------------------------------------------------------------------
import qualified Control.Concurrent as Concurrent
import qualified Control.Concurrent.Async as Async
;; Rewriting the example of a Finite State Machine using Clojure and
;; Haskell-inspired type annotations. Why mix the two languages? Firstly, I like
;; LISPs. Secondly, Haskell type-annotations help me organize my thoughts.
;;
;; This example was sourced from page 91 of Game Programming Patterns by Robert
;; Nystrom.
;; type State = Ducking
;; | Standing
;; | Jumping
(|>) :: a -> (a -> b) -> b
(|>) x f = f x
m n
| n > 100 = n - 10
| n <= 100 = n + 11 |> m |> m
main = do
m 101 |> show |> putStrLn
m 102 |> show |> putStrLn
@wpcarro
wpcarro / macros.el
Created August 4, 2019 20:25
More ergonomic way to add before-save-hook in ELisp
;; This macro hopefully provides a more ergonomic way to add mode-specific hooks before buffer save events.
;;
;; In ELisp you might see docs that tell you to write the following:
;; (add-hook 'reason-mode-hook (lambda () (add-hook 'before-save-hook #'refmt-before-save)))
;;
;; Instead of writing this, use this macro to write:
;; (add-hook-before-save 'reason-mode-hook #'refmt-before-save)
(defmacro add-hook-before-save
(mode f)
################################################################################
# 8-bit colors
################################################################################
black='\u001b[30m'
red='\u001b[31m'
green='\u001b[32m'
yellow='\u001b[33m'
blue='\u001b[34m'
magenta='\u001b[35m'
@wpcarro
wpcarro / runc.zsh
Last active August 7, 2019 11:02
Ergonomic way to quickly run individual C files.
runc() {
# Compile and run $1. Pass $1 as file.c.
# This is modelled after the `runhaskell` command.
# Deletes the compiled binary after executing it.
#
# depends gcc
gcc "$1" -o "${1%.c}" && "./${1%.c}" && rm "${1%.c}"
}
@wpcarro
wpcarro / runrust.zsh
Created August 7, 2019 11:07
Ergonomic way to run individual Rust files.
runrust() {
# Compile and run $1. Pass $1 as file.rs.
# This is modelled after the `runhaskell` command.
# Deletes the compiled binary after executing it.
#
# depends rustc
rustc "$1" && "./${1%.rs}" && rm "${1%.rs}"
}
@wpcarro
wpcarro / generate.py
Created August 9, 2019 16:00
Programming Pearls - disk sort
#!/usr/bin/env python3
import random
sample_size = 9000000
numbers = list(range(0, 10000000))
random.shuffle(numbers)
with open('numbers.txt', 'w') as f:
for i in numbers[0:sample_size]:
print('{}'.format(i))
@wpcarro
wpcarro / rotate.rs
Created August 12, 2019 09:03
Example implementation of rotating a vector. From "Programming Pearls" book.
// Example implementation of a vector rotation from the book, "Programming Pearls".
//
// The algorithm can be explained using your hands:
//
// +----------------+---------------+---------------+---------------+
// | Stack left over| Flip left hand| Flip right | Turn both |
// | right with | over. | hand over. | hands over |
// | palms facing | | | right-over- |
// | towards you | | | left. |
// +----------------+---------------+---------------+---------------+