Last active
March 14, 2018 05:36
-
-
Save abhin4v/fb214f9be1c81bf2f52d6e15653a1c9b to your computer and use it in GitHub Desktop.
Simple on-file number sort in haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Data.List (sort) | |
import Data.List.Split (chunksOf) | |
import System.IO.Temp (writeSystemTempFile) | |
readI :: String -> Int | |
readI = read | |
merge :: (Ord a) => [a] -> [a] -> [a] | |
merge [] [] = [] | |
merge xs [] = xs | |
merge [] ys = ys | |
merge (x:xs) (y:ys) = if x < y then x : merge xs (y:ys) else y : merge (x:xs) ys | |
main :: IO () | |
main = | |
getContents | |
>>= return . map (unlines . map show . sort) . chunksOf 10000 . map readI . lines | |
>>= mapM (writeSystemTempFile "hsort") | |
>>= traverse readFile | |
>>= return . unlines . map show . foldr1 merge . map (map readI . lines) | |
>>= putStr | |
-- Usage: cat numbers | fsort > numbers_sorted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment