Created
February 6, 2011 17:53
-
-
Save sprsquish/813561 to your computer and use it in GitHub Desktop.
Very basic, very crude ring buffer in haskell
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 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