Skip to content

Instantly share code, notes, and snippets.

View mbloms's full-sized avatar

Mikael Blomstrand mbloms

View GitHub Profile
@mbloms
mbloms / annat.sh
Last active September 18, 2017 12:26
IPOP Lab1
sudo systemctl disable NetworkManager.service
sudo systemctl disable systemd-networkd
@mbloms
mbloms / byt_root.md
Last active October 4, 2017 08:42
Laga btrfs

Hur man byter root

låt @ny = läsbar snapshot av den man vill använda

sudo mv @ @bad; sudo mv @ny @
reboot
@mbloms
mbloms / ttcp.c
Created October 5, 2017 09:40
TCP benchmarking tool
/*
* T T C P . C
*
* Test TCP connection. Makes a connection on port 5001
* and transfers fabricated buffers or data copied from stdin.
*
* Usable on 4.2, 4.3, and 4.1a systems by defining one of
* BSD42 BSD43 (BSD41a)
* Machines using System V with BSD sockets should define SYSV.
*
@mbloms
mbloms / funkyProduct.hs
Last active December 2, 2017 14:03
En kvällskluring; implementera en funktion som gör följande: [2 3 5 10] -> [150 100 60 30] (varje tal i outputen är produkten av hela inputen, bortsett från talet på samma index) Naiv lösning är trivial, men om lösningen INTE får vara O(n^2) och INTE får använda division? Hur smart kan man göra den?
funkyProduct lst = zipWith
(*)
(scanl (*) 1 lst)
(scanr (*) 1 $ (tail lst))
--Prelude> funkyProduct [2,3,5,10]
--[150,100,60,30]
@mbloms
mbloms / bfix.hs
Created December 15, 2017 00:50
Comonadic fixed point
-- Normal fixed point
fix f = f (fix f)
-- | Slow comonadic fixed point à la Kenneth Foner:
pfix :: Comonad w => w (w a -> a) -> w a
pfix = extend wfix
-- | Comonadic fixed point à la Kenneth Foner:
kfix :: ComonadApply w => w (w a -> a) -> w a
kfix w = fix $ \u -> w <@> duplicate u
@mbloms
mbloms / Expression.hs
Last active December 15, 2017 13:38
Expression som comonad
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ViewPatterns #-}
import Grammar (BinOp, Identifier, Type, HashMap)
import Typed
import Control.Comonad
import Control.Applicative (liftA2)
data Expression t
= BinOp {lhs :: Expression t, op :: BinOp, rhs :: Expression t, typ :: t}
| MethodCall (Expression t) Identifier [Expression t] t
@mbloms
mbloms / coscan.hs
Last active December 16, 2017 13:45
ComonadScan
class Comonad w => ComonadScan w where
scanW :: (a -> w b -> a) -> a -> w b -> w a
scanW f acc w = (duplicate w)
instance ComonadScan Tree where
scanW f acc w@(Node _ as) = let acc' = f acc w in Node acc' (map (scanW f acc') as)
@mbloms
mbloms / galet.hs
Created December 23, 2017 23:24
Tree parameterized over the root, with an arbitrary type in the child-nodes.
{-# LANGUAGE ExistentialQuantification #-}
import Data.List (unlines)
data HasEq = forall a. (Eq a, Show a) => HasEq a
data AnyIntegral = forall a. Integral a => Integral a
data AnyTree x = forall a b. (Show a, Show b) => Tree x (AnyTree a) (AnyTree b) | Nil
instance Show a => Show (AnyTree a) where
show (Tree x a b) = show (x,(a,b))
show Nil = "Nil"
@mbloms
mbloms / day1.hs
Created December 23, 2017 23:27
Simple problem from advent of code 2017. Solved as complicated as possible in as few lines as possible.
import Data.Monoid
import Data.Char (digitToInt)
import Data.Foldable (foldrM)
--- Part One ---
-- Bind using the reader monad:
-- x >>= k = \w-> k (x w) w
-- head >>= foldrM bar = \w -> foldrM bar (head w) w
foo = head >>= foldrM bar
@mbloms
mbloms / algebra.hs
Last active December 23, 2017 23:33
Vectors and Matrices type parameterised over their length.
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
import Prelude hiding (tail, head)