This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
module Parser where | |
import Data.Char | |
-- | Built-in functions in our language: | |
data BuiltFn | |
= Nat Integer -- ^ Natural number | |
deriving (Eq, Ord, Read, Show) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class None<T> | |
extends AbstractCollection<T> | |
implements Option<T> | |
{ | |
public None() {} | |
public T get() { | |
throw new IllegalArgumentException("No value"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
OlderNewer