Skip to content

Instantly share code, notes, and snippets.

@passingloop
Created October 18, 2011 01:24
Show Gist options
  • Save passingloop/1294391 to your computer and use it in GitHub Desktop.
Save passingloop/1294391 to your computer and use it in GitHub Desktop.
7l7w-haskell-day-2
module Passingloop where
import Char
wrapJoin :: Int -> (String, Int) -> (String, Int) -> (String, Int)
wrapJoin n (str, lineLength) (word, length)
| lineLength + length < n = (str ++ " " ++ word, lineLength + length)
| otherwise = (str ++ "\n" ++ word, length)
wrap :: Int -> String -> String
wrap n str = fst $ foldl1 (wrapJoin n) [ (w, length w) | w <- words str]
number :: Int -> String -> String
number n l = [intToDigit n] ++ ": " ++ l
numberLines :: String -> String
numberLines str = unlines $ zipWith number [1 ..] (lines str)
justify :: Int -> String -> String
justify n l = take n $ l ++ repeat '_'
alignLeft :: Int -> (String -> String)
alignLeft n = unlines . (map (justify n)) . lines
alignRight :: Int -> (String -> String)
alignRight n = unlines . (map (reverse . (justify n) . reverse)) . lines
padding :: Int -> String -> String
padding n str = (take ((n - length str) `div` 2) $ repeat '_') ++ str
center :: Int -> (String -> String)
center n = unlines . (map ((justify n) . (padding n))) . lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment