Sha256: 13aa6dd4761ec1ffc379687bf2eb5722dbde154b922e541353c7a511dd88f497

Contents?: true

Size: 868 Bytes

Versions: 337

Compression:

Stored size: 868 Bytes

Contents

module Series (slices) where

import Control.Applicative   (liftA2)
import Control.Monad         (guard)
import Data.ByteString.Char8 (ByteString, foldr, length, tails, take)
import Data.Function         (on)
import Data.Maybe            (mapMaybe)
import Data.Sequence         (Seq, empty, fromList, singleton, (<|))
import Prelude        hiding (foldr, length, take)

slices :: Num a => Int -> ByteString -> Seq (Seq a)
slices 0 _  = singleton empty
slices n bs = fromList
            . mapMaybe toDigits
            . takeWhile ((== n) . length)
            . map (take n)
            . tails
            $ bs

toDigits :: Num a => ByteString -> Maybe (Seq a)
toDigits = foldr (liftA2 (<|) . toDigit) (Just empty)

toDigit :: Num a => Char -> Maybe a
toDigit x = guard (0 <= dec && dec <= 9) >> return (fromIntegral dec)
  where
    dec = ((-) `on` fromEnum) x '0'

Version data entries

337 entries across 337 versions & 1 rubygems

Version Path
trackler-2.2.1.53 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.52 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.51 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.50 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.49 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.48 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.47 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.46 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.45 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.44 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.43 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.42 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.41 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.40 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.39 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.38 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.37 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.36 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.35 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.34 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs