Sparkline in unicode: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: Tidied, updated primitives.)
Line 1,871: Line 1,871:


from functools import reduce
from functools import reduce
import operator
import re
import re



# ------------------- LABELLED SPARKLINE -------------------
# ------------------- LABELLED SPARKLINE -------------------


# sparkLine :: [Float] -> [String]
# sparkLine :: [Float] -> [String]


def sparkLine(xs):
def sparkLine(xs):
'''Unicode sparkline summary of a
'''Unicode sparkline summary of a
Line 1,889: Line 1,889:
w = (mx - mn) / 8
w = (mx - mn) / 8
lbounds = list(map(lambda i: mn + (w * i), range(1, 8)))
lbounds = list(map(lambda i: mn + (w * i), range(1, 8)))
le = curry(operator.le)

# spark :: Float -> Char
def spark(x):
def spark(x):
def go(i):
def go(i):
return '▁▂▃▄▅▆▇'[i]
return '▁▂▃▄▅▆▇'[i]
return maybe('█')(go)(
return maybe('█')(go)(
findIndex(lambda b: b > x)(lbounds)
findIndex(le(x))(lbounds)
)
)
return [
return [
Line 1,960: Line 1,962:
return fg
return fg
return reduce(go, fs, lambda x: x)
return reduce(go, fs, lambda x: x)


# curry :: ((a, b) -> c) -> a -> b -> c
def curry(f):
'''A curried function derived
from an uncurried function.
'''
return lambda x: lambda y: f(x, y)




Line 2,025: Line 2,035:




# MAIN ---
# TEST -------------------------------------------------
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</lang>