Sparkline in unicode: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Add Factor)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 43:
</lang>
Note → APL accepts the input with commas and spaces naturally. If one wanted to read input as a string they could use ⍎⍞ to do so.
 
 
=={{header|AppleScript}}==
Line 1,123 ⟶ 1,122:
 
</pre>
 
 
=={{header|JavaScript}}==
Line 1,406 ⟶ 1,404:
▂▁▄▃▆▅█▇</pre>
 
=={{header|NetRexx}}==
<lang NetRexx>/* NetRexx */
options replace format comments java crossref symbols nobinary
 
runSample(arg)
return
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method sparkline(spark) private static
spark = spark.changestr(',', ' ')
bars = '\u2581 \u2582 \u2583 \u2584 \u2585 \u2586 \u2587 \u2588'
barK = bars.words()
nmin = spark.word(1)
nmax = nmin
-- get min & max values
loop iw = 1 to spark.words()
nval = spark.word(iw)
nmin = nval.min(nmin)
nmax = nval.max(nmax)
end iw
range = nmax - nmin + 1
slope = ''
loop iw = 1 to spark.words()
point = Math.ceil((spark.word(iw) - nmin + 1) / range * barK)
slope = slope || bars.word(point)
end iw
return slope nmin nmax range
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
-- sample data setup
parse arg vals
sparks = 0
sparks[0] = 0
if vals = '' then do
si = sparks[0] + 1; sparks[0] = si; sparks[si] = 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
si = sparks[0] + 1; sparks[0] = si; sparks[si] = '1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5'
end
else do
loop until vals = ''
-- split input on a ! character
parse vals lst '!' vals
si = sparks[0] + 1; sparks[0] = si; sparks[si] = lst
end
end
-- run the samples
loop si = 1 to sparks[0]
vals = sparks[si]
parse sparkline(vals) slope .
say 'Input: ' vals
say 'Sparkline: ' slope
say
end si
return
</lang>
{{out}}
<pre>
Input: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
Sparkline: ▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
 
Input: 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
Sparkline: ▂▁▄▃▆▅█▇
</pre>
=={{header|M2000 Interpreter}}==
This statement a=cdr(a) make a new array from a excluding the first item, so can be return the empty array (,). In Row$(a) a is passing by value, but is a pointer to array, so we get a pointer to point to same array. When we do the a=cdr(a) we change pointer so variable (a) point to a new array.
Line 1,600 ⟶ 1,534:
toSparkline[" 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5 "]
▂▁▄▃▆▅█▇
</pre>
 
=={{header|NetRexx}}==
<lang NetRexx>/* NetRexx */
options replace format comments java crossref symbols nobinary
 
runSample(arg)
return
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method sparkline(spark) private static
spark = spark.changestr(',', ' ')
bars = '\u2581 \u2582 \u2583 \u2584 \u2585 \u2586 \u2587 \u2588'
barK = bars.words()
nmin = spark.word(1)
nmax = nmin
-- get min & max values
loop iw = 1 to spark.words()
nval = spark.word(iw)
nmin = nval.min(nmin)
nmax = nval.max(nmax)
end iw
range = nmax - nmin + 1
slope = ''
loop iw = 1 to spark.words()
point = Math.ceil((spark.word(iw) - nmin + 1) / range * barK)
slope = slope || bars.word(point)
end iw
return slope nmin nmax range
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
-- sample data setup
parse arg vals
sparks = 0
sparks[0] = 0
if vals = '' then do
si = sparks[0] + 1; sparks[0] = si; sparks[si] = 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
si = sparks[0] + 1; sparks[0] = si; sparks[si] = '1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5'
end
else do
loop until vals = ''
-- split input on a ! character
parse vals lst '!' vals
si = sparks[0] + 1; sparks[0] = si; sparks[si] = lst
end
end
-- run the samples
loop si = 1 to sparks[0]
vals = sparks[si]
parse sparkline(vals) slope .
say 'Input: ' vals
say 'Sparkline: ' slope
say
end si
return
</lang>
{{out}}
<pre>
Input: 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
Sparkline: ▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
 
Input: 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
Sparkline: ▂▁▄▃▆▅█▇
</pre>
 
Line 1,700 ⟶ 1,699:
8 values; range -8e+307..-1e+307
█▇▆▅▄▃▂▁</pre>
 
=={{header|Perl 6}}==
<lang perl6>constant @bars = '▁' ... '█';
while prompt 'Numbers separated by anything: ' -> $_ {
my @numbers = map +*, .comb(/ '-'? [[\d+ ['.' \d*]?] | ['.' \d+]] /);
my ($mn,$mx) = @numbers.minmax.bounds;
say "min: $mn.fmt('%5f'); max: $mx.fmt('%5f')";
say @bars[ @numbers.map: { @bars * ($_ - $mn) / ($mx - $mn) min @bars - 1 } ].join;
}</lang>
{{out}}
<pre>Numbers separated by anything: 9 18 27 36 45 54 63 72 63 54 45 36 27 18 9
9 18 27 36 45 54 63 72 63 54 45 36 27 18 9
min: 9.000000; max: 72.000000
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
Numbers separated by anything: 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
1.5 0.5 3.5 2.5 5.5 4.5 7.5 6.5
min: 0.500000; max: 7.500000
▂▁▄▃▆▅█▇
Numbers separated by anything: 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0 1 2 3
min: -4.000000; max: 3.000000
█▇▆▅▄▃▂▁▂▃▄▅▆▇█
Numbers separated by anything: ^D</pre>
 
=={{header|Phix}}==
Line 1,969 ⟶ 1,946:
1.5 0.5 3.5 2.5 5.5 4.5 7.5 6.5
Min 0.5 Mean 4.0 Median 4.0 Max 7.5</pre>
 
=={{header|Racket}}==
<lang racket>
#lang racket (require syntax/parse)
 
(define bars "▁▂▃▄▅▆▇█")
(define bar-count (string-length bars))
 
(define (sparks str)
(define ns (map string->number (string-split str #rx"[ ,]" #:repeat? #t)))
(define mn (apply min ns))
(define bar-width (/ (- (apply max ns) mn) (- bar-count 1)))
(apply string (for/list ([n ns]) (string-ref bars (exact-floor (/ (- n mn) bar-width))))))
 
(sparks "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1")
(sparks "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5")
</lang>
{{out}}
<lang racket>
"▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
"▂▁▄▃▆▅█▇"
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>constant @bars = '▁' ... '█';
while prompt 'Numbers separated by anything: ' -> $_ {
my @numbers = map +*, .comb(/ '-'? [[\d+ ['.' \d*]?] | ['.' \d+]] /);
my ($mn,$mx) = @numbers.minmax.bounds;
say "min: $mn.fmt('%5f'); max: $mx.fmt('%5f')";
say @bars[ @numbers.map: { @bars * ($_ - $mn) / ($mx - $mn) min @bars - 1 } ].join;
}</lang>
{{out}}
<pre>Numbers separated by anything: 9 18 27 36 45 54 63 72 63 54 45 36 27 18 9
9 18 27 36 45 54 63 72 63 54 45 36 27 18 9
min: 9.000000; max: 72.000000
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
Numbers separated by anything: 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
1.5 0.5 3.5 2.5 5.5 4.5 7.5 6.5
min: 0.500000; max: 7.500000
▂▁▄▃▆▅█▇
Numbers separated by anything: 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0 1 2 3
min: -4.000000; max: 3.000000
█▇▆▅▄▃▂▁▂▃▄▅▆▇█
Numbers separated by anything: ^D</pre>
 
=={{header|REXX}}==
Line 2,100 ⟶ 2,122:
output: ▂▁▄▃▆▅█▇
</pre>
 
=={{header|Racket}}==
<lang racket>
#lang racket (require syntax/parse)
 
(define bars "▁▂▃▄▅▆▇█")
(define bar-count (string-length bars))
 
(define (sparks str)
(define ns (map string->number (string-split str #rx"[ ,]" #:repeat? #t)))
(define mn (apply min ns))
(define bar-width (/ (- (apply max ns) mn) (- bar-count 1)))
(apply string (for/list ([n ns]) (string-ref bars (exact-floor (/ (- n mn) bar-width))))))
 
(sparks "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1")
(sparks "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5")
</lang>
{{out}}
<lang racket>
"▁▂▃▄▅▆▇█▇▆▅▄▃▂▁"
"▂▁▄▃▆▅█▇"
</lang>
 
=={{header|Ruby}}==
10,333

edits