Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
57
Task/Sparkline-in-unicode/Haskell/sparkline-in-unicode-1.hs
Normal file
57
Task/Sparkline-in-unicode/Haskell/sparkline-in-unicode-1.hs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import Data.List.Split (splitOneOf)
|
||||
import Data.Char (chr)
|
||||
|
||||
|
||||
toSparkLine :: [Double] -> String
|
||||
toSparkLine xs = map cl xs
|
||||
where
|
||||
top = maximum xs
|
||||
bot = minimum xs
|
||||
range = top - bot
|
||||
cl x = chr $ 0x2581 + floor (min 7 ((x - bot) / range * 8))
|
||||
|
||||
makeSparkLine :: String -> (String, Stats)
|
||||
makeSparkLine xs = (toSparkLine parsed, stats parsed)
|
||||
where
|
||||
parsed = map read $ filter (not . null) $ splitOneOf " ," xs
|
||||
|
||||
data Stats = Stats
|
||||
{ minValue, maxValue, rangeOfValues :: Double
|
||||
, numberOfValues :: Int
|
||||
}
|
||||
|
||||
instance Show Stats where
|
||||
show (Stats mn mx r n) =
|
||||
"min: " ++
|
||||
show mn ++
|
||||
"; max: " ++
|
||||
show mx ++ "; range: " ++ show r ++ "; no. of values: " ++ show n
|
||||
|
||||
stats :: [Double] -> Stats
|
||||
stats xs =
|
||||
Stats
|
||||
{ minValue = mn
|
||||
, maxValue = mx
|
||||
, rangeOfValues = mx - mn
|
||||
, numberOfValues = length xs
|
||||
}
|
||||
where
|
||||
mn = minimum xs
|
||||
mx = maximum xs
|
||||
|
||||
drawSparkLineWithStats :: String -> IO ()
|
||||
drawSparkLineWithStats xs = putStrLn sp >> print st
|
||||
where
|
||||
(sp, st) = makeSparkLine xs
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
drawSparkLineWithStats
|
||||
[ "0, 1, 19, 20"
|
||||
, "0, 999, 4000, 4999, 7000, 7999"
|
||||
, "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"
|
||||
, "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5"
|
||||
, "3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0 1 2 3"
|
||||
, "-1000 100 1000 500 200 -400 -700 621 -189 3"
|
||||
]
|
||||
37
Task/Sparkline-in-unicode/Haskell/sparkline-in-unicode-2.hs
Normal file
37
Task/Sparkline-in-unicode/Haskell/sparkline-in-unicode-2.hs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import Data.Bifunctor (bimap)
|
||||
import Data.List (findIndex)
|
||||
import Data.List.Split (splitOneOf)
|
||||
import Data.Maybe (maybe)
|
||||
|
||||
------------------------ SPARK LINE ----------------------
|
||||
|
||||
sparkLine :: [Float] -> String
|
||||
sparkLine [] = ""
|
||||
sparkLine xxs@(x : xs) =
|
||||
fmap
|
||||
( maybe '█' ("▁▂▃▄▅▆▇" !!)
|
||||
. flip findIndex lbounds
|
||||
. (<)
|
||||
)
|
||||
xxs
|
||||
where
|
||||
(mn, mx) = foldr (bimap . min <*> max) (x, x) xs
|
||||
w = (mx - mn) / 8
|
||||
lbounds = (mn +) . (w *) <$> [1 .. 7]
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_ putStrLn $
|
||||
sparkLine . parseFloats
|
||||
<$> [ "0, 1, 19, 20",
|
||||
"0, 999, 4000, 4999, 7000, 7999",
|
||||
"1 2 3 4 5 6 7 8 7 6 5 4 3 2 1",
|
||||
"1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5"
|
||||
]
|
||||
|
||||
parseFloats :: String -> [Float]
|
||||
parseFloats =
|
||||
fmap read
|
||||
. filter (not . null)
|
||||
. splitOneOf " ,"
|
||||
Loading…
Add table
Add a link
Reference in a new issue