2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,6 +1,8 @@
A [[wp:Sparkline|sparkline]] is a graph of successive values laid out horizontally
where the height of the line is proportional to the values in succession.
;Task:
Use the following series of Unicode characters to create a program
that takes a series of numbers separated by one or more whitespace or comma characters
and generates a sparkline-type bar graph of the values on a single line of output.
@ -17,3 +19,4 @@ here on this page:
;Notes:
* A space is not part of the generated sparkline.
* The sparkline may be accompanied by simple statistics of the data such as its range.
<br><br>

View file

@ -0,0 +1,194 @@
use framework "Foundation" -- Yosemite onwards for splitting by regex
-- sparkLine :: [Num] -> String
on sparkLine(xs)
set min to minimumBy(my numericOrdering, xs)
set max to maximumBy(my numericOrdering, xs)
set dataRange to max - min
-- scale :: Num -> Num
script scale
on lambda(x)
((x - min) * 7) / dataRange
end lambda
end script
-- bucket :: Num -> String
script bucket
on lambda(n)
if n 0 and n < 8 then
item (n + 1 as integer) of "▁▂▃▄▅▆▇█"
else
missing value
end if
end lambda
end script
intercalate("", map(bucket, map(scale, xs)))
end sparkLine
-- numericOrdering :: Num -> Num -> (-1 | 0 | 1)
on numericOrdering(a, b)
if a < b then
-1
else
if a > b then
1
else
0
end if
end if
end numericOrdering
-- TEST
on run
-- splitNumbers :: String -> [Real]
script splitNumbers
script asReal
on lambda(x)
x as real
end lambda
end script
on lambda(s)
map(asReal, splitRegex("[\\s,]+", s))
end lambda
end script
map(sparkLine, map(splitNumbers, ["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"]))
-- {"▁▂▃▄▅▆▇█▇▆▅▄▃▂▁","▂▁▄▃▆▅█▇","█▇▆▅▄▃▂▁▂▃▄▅▆▇█","▁▅█▆▅▃▂▇▄▅"}
end run
-- GENERIC LIBRARY FUNCTIONS
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to lambda(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- maximumBy :: (a -> a -> Ordering) -> [a] -> a
on maximumBy(f, xs)
script max
property cmp : f
on lambda(a, b)
if a is missing value or cmp(a, b) < 0 then
b
else
a
end if
end lambda
end script
foldl(max, missing value, xs)
end maximumBy
-- minimumBy :: (a -> a -> Ordering) -> [a] -> a
on minimumBy(f, xs)
script min
property cmp : f
on lambda(a, b)
if a is missing value or cmp(a, b) > 0 then
b
else
a
end if
end lambda
end script
foldl(min, missing value, xs)
end minimumBy
-- splitRegex :: RegexPattern -> String -> [String]
on splitRegex(strRegex, str)
set lstMatches to regexMatches(strRegex, str)
if length of lstMatches > 0 then
script preceding
on lambda(a, x)
set iFrom to start of a
set iLocn to (location of x)
if iLocn > iFrom then
set strPart to text (iFrom + 1) thru iLocn of str
else
set strPart to ""
end if
{parts:parts of a & strPart, start:iLocn + (length of x) - 1}
end lambda
end script
set recLast to foldl(preceding, {parts:[], start:0}, lstMatches)
set iFinal to start of recLast
if iFinal < length of str then
parts of recLast & text (iFinal + 1) thru -1 of str
else
parts of recLast & ""
end if
else
{str}
end if
end splitRegex
-- regexMatches :: RegexPattern -> String -> [{location:Int, length:Int}]
on regexMatches(strRegex, str)
set ca to current application
set oRgx to ca's NSRegularExpression's regularExpressionWithPattern:strRegex ¬
options:((ca's NSRegularExpressionAnchorsMatchLines as integer)) |error|:(missing value)
set oString to ca's NSString's stringWithString:str
set oMatches to oRgx's matchesInString:oString options:0 range:{location:0, |length|:oString's |length|()}
set lstMatches to {}
set lng to count of oMatches
repeat with i from 1 to lng
set end of lstMatches to range() of item i of oMatches
end repeat
lstMatches
end regexMatches
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property lambda : f
end script
end if
end mReturn

View file

@ -1,8 +1,8 @@
defmodule RC do
def sparkline(str) do
values = str |> String.split(~r/(,| )+/)
|> Enum.map(&(elem(Float.parse(&1), 0)))
{min, max} = {Enum.min(values), Enum.max(values)}
|> Enum.map(&elem(Float.parse(&1), 0))
{min, max} = Enum.min_max(values)
IO.puts Enum.map(values, &(round((&1 - min) / (max - min) * 7 + 0x2581)))
end
end

View file

@ -0,0 +1,49 @@
(() => {
'use strict';
// sparkLine :: [Num] -> String
let sparkLine = xs => {
let min = minimumBy(numericOrdering, xs),
max = maximumBy(numericOrdering, xs),
range = max - min;
return xs.map(x => ((x - min) * 7) / range)
.map(
n => (n >= 0 && n < 8) ? '▁▂▃▄▅▆▇█'
.split('')[Math.round(n)] : undefined
).join('');
},
// maximumBy :: (a -> a -> Ordering) -> [a] -> a
maximumBy = (f, xs) =>
xs.reduce((a, x) =>
a === undefined ? x : (
f(x, a) > 0 ? x : a
),
undefined
),
// minimumBy :: (a -> a -> Ordering) -> [a] -> a
minimumBy = (f, xs) =>
xs.reduce((a, x) =>
a === undefined ? x : (
f(x, a) < 0 ? x : a
),
undefined
),
numericOrdering = (a, b) => a < b ? -1 : (a > b ? 1 : 0);
// TEST
return ["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"
].map(
s => s.split(/[,\s]+/)
.map(x => parseFloat(x, 10))
).map(sparkLine);
})();

View file

@ -0,0 +1,22 @@
internal val bars = "▁▂▃▄▅▆▇█"
internal val n = bars.length - 1
fun <T: Number> Iterable<T>.toSparkline(): String {
var min = Double.MAX_VALUE
var max = Double.MIN_VALUE
val doubles = map { it.toDouble() }
doubles.forEach { i -> when { i < min -> min = i; i > max -> max = i } }
val range = max - min
return doubles.fold("") { line, d -> line + bars[Math.ceil((d - min) / range * n).toInt()] }
}
fun String.toSparkline() = replace(",", "").split(" ").map { it.toFloat() }.toSparkline()
fun main(args: Array<String>) {
val s1 = "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"
println(s1)
println(s1.toSparkline())
val s2 = "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5"
println(s2)
println(s2.toSparkline())
}

View file

@ -0,0 +1,6 @@
(de sparkLine (Lst)
(let (Min (apply min Lst) Max (apply max Lst) Rng (- Max Min))
(for N Lst
(prin
(char (+ 9601 (*/ (- N Min) 7 Rng)) ) ) )
(prinl) ) )

View file

@ -0,0 +1,2 @@
(sparkLine (str "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1"))
(sparkLine (scl 1 (str "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5")))