Skip to content

Instantly share code, notes, and snippets.

@sprsquish
Created February 6, 2011 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sprsquish/813561 to your computer and use it in GitHub Desktop.
Save sprsquish/813561 to your computer and use it in GitHub Desktop.
Very basic, very crude ring buffer in haskell
import Prelude hiding (read)
import qualified Data.Map as Map
type SeqCounter = Int
type MaxBound = Int
data Ring a = Ring (Map.Map Int a) SeqCounter MaxBound
newRing :: Int -> Ring a
newRing mb = Ring Map.empty 0 mb
write :: v -> Ring v -> Ring v
write v ring = Ring nm nc m
where
(Ring rm c m) = ring
nc = c + 1
nm = Map.insert (nc `mod` m) v rm
read :: Ring m -> Int -> Maybe m
read ring n = Map.lookup (n `mod` mb) rm
where
(Ring rm _ mb) = ring
main = do
putStrLn $ show vals
where
mb = 10
fullRing = foldr write (newRing mb) ['z','y'..'a']
vals = map (read fullRing) [0..mb * 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment