Advertisement
ATuin

Palindrome in scala using Scalaz - v2

May 14th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.78 KB | None | 0 0
  1. package logikujo.atuin.FunctionalSamples
  2.  
  3. import scalaz._
  4. import Scalaz._
  5. import scalaz.effects._
  6.  
  7. object App {
  8.   def main(args: Array[String]):Unit = Palindrome.run
  9. }
  10.  
  11. object Palindrome {
  12.   def run:Unit = loop().unsafePerformIO
  13.  
  14.   def loop(a:Unit):IO[Unit] =
  15.     showBanner >>=| readLn >>= palindrome >>= showResult >>= loop
  16.  
  17.   def showBanner:IO[Unit] = for {
  18.     _ <- putStrLn("Little example to test if a line is a palindrome:")
  19.     _ <- putStrLn("Enter a line:?")
  20.   } yield ()
  21.  
  22.   def palindrome(s:String):IO[Option[Boolean]] =
  23.     io{(s != "quit") option ((s.reverse == s) ?? true)}
  24.  
  25.   def showResult(o:Option[Boolean]):IO[Unit] =
  26.     o.some(_ ? (putStrLn("Is a palindrome!")) |
  27.         (putStrLn("Is NOT a palindrome :(, try it again!"))).none(io{System.exit(0)})
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement