Skip to content

Instantly share code, notes, and snippets.

View regiskuckaertz's full-sized avatar

Regis Kuckaertz regiskuckaertz

View GitHub Profile
@regiskuckaertz
regiskuckaertz / arrival.hs
Last active March 20, 2017 16:36
Template compiler in Haskell
--- We just read whatever comes from standard input and spit out its conversion result
main = interact louise
--- Our runner will just pipe the three components together:
louise :: String -> String
louise html = let result = parseHTML html >>= html2js >>= writeJS in
case result of
Right ok -> ok
Left nok -> "Error: " ++ nok
@regiskuckaertz
regiskuckaertz / dom-zipper.hs
Last active March 22, 2017 13:50
Zipper for a DOM-like tree data type
data Tree a = Leaf a | Node a [Tree]
type Crumb a = (a, [Tree a], [Tree a])
type Breadcrumbs a = [Crumb a]
type Zipper a = (Tree a, Breadcrumbs a)
goDown :: Zipper a -> Int -> Zipper a
goDown (Node el children, bs) n = let (head, nth:tail) = splitAt children n
in (nth, Crumb el head tail : bs)
@regiskuckaertz
regiskuckaertz / copy.js
Created July 28, 2017 09:31
Object.prototype.copy
Object.defineProperty(
Object.prototype,
'copy',
{ value: function(spec) {
return Object.freeze(Object.assign({}, this, spec));
}
}
);
@regiskuckaertz
regiskuckaertz / stripmargin.js
Created August 4, 2017 12:21
Scala's stripMargin
function stripMargin(sts, ...vals) {
let tts = sts[0];
if( vals.length ) {
tts = sts.reduce((s, i) => s + vals[i], '');
}
return tts.split('\n')
.map(s => s.trimLeft().startsWith('|')
? s.trimLeft().substr(1)
: s)
package humbug
package codecs
sealed abstract class Type[T]
case object TyBool extends Type[Boolean]
case object TyByte extends Type[Byte]
case object TyDouble extends Type[Double]
case object TyI16 extends Type[Short]
case object TyI32 extends Type[Int]
case object TyI64 extends Type[Long]
@regiskuckaertz
regiskuckaertz / Main.purs
Created February 18, 2018 09:24
Instance function not in scope?
module Main where
import Prelude
import Data.Either (Either(..))
import Data.Profunctor (class Profunctor, arr)
import Data.Profunctor.Choice (class Choice)
import Data.Profunctor.Strong (second)
import Data.Tuple (Tuple(..))
-- State transformers
module Main where
--------------------------------------------------------------------------------
import Data.HeytingAlgebra
import Prelude
import Data.Array (filter, any, (:), uncons)
import Data.Either (Either(..))
import Data.Maybe (Maybe(..))
import Data.Profunctor (class Profunctor, arr)
import Data.Profunctor.Choice (class Choice, left)
import Control.Monad.State
-- first version
toh n = fst $ tohmoves n (n, 0, 0)
tohmoves :: Int -> (Int, Int, Int) -> (Int, (Int, Int, Int))
tohmoves 1 (a, b, c) = (1, (a - 1, b, c + 1))
tohmoves n (a, b, c) =
let (n', (a', c', b')) = tohmoves (n - 1) (a, c, b)
(n'', (a'', b'', c'')) = tohmoves 1 (a', b', c')
package example
import cats._
import cats.free._
import cats.data._
import cats.syntax.flatMap._
import cats.effect.{LiftIO, IO}
object Example {
import functors._
series :: Int -> [Int]
series n = go where go = n : (zipWith (+) go $ fmap mirror go)
mirror :: Int -> Int
mirror = undigits . digits
digits :: Int -> [Int]
digits = map (`mod` 10) . takeWhile (/= 0) . iterate (`div` 10)
undigits :: [Int] -> Int