Sparkline in unicode: Difference between revisions

→‎{{header|Python}}: Tidied, updated primitives.
(→‎{{header|Haskell}}: Tidied, pruned out one import.)
(→‎{{header|Python}}: Tidied, updated primitives.)
Line 1,868:
If the search result is 'not found' (Nothing), then we use the highest (nominally 8th, or index 7) block.
 
<lang python>import'''Sparkline rein Unicode'''
 
from functools import reduce
import re
 
# ------------------- LABELLED SPARKLINE -------------------
 
# sparkLine :: [Float] -> [String]
 
 
def sparkLine(xs):
'''Unicode sparkline summary of a
list of floating point numbers.
)'''
def go(xs):
ys = sorted(xs)
Line 1,880 ⟶ 1,889:
w = (mx - mn) / 8
lbounds = list(map(lambda i: mn + (w * i), range(1, 8)))
 
def )spark(x),:
def xsgo(i):
lambda i:return '▁▂▃▄▅▆▇'[i]
lambda x:return maybe('█')(go)(
)(findIndex(lambda b: b > x)(lbounds)),
)
return [
''.join(map(spark, xs)),
lambda x: maybe('█')(
lambda i: '▁▂▃▄▅▆▇'[i]
)(findIndex(lambda b: b > x)(lbounds)),
xs
)),
' '.join(map(str, xs)),
'\t'.join([
Line 1,903 ⟶ 1,914:
 
 
# -------------------------- TEST --------------------------
# main :: IO ()
def main():
'''Tested on some sample lists.
'''
print(
unlines(map(
compose(compose(unlines)(, sparkLine))(, readFloats),
[
"0, 1, 19, 20",
Line 1,918 ⟶ 1,932:
 
 
# GENERIC ------------------------ GENERIC -------------------------
 
 
# Just :: a -> Maybe a
def Just(x):
'''Constructor for an inhabited Maybe (option type) value.
Wrapper containing the result of a computation.
'''
return {'type': 'Maybe', 'Nothing': False, 'Just': x}
 
Line 1,928 ⟶ 1,944:
# Nothing :: Maybe a
def Nothing():
'''Constructor for an empty Maybe (option type) value.
Empty wrapper returned where a computation is not possible.
'''
return {'type': 'Maybe', 'Nothing': True}
 
 
# compose (<<<) :: (b(a -> ca), ...) -> (a -> ba) -> a -> c
def compose(g*fs):
'''Composition, from right to left,
return lambda f: lambda x: g(f(x))
of a series of functions.
'''
def go(f, g):
def fg(x):
return f(g(x))
return fg
return reduce(go, fs, lambda x: x)
 
 
# even :: Int -> Bool
def even(x):
'''True if x is an integer
multiple of two.
'''
return 0 == x % 2
 
Line 1,943 ⟶ 1,972:
# findIndex :: (a -> Bool) -> [a] -> Maybe Int
def findIndex(p):
'''Just the first index at which an
element in xs matches p,
or Nothing if no elements match.
'''
def go(xs):
try:
Line 1,950 ⟶ 1,983:
except StopIteration:
return Nothing()
return lambda xs: go(xs)
 
 
# maybe :: b -> (a -> b) -> Maybe a -> b
def maybe(v):
return'''Either lambdathe f:default lambda m:value v, if m.get('Nothing') elseis (Nothing,
or the application of f to x,
f(m.get('Just'))
where m is Just(x).
)
'''
return lambda f: lambda xm: g(fv if (x))
None is m or m.get('Nothing')
) else f(m.get('Just'))
 
 
# mean :: [Num] -> Float
def mean(xs):
'''The arithmetic mean of the numeric
values in xs.
'''
return sum(xs) / float(len(xs))
 
Line 1,967 ⟶ 2,007:
# readFloats :: String -> [Float]
def readFloats(s):
'''A list of floats parsed from
a numeric string delimited by
commas and/or white space.
'''
return list(map(
float,
re.split(r'[\s,]+', s)
))
 
Line 1,975 ⟶ 2,019:
# unlines :: [String] -> String
def unlines(xs):
'''A single string formed by the intercalation
of a list of strings with the newline character.
'''
return '\n'.join(xs)
 
9,655

edits