Sha256: a2e70bc8fc5bef5e552e2ec0ba5053fff6ffa6671937a22749ec30ec54260110

Contents?: true

Size: 1.17 KB

Versions: 268

Compression:

Stored size: 1.17 KB

Contents

module AtbashCipher
  ( decode
  , encode
  ) where

import Prelude
import Data.Array (drop, filter, take, zip, (:))
import Data.Char.Unicode (isPunctuation, isSpace)
import Data.Map (Map, fromFoldable, lookup)
import Data.Maybe (Maybe)
import Data.String (fromCharArray, joinWith, toCharArray, toLower)
import Data.Traversable (traverse)

plain :: Array Char
plain = toCharArray "abcdefghijklmnopqrstuvwxyz0123456789"

cipher :: Array Char
cipher = toCharArray "zyxwvutsrqponmlkjihgfedcba0123456789"

decMap :: Map Char Char
decMap = fromFoldable (zip cipher plain)

encMap :: Map Char Char
encMap = fromFoldable (zip plain cipher)

decode :: String -> Maybe String
decode s = fromCharArray <$> dec (toCharArray s)
  where dec = filter ((/=) ' ')
              >>> traverse (_ `lookup` decMap)

chunks :: forall a. Int -> Array a -> Array (Array a)
chunks n [] = []
chunks n a = take n a : chunks n (drop n a)

encode :: String -> Maybe String
encode s = group <$> enc (toLower s # toCharArray)
  where enc = filter (not (isPunctuation || isSpace))
              >>> traverse (_ `lookup` encMap)
        group = chunks 5
                >>> map fromCharArray
                >>> joinWith " "

Version data entries

268 entries across 268 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.158 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.157 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.156 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.155 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.154 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.153 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.152 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.151 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.150 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.149 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.148 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.147 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.146 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.145 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.144 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.143 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.142 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.141 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs
trackler-2.2.1.140 tracks/purescript/exercises/atbash-cipher/examples/src/AtbashCipher.purs