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
package main | |
import ( | |
"flag" | |
"fmt" | |
"github.com/elazarl/goproxy" | |
"log" | |
"net/http" | |
"os" | |
) |
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 SwapElts where | |
-- If you have to use this function, arrays may be a better choice. | |
swapElts i j ls = [get k x | (k, x) <- zip [0..length ls - 1] ls] | |
where get k x | k == i = ls !! j | |
| k == j = ls !! i | |
| otherwise = x | |
-- This is a nice example of how to efficiently generate test cases. | |
-- A naive approach would have been to take separate arguments for |
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 runhaskell | |
{-# LANGUAGE TemplateHaskell #-} | |
import Test.QuickCheck ((==>), Property) | |
import Test.QuickCheck.All (quickCheckAll) | |
-- main = interact $ show . checkBalance | |
main = $quickCheckAll | |
checkBalance :: String -> Bool |
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
class Heap(object): | |
def __init__(self, size): | |
self.num = 0 | |
self.size = size | |
self.data = [None] * size | |
def __repr__(self): | |
return '<Thing: %s>' % (self.data,) | |
def insert(arr, x): |
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 Data.Bits ((.&.)) | |
import Test.QuickCheck (Property, (==>)) | |
prop_bits :: Int -> Property | |
prop_bits n = n > 0 ==> intToBool (n .&. (n - 1)) == not (isPowerOfTwo n) | |
intToBool 0 = False | |
intToBool _ = True | |
isPowerOfTwo :: Int -> Bool |
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 runhaskell | |
import Data.List (sort, (\\)) | |
import Test.QuickCheck (Property, (==>)) | |
main = interact (showLn . median . map read . lines) | |
showLn :: Show a => a -> String | |
showLn x = show x ++ "\n" |
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
-- cabal install MonadRandom | |
-- ghc random_monad_example | |
-- ./random_monad_example | |
-- The code here is stolen from a comment in the MonadRandom source code. | |
import Control.Monad.Random | |
die :: RandomGen g => Rand g Int | |
die = getRandomR (1,6) |
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
package main | |
import ( | |
"reflect" | |
"sort" | |
"testing" | |
"testing/quick" | |
) | |
type IntSlice []int |
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 runhaskell | |
-- Example of tailing a file, using two communicating Haskell threads. It's not | |
-- really necessary to do this concurrently, but the approach here generalizes | |
-- nicely if we want to filter or transform the output. | |
import Control.Concurrent | |
import Control.Monad | |
import System.Environment | |
import System.Exit |
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 runhaskell | |
-- This example uses the hslogger library. | |
-- For debugging it may be more convenient to use Debug.Trace instead since that | |
-- allows you to log debugging output from otherwise pure functions. | |
import System.IO (stderr, Handle) | |
import System.Log.Logger (rootLoggerName, setHandlers, updateGlobalLogger, | |
Priority(INFO), Priority(WARNING), infoM, debugM, | |
warningM, errorM, setLevel) |