Advertisement
banovski

List numberer

Oct 9th, 2022 (edited)
2,737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 1.16 KB | Source Code | 0 0
  1. -- Copies newline-separated text from the clipboard, replaces tab
  2. -- characters with rightwards arrows surrounded by spaces, numbers the
  3. -- lines, replaces the original text in the clipboard with the output.
  4.  
  5. import System.Clipboard as SC
  6.  
  7. main :: IO ()
  8. main = do
  9.   input <- SC.getClipboardString
  10.   if input == Nothing
  11.     then SC.setClipboardString "There was nothing in the clipboard!"
  12.     else SC.setClipboardString $ processData input
  13.  
  14. processData :: Maybe String -> String
  15. processData = numberedListItemsToString . numberListItems . replaceTabsWithArrows . inputToListOfStrings
  16.  
  17. inputToListOfStrings :: Maybe String -> [String]
  18. inputToListOfStrings Nothing = ["Nothing!"]
  19. inputToListOfStrings (Just string) = lines string
  20.  
  21. replaceTabsWithArrows :: [String] -> [String]
  22. replaceTabsWithArrows = map aux
  23.   where
  24.     aux [] = []
  25.     aux (x:xs)
  26.       | x == '\t' = ' ' : '→' : ' ' : aux xs
  27.       | otherwise = x : aux xs
  28.  
  29. numberListItems :: [String] -> [(Int, String)]
  30. numberListItems = zip [1 ..]
  31.  
  32. numberedListItemsToString :: [(Int, String)] -> String
  33. numberedListItemsToString = unlines . map (\(number, string) -> show number ++ ". " ++ string)
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement