Textonyms: Difference between revisions

→‎{{header|Haskell}}: Simpler with flipped tuples.
(→‎{{header|Haskell}}: Simpler with flipped tuples.)
Line 902:
Or, in terms of ''Data.Map'' and ''traverse'':
 
<lang haskell>{-#import LANGUAGEData.List TupleSections(groupBy, #-}maximum, sortBy, sortOn)
 
import Data.List (groupBy, maximum, sortBy, sortOn)
import qualified Data.Map as M
import Data.Maybe (mapMaybe)
Line 911 ⟶ 909:
 
digitEncoded :: [String] -> [(String, String)]
digitEncoded = mapMaybe (\cs -((>>=) . traverse toKey) cs <*>>= (curry Just . (, cs)))
 
codeGrouped :: [(String, String)] -> [[(String, String)]]
codeGrouped = groupBy (on (==) fstsnd) . sortOn fstsnd
 
textonymSubset :: [[(String, String)]] -> [[(String, String)]]
Line 933 ⟶ 931:
showTextonym :: Int -> [(String, String)] -> String
showTextonym w ts =
concat [justifyRight w ' ' (fstsnd (head ts)), " -> ", unwords $ fmap sndfst ts]
 
justifyRight :: Int -> Char -> String -> String
Line 939 ⟶ 937:
 
firstColWidth :: [[(String, String)]] -> Int
firstColWidth = maximum . map (length . fstsnd . head)
 
main :: IO ()
Line 949 ⟶ 947:
textonyms = textonymSubset codeGroups
ambiguous = take 5 $ sortBy (flip (comparing length)) textonyms
longer = take 5 $ sortBy (flip (comparing (length . fstsnd . head))) textonyms
[wa, wl] = firstColWidth <$> [ambiguous, longer]
mapM_ putStrLn $
9,659

edits