Skip to content
Lib.hs 464 B
Newer Older
module Lib ( boundedPushRight
           ) where

import qualified Data.Sequence as S

-- | use Seq as a ring buffer. push new item to Seq on the right, if length is
-- greater than given size, remove item from the left, so that size is always
-- within maxSize.
boundedPushRight :: S.Seq a -> Int -> a -> S.Seq a
boundedPushRight buffer maxSize item =
    if S.length buffer == maxSize
    then
        S.drop 1 buffer S.|> item
        buffer S.|> item