Advertisement
banovski

Unltracolumnizer

Sep 12th, 2024 (edited)
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 1.54 KB | Source Code | 0 0
  1. -- The app reads text from the clipboard -- the text is two columns of
  2. -- words separated by tab characters -- removes the first column and
  3. -- the tab characters, splits the remaining text into single letters,
  4. -- removes duplicates, shuffles the letters and capitalizez the
  5. -- letters. The result is stored in the clipboard.
  6.  
  7. import System.Hclip (getClipboard, setClipboard)
  8. import Data.List (dropWhile, intersperse, nub, transpose)
  9. import Data.Char (toUpper)
  10. import System.Exit (die)
  11.  
  12. main :: IO ()
  13. main = do
  14.   input <- getClipboard
  15.   if null input
  16.     then die "The clipboard is empty!"
  17.     else setClipboard $
  18.          insertNewLines $
  19.          stringToUppers $
  20.          stringShuffler $ reverse $ nub $ concatMap dropAtTab (lines input)
  21.  
  22. dropAtTab :: String -> String
  23. dropAtTab [] = []
  24. dropAtTab string = tail $ dropWhile (/= '\t') string
  25.  
  26. stringToUppers :: String -> String
  27. stringToUppers [] = []
  28. stringToUppers string = map toUpper string
  29.  
  30. insertNewLines :: String -> String
  31. insertNewLines [] = []
  32. insertNewLines string = intersperse '\n' string
  33.  
  34. stringShuffler :: String -> String
  35. stringShuffler string =
  36.   concat $ transpose $ chunksOf (transposableSize string) string
  37.   where
  38.     transposableSize lst = roundedUpSquareRoot $ length lst
  39.  
  40. roundedUpSquareRoot :: (Integral t, Integral b) => t -> b
  41. roundedUpSquareRoot x = truncate $ sqrt x_double
  42.   where
  43.     x_double = fromIntegral x :: Double
  44.  
  45. chunksOf :: Int -> [a] -> [[a]]
  46. chunksOf _ [] = []
  47. chunksOf size string = take size string : chunksOf size (drop size string)
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement