Skip to content

Instantly share code, notes, and snippets.

@joaofnds
Created November 9, 2018 20:25
Show Gist options
  • Select an option

  • Save joaofnds/c007d9cd522b1ff2d49362308a48aead to your computer and use it in GitHub Desktop.

Select an option

Save joaofnds/c007d9cd522b1ff2d49362308a48aead to your computer and use it in GitHub Desktop.
-- Drop while matches Char
dropInitialMatches :: Char -> String -> String
dropInitialMatches c xs = dropWhile (==c) xs
-- Drop preceding spaces
dropInitialSpaces :: String -> String
dropInitialSpaces xs = dropInitialMatches ' ' xs
-- Drop preceding linefeeds
dropInitialNewLines :: String -> String
dropInitialNewLines xs = dropInitialMatches '\n' xs
-- Drop while matches Chars reversed
dropLeadingMatches :: Char -> String -> String
dropLeadingMatches c xs = reverse (dropInitialMatches c (reverse xs))
-- Drop leading spaces
dropLeadingSpaces :: String -> String
dropLeadingSpaces xs = dropLeadingMatches ' ' xs
-- Drop leading linefeed chars
dropLeadingNewLines :: String -> String
dropLeadingNewLines xs = dropLeadingMatches '\n' xs
-- Drop while differs from Char
dropWhileDiff :: Char -> String -> String
dropWhileDiff c xs = dropWhile (/=c) xs
-- Drop until the first space
dropUntilSpace :: String -> String
dropUntilSpace xs = dropWhileDiff ' ' xs
-- Drop until linefeed
dropUntilNewLine :: String -> String
dropUntilNewLine xs = dropWhileDiff '\n' xs
-- Drop while differs from Char
takeWhileDiff :: Char -> String -> String
takeWhileDiff c xs = takeWhile (/=c) xs
-- Take until the first space
takeUntilSpace :: String -> String
takeUntilSpace xs = takeWhileDiff ' ' xs
-- Take until the first linefeed
takeUntilNewLine :: String -> String
takeUntilNewLine xs = takeWhileDiff '\n' xs
-- Take the first word out of a String
takeWord :: String -> String
takeWord xs = takeUntilSpace (dropInitialSpaces xs)
-- Take the first line out of a String
takeLine :: String -> String
takeLine xs = takeUntilNewLine (dropInitialNewLines xs)
-- Remove the first word out of a String
removeWord :: String -> String
removeWord xs = dropInitialSpaces (dropUntilSpace (dropInitialSpaces xs))
-- Remove the first Line out of a String
removeLine :: String -> String
removeLine xs = dropInitialNewLines (dropUntilNewLine (dropInitialNewLines xs))
-- Remove redundant spaces
compressSpaces :: String -> String
compressSpaces [] = []
compressSpaces [' ',' '] = [' ']
compressSpaces (x:xs)
| (x == ' ') && ((head xs) == ' ') = compressSpaces xs
| otherwise = x:(compressSpaces xs)
-- Remove redundant linefeeds
compressNewLines :: String -> String
compressNewLines [] = []
compressNewLines ['\n','\n'] = ['\n']
compressNewLines (x:xs)
| (x == '\n') && ((head xs) == '\n') = compressNewLines xs
| otherwise = x:(compressNewLines xs)
-- Split a string by words
splitByWords :: String -> [String]
splitByWords [] = [[]]
splitByWords xs = (takeWord xs):(splitByWords (removeWord xs))
-- Split a string by lines
splitByLine :: String -> [String]
splitByLine [] = [[]]
splitByLine xs = (takeLine xs):(splitByLine (removeLine xs))
-- Remove redundant chars
sanitizeString :: String -> String
sanitizeString xs = dropLeadingSpaces (dropInitialSpaces (compressSpaces xs))
-- Split by lines removing redundant chars
splitByLineSanitize :: String -> [String]
splitByLineSanitize xs = init (map sanitizeString (splitByLine xs))
-- Given a single string text, returns an array of lines([string]), each contaning
-- an array of words. No spaces or linefeeds, just words.
textToWords :: String -> [[String]]
textToWords xs = map (\ a -> init (splitByWords a)) (splitByLineSanitize xs)
-- Calculate the amount of spaces needed to join a line splited by words
spaceCount :: [String] -> Int
spaceCount xs = (length xs) - 1
-- Calculate the size of a line (including spaces)
lineLength :: [String] -> Int
lineLength xs = (sum (map length xs)) + spaceCount xs
-- Returns the longest line of a given previously splited in text
longestLine :: [[String]] -> Int
longestLine xs = maximum (map lineLength xs)
spacesToInsert :: Int -> Int -> Int -> Int
spacesToInsert _ _ 0 = 0
spacesToInsert longest current spcCount = (longest - current) `div` spcCount
joinWith :: String -> [String] -> String
joinWith s [] = []
joinWith s [x] = x
joinWith s (x:xs) = x ++ s ++ (joinWith s xs)
insertSpaces :: Int -> String -> String
insertSpaces 0 s = s
insertSpaces n [] = []
insertSpaces n s =
let
firstWord = takeUntilSpace s
spaces = (takeWhile (==' ') (dropWhile (/=' ') s)) ++ " "
remainder = dropWhile (==' ') (dropWhile (/=' ') s)
in
firstWord ++ spaces ++ (insertSpaces (n-1) remainder)
takeRepeat :: Int -> a -> [a]
takeRepeat n c = take n (repeat c)
justifyLines :: String -> [String]
justifyLines xs =
let
textInWords = textToWords xs
longest = longestLine textInWords
in
(map
(\ line -> joinWith
(takeRepeat
((spacesToInsert longest (lineLength line) (spaceCount line))+1)
' ')
line)
textInWords
)
insertMissingSpaces :: [String] -> [String]
insertMissingSpaces xs =
let
longest = maximum (map length xs)
in
(map
(\ line -> insertSpaces (longest - (length line)) line)
xs)
justifyText :: String -> String
justifyText xs = (joinWith "\n" (insertMissingSpaces (justifyLines xs))) ++ "\n"
texto_exemplo = "RUBIÃO fitava a enseada -- eram oito horas da manhã.\nQuem o visse com os polegares metidos no cordão do chambre à janela de uma\ngrande casa de Botafogo cuidaria que ele admirava aquele pedaço de água\nquieta mas em verdade vos digo que pensava em outra coisa.\nCotejava o passado com o presente. Que era há um ano?\nProfessor. Que é agora? Capitalista! Olha para si para as chinelas\n(umas chinelas de Túnis que lhe deu recente amigo Cristiano Palha) para a casa\npara o jardim para a enseada para os morros e para o céu e tudo desde as chinelas\naté o céu tudo entra na mesma sensação de propriedade."
texto_exemplo_2 = "Animation in user interfaces has been an issue of hot debates for several years\nso far. It’s especially active in the domain of conceptual animation for user\ninterfaces. It offers creative experiments and pushes the limits of UI motion.\nHere in Tubik we have already shared articles about UI animation and its\nbenefits for apps and website. Today, let’s continue the theme considering\nhow conceptual animation influences product success together with Tubik\nmotion designer Kirill Yerokhin."
texto_exemplo_3 = "Conceptual animation is a field of concept art.\nIt is a piece of motion design that is created to\nconvey a particular idea before it is put into a real\nproduct. In user interfaces design, conceptual\nanimation may be found in various concepts for\ninteractions, transitions, manipulations with controls,\nanimation marking the feedback from the system etc.\nMotion designers use a variety of tools among which we\ncould mention Adobe After Effects, Principle, Figma and\nInVision."
texto_exemplo_4 = "In fact, creation and research of something innovative start from a concept in practically any industry or creative field.\nLook at automotive industry or architecture, remember how new art directions appeared and developed in history. Whatever is\nthe sphere, the attitude to concepts will show two opposites from “that’s just a fantasy that has nothing to do with real\nlife” to “why not…” Both variants are viable.\nStill, for better or worse, concepts from the power that makes progress possible.\nThe same situation is observed in the domain of UI animation. Most animations which today are taken for granted as an integral\npart of our interfaces were a kind of “unreal” concepts not so long ago. In the age of flat design, when shapes and colors\nare striving for simplicity and cleanness, animation becomes the proven way to make the apps\nand solutions stand out in terms of tense competition."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment