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.134 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.133 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.132 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.131 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.130 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.129 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.128 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.127 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.126 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.125 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.124 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.123 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.122 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.121 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.120 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.119 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.118 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.117 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.116 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs
trackler-2.2.1.115 tracks/haskell/exercises/series/examples/success-byteseqs/src/Series.hs