Skip to content

Instantly share code, notes, and snippets.

View ppetr's full-sized avatar
🤞
I may be slow to respond.

Petr ppetr

🤞
I may be slow to respond.
View GitHub Profile
@ppetr
ppetr / Pause.lhs
Created August 31, 2012 13:01
An implementation of a Pause monad from http://stackoverflow.com/q/10236953/1333025
http://stackoverflow.com/q/10236953/1333025
I quite enjoyed this exercise. I tried to do it without looking at the answers,
and it was worth it. It took me considerable time, but the result is
surprisingly close to two of the other answers, as well as to
[monad-coroutine](http://hackage.haskell.org/package/monad-coroutine) library.
So I guess this is somewhat natural solution to this problem. Without this
exercise, I wouldn't understand how _monad-coroutine_ really works.
To add some additional value, I'll explain the steps that eventually led me to
@ppetr
ppetr / CoerceUsingUnsafe.hs
Created September 10, 2012 19:44
An example how to convert any type to any other using Haskell's unsafePerformIO
module CoerceUsingUnsafe where
import Data.IORef
import System.IO.Unsafe (unsafePerformIO)
import Data.Word
{- The trick behind this function is in the `test` experession.
When we create a new mutable variable in the IO monad,
it has a side effect of creating it somewhere in the memory.
@ppetr
ppetr / Main.dump-simpl
Created October 28, 2012 05:23
Is it possible to make GHC optimize (deforest) generic functions such as catamorphisms?
Result size = 325
Main.$fFunctorTreeT_$cfmap
:: forall a_ajJ b_ajK.
(a_ajJ -> b_ajK) -> Main.TreeT a_ajJ -> Main.TreeT b_ajK
[GblId,
Arity=2,
Caf=NoCafRefs,
Str=DmdType LS,
Unf=Unf{Src=<vanilla>, TopLvl=True, Arity=2, Value=True,
@ppetr
ppetr / md5quine.py
Created November 9, 2012 12:13
Cryptographic quine - a program that prints out the MD5 sum of its source.
#!/usr/bin/env python
import hashlib
import sys
input = ['#!/usr/bin/env python', 'import hashlib', 'import sys', 'try: input', 'except NameError: input = []', 'try: trans', "except NameError: trans = lambda x: 'MD5 sum of my source is: ' \\", ' + hashlib.md5(x).hexdigest()', 'lines = input', 'lines.insert(3, "input = " + repr(input))', "out = '\\n'.join(lines) + '\\n'", 'print trans(out),']
try: input
except NameError: input = []
try: trans
except NameError: trans = lambda x: 'MD5 sum of my source is: ' \
+ hashlib.md5(x).hexdigest()
lines = input
@ppetr
ppetr / Parser.y
Last active December 10, 2015 23:59 — forked from anonymous/Parser.y
A small parser for a simple exercise functional language. Example: `(fix fact . \x . ifzero x 1 (mul x (fact (minus x 1)))) 10`.
{
module Parser where
import Data.Char
-- | Built-in functions in our language:
data BuiltFn
= Nat Integer -- ^ Natural number
deriving (Eq, Ord, Read, Show)
@ppetr
ppetr / Feedback.hs
Last active December 14, 2015 15:49
Alternate approach to conduit's leftovers.
import Control.Monad
import Data.Conduit.Internal
import Data.Void
import Data.Sequence
type PipeF i o u m r = Pipe Void i (Either i o) u m r
-- | Implements feedback for a `PipeF`, converting it to `Pipe`.
-- Any leftover feedback not consumed by the pipe (or produced after its
/*
BSD3 license
------------
Copyright (c) 2013, Petr Pudlák
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
@ppetr
ppetr / CloseExample.scala
Created June 21, 2013 14:13
An example of using scala-conduit for reading a file util a given character is found. See https://github.com/ppetr/scala-conduit
import java.nio._
import java.nio.channels.Channels
import java.io.{ FileInputStream, IOException }
import java.util.zip._
import scala.util.control.Exception._
import conduit._
import conduit.Pipe._
object CloseExample extends App {
/**
public final class None<T>
extends AbstractCollection<T>
implements Option<T>
{
public None() {}
public T get() {
throw new IllegalArgumentException("No value");
}
{-# LANGUAGE RankNTypes #-}
import Control.Monad
import Control.Monad.State
import Control.Monad.Writer
import Data.Char (intToDigit)
import Data.Machine
import Data.Machine.Plan
import Data.Machine.Source
import Data.Monoid