Skip to content

Commit

Permalink
WIP on randomPhrase function
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelBoucey committed Nov 21, 2024
1 parent 9c40c8f commit 6495120
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions Spintax.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ library
other-modules: Text.Spintax.RandomPhrase
build-depends: attoparsec >= 0.12.1.6 && < 0.15
, base >= 4.7 && < 5
, either >= 5 && < 6
, extra >= 1.4.3 && < 1.9
, mtl >= 2.2.1 && < 2.4
, mwc-random >= 0.13.3.2 && < 0.16
Expand Down
12 changes: 9 additions & 3 deletions src/Text/Spintax/RandomPhrase.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
module Text.Spintax.RandomPhrase where

import qualified Data.Text as T
import Data.Either.Combinators (mapRight)
import qualified Data.Text as T
import Text.Spintax

type RandomPhrase = T.Text
newtype RandomPhrase = RandomPhrase { unRandomPhrase :: T.Text }

instance Show RandomPhrase where
show (RandomPhrase t) = show t

-- | Generate random passphrase or unique id
--
-- >λ> randomPhrase "-" [["blacky","monk","gillespie","coltrane"],["apple","apricot","banana","coconut"],["kant","hegel","husserl","habermas"]]
-- > Right "coltrane-coconut-kant"
--
randomPhrase :: T.Text -> [[T.Text]] -> IO (Either String RandomPhrase)
randomPhrase s ls = spintax $ writeSpintaxExpression s $ writeSpintaxAlternative <$> ls
randomPhrase s ls = do
r <- spintax $ writeSpintaxExpression s $ writeSpintaxAlternative <$> ls
pure (mapRight RandomPhrase r)

0 comments on commit 6495120

Please sign in to comment.