Skip to content

Instantly share code, notes, and snippets.

Avatar

Dimitri DeFigueiredo dimitri-xyz

View GitHub Profile
@dimitri-xyz
dimitri-xyz / RejectionSampling.hs
Created Apr 20, 2017
A non-biased way to generate random integers in a given range
View RejectionSampling.hs
import Control.Monad.Random.Class
import System.Random
import Data.Word
maxIterations :: Int
maxIterations = 100
acceptReject :: (Random a, MonadRandom m) => (a -> Bool) -> (a -> b) -> m b
acceptReject = acceptReject' (Just maxIterations)
View lift-pipes-example.hs
import System.IO
import Control.Monad
import Control.Monad.IO.Class
import Control.Exception.Base (evaluate)
import Pipes
import Pipes.Prelude (stdoutLn)
withCSV :: FilePath -> (Handle -> IO r) -> IO r
withCSV path action = do
View auto-lift-example.hs
-----------------------------------
import System.IO
import Control.Monad.Reader
import Control.Monad.IO.Class
import Control.Exception.Base (evaluate)
data ColumnHeaders = FirstLine | None
withCSV :: FilePath -> (Handle -> IO r) -> IO r
withCSV path action = do
@dimitri-xyz
dimitri-xyz / merging2.hs
Last active Oct 13, 2015
Merging 2 branches - function definition depends on constant definition
View merging2.hs
-- base
-------
c :: Int
c = 5
plusc :: Int -> Int
plusc x = x + c
main = putStrLn $ "the total is: " ++ show (plusc 1)
@dimitri-xyz
dimitri-xyz / merging.hs
Last active Oct 13, 2015
Merging 2 branches - function definition does NOT depend on constant
View merging.hs
-- base
-------
c :: Int
c = 5
plusone :: Int -> Int
plusone x = x + 1
main = putStrLn $ "the total is: " ++ show (plusone c)