Sparkline in unicode: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: no_empty param is no more)
(Updated to work with Nim 1.4: changed "True" to "true"; added import of "sequtils" (for "map"). Also, used directly the UTF-8 strings for the chars (no need for module "unicode"), changed precision to -1, added "filterIt"..)
Line 1,654: Line 1,654:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Python}}
{{trans|Python}}
<lang nim>import rdstdin, strutils, unicode
<lang nim>import rdstdin, sequtils, strutils


const bar = [9601, 9602, 9603, 9604, 9605, 9606, 9607, 9608]
const bar = ["\u2581", "\u2582", "\u2583", "\u2584", "\u2585", "\u2586", "\u2587", "\u2588"]
const barcount = float(bar.high)
const barcount = float(bar.high)


while True:
while true:
let
let
line = readLineFromStdin "Numbers please separated by space/commas: "
line = readLineFromStdin "Numbers please separated by space/commas: "
numbers = line.split({' ',','}).map(parseFloat)
numbers = line.split({' ', ','}).filterIt(it.len != 0).map(parseFloat)
mn = min(numbers)
mn = min(numbers)
mx = max(numbers)
mx = max(numbers)
Line 1,668: Line 1,668:
var sparkline = ""
var sparkline = ""
for n in numbers:
for n in numbers:
let i = int((n-mn) / extent * barcount)
let i = int((n - mn) / extent * barcount)
sparkline.add($TRune(bar[i]))
sparkline.add bar[i]
echo "min: ", mn.formatFloat(precision = 0), "; max: ", mx.formatFloat(precision = 0)
echo "min: ", mn.formatFloat(precision = 0), "; max: ", mx.formatFloat(precision = -1)
echo sparkline</lang>
echo sparkline</lang>
{{out}}
{{out}}