Skip to content

Instantly share code, notes, and snippets.

@noughtmare
noughtmare / rbinom.c
Last active September 10, 2019 19:34
standalone version of rbinom
/*
* Mathlib : A C Library of Special Functions
* Copyright (C) 1998 Ross Ihaka
* Copyright (C) 2000-2014 The R Core Team
* Copyright (C) 2007 The R Foundation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
@noughtmare
noughtmare / rbinom.hs
Last active September 11, 2019 22:59
Haskell standalone rbinom
#!/usr/bin/env cabal
{- cabal:
build-depends: base, mwc-random
ghc-options: -with-rtsopts=-s -Wall
-}
{-
Copyright (C) 1998 Ross Ihaka
Copyright (C) 2000-2014 The R Core Team
Copyright (C) 2007 The R Foundation
module Main where
import qualified Data.Vector.Unboxed.Mutable as VM
import qualified Data.Vector.Unboxed as V
import Control.Monad.ST (runST)
import Control.Monad (when)
sieve :: Int -> V.Vector Bool
sieve n = runST $ do
vm <- VM.new n
@noughtmare
noughtmare / infinite-string-stream.hs
Last active August 27, 2020 12:58
Convert an `IO String` into an infinite string.
-- Run this example with runhaskell:
--
-- $ runhaskell infinite-string-stream.hs
--
-- Type in some text and press enter.
-- Repeat as long as you like.
-- At the end press CTRL-d to quit.
--
-- Using unsafeInterleaveIO the text you put in will appear immediately after
-- each new line. Without it, the output will only be printed after you quit
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import qualified Data.ByteString.Char8 as B
import System.IO
import Data.List
import qualified Data.Vector as V
import Data.Foldable
generateWords :: [B.ByteString] -> B.ByteString -> [B.ByteString]
{-# LANGUAGE BangPatterns #-}
module Main where
import qualified Data.ByteString as S -- S for strict (hmm...)
import qualified Data.ByteString.Internal as S
import qualified Data.ByteString.Unsafe as S
import qualified Data.ByteString.Lazy.Internal as L
import Foreign
import Gauge.Main
@noughtmare
noughtmare / Scope.hs
Created April 30, 2021 20:34
Examples from "Effect handlers in Scope" implemented with the EvEff package.
{-# LANGUAGE RankNTypes, TypeOperators, FlexibleContexts, UndecidableInstances, TypeApplications, LambdaCase, ScopedTypeVariables, BlockArguments, DeriveFunctor, KindSignatures, ExistentialQuantification, FlexibleContexts #-}
{-# OPTIONS_GHC -Wall -Wno-orphans #-}
module Control.Ev.Scope where
import Control.Applicative
import Control.Ev.Eff
import Control.Monad
import Data.Foldable
import Data.Functor ( (<&>) )
import Debug.Trace
@noughtmare
noughtmare / Weave.hs
Created May 25, 2021 11:47
An implementation and extension of ideas from "Weaving a Web" by Hinze and Jeuring.
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}
module Weave
( Weaver
, cons
@noughtmare
noughtmare / Main.hs
Created June 21, 2021 18:46
Testing concurrency of unsafe ffi calls
import Foreign.C
import Control.Concurrent
import System.IO.Unsafe
import Data.Foldable
foreign import ccall unsafe "keep_sleeping" c_keep_sleeping :: CInt -> IO CInt
{-# NOINLINE children #-}
children :: MVar [MVar ()]
children = unsafePerformIO (newMVar [])
@noughtmare
noughtmare / ASD.hs
Last active August 28, 2021 10:24
Implementation of arrowised parser from Hughes' paper: "Generalising Monads to Arrows", based on ideas by Swierstra and Duponcheel.
import Prelude hiding (id, (.))
import Control.Arrow
import Control.Category
import qualified Data.List as List
data StaticParser s = SP Bool [s]
newtype DynamicParser s a b = DP ((a, [s]) -> (b, [s]))
data Parser s a b = P (StaticParser s) (DynamicParser s a b)
instance Eq s => Category (Parser s) where