Skip to content

Instantly share code, notes, and snippets.

@el-hult
el-hult / comonadVectorDiff.hs
Created May 7, 2019 19:46
Finding right angles between 2D vectors using comonads
-- This is using a haskell comonad to find right angles between given geometrical vectors
-- The code is kind of overkill, but it is a cute showcase of comonads
-- a FocusSet. One element in Focus, and all other elements in a list.
data Fs a = Fs a [a] deriving (Show)
instance Functor Fs where
fmap fun (Fs k l) = Fs (fun k) (map fun l)
@el-hult
el-hult / comonadVectorDiff.hs
Created May 7, 2019 19:46
Finding right angles between 2D vectors using comonads
-- This is using a haskell comonad to find right angles between given geometrical vectors
-- The code is kind of overkill, but it is a cute showcase of comonads
-- a FocusSet. One element in Focus, and all other elements in a list.
data Fs a = Fs a [a] deriving (Show)
instance Functor Fs where
fmap fun (Fs k l) = Fs (fun k) (map fun l)
@el-hult
el-hult / gameOfLife.hs
Created May 7, 2019 19:37
1D Game of Life in the terminal
-- http://blog.sigfpe.com/2006/12/evaluating-cellular-automata-is.html
-- The above post learned me about this neat comonad instance! :D
data U x = U [x] x [x]
right,left:: U x -> U x -- shift focus of the zipper
right (U a b (c:cs)) = U (b:a) c cs
left (U (a:as) b c) = U as a (b:c)
-- the zipper is a functor
@el-hult
el-hult / day4.html
Last active December 27, 2018 14:06
advent of code 2017 day 4 html page.
<!doctype html><html><head><meta charset="utf-8"/><script>
const isValid = raw => {
const words = raw.split(/\s+/)
// writeToBody("raw: " + raw)
// writeToBody("words: " + words)
const checker = (prevAcc, nextWord) => {
const acc = { set: prevAcc.set, count: prevAcc.count };
if( acc.set.has(nextWord) ) { acc.count = acc.count + 1 }
else { acc.set.add(nextWord) }