September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,4 +1,6 @@
|
|||
use framework "Foundation" -- Yosemite onwards – for splitting by regex
|
||||
use framework "Foundation" -- Yosemite onwards – for splitting by regex
|
||||
|
||||
-- SPARKLINE -----------------------------------------------------------------
|
||||
|
||||
-- sparkLine :: [Num] -> String
|
||||
on sparkLine(xs)
|
||||
|
|
@ -8,20 +10,20 @@ on sparkLine(xs)
|
|||
|
||||
-- scale :: Num -> Num
|
||||
script scale
|
||||
on lambda(x)
|
||||
on |λ|(x)
|
||||
((x - min) * 7) / dataRange
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
-- bucket :: Num -> String
|
||||
script bucket
|
||||
on lambda(n)
|
||||
on |λ|(n)
|
||||
if n ≥ 0 and n < 8 then
|
||||
item (n + 1 as integer) of "▁▂▃▄▅▆▇█"
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
intercalate("", map(bucket, map(scale, xs)))
|
||||
|
|
@ -41,20 +43,20 @@ on numericOrdering(a, b)
|
|||
end numericOrdering
|
||||
|
||||
|
||||
-- TEST
|
||||
-- TEST ----------------------------------------------------------------------
|
||||
on run
|
||||
|
||||
-- splitNumbers :: String -> [Real]
|
||||
script splitNumbers
|
||||
script asReal
|
||||
on lambda(x)
|
||||
on |λ|(x)
|
||||
x as real
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
on lambda(s)
|
||||
on |λ|(s)
|
||||
map(asReal, splitRegex("[\\s,]+", s))
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(sparkLine, map(splitNumbers, ["1 2 3 4 5 6 7 8 7 6 5 4 3 2 1", ¬
|
||||
|
|
@ -66,9 +68,7 @@ on run
|
|||
end run
|
||||
|
||||
|
||||
|
||||
|
||||
-- GENERIC LIBRARY FUNCTIONS
|
||||
-- GENERIC LIBRARY FUNCTIONS -------------------------------------------------
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
|
|
@ -76,19 +76,27 @@ on foldl(f, startValue, xs)
|
|||
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)
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
-- 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
|
||||
|
||||
-- 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)
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
|
|
@ -98,13 +106,13 @@ end map
|
|||
on maximumBy(f, xs)
|
||||
script max
|
||||
property cmp : f
|
||||
on lambda(a, b)
|
||||
on |λ|(a, b)
|
||||
if a is missing value or cmp(a, b) < 0 then
|
||||
b
|
||||
else
|
||||
a
|
||||
end if
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldl(max, missing value, xs)
|
||||
|
|
@ -114,48 +122,29 @@ end maximumBy
|
|||
on minimumBy(f, xs)
|
||||
script min
|
||||
property cmp : f
|
||||
on lambda(a, b)
|
||||
on |λ|(a, b)
|
||||
if a is missing value or cmp(a, b) > 0 then
|
||||
b
|
||||
else
|
||||
a
|
||||
end if
|
||||
end lambda
|
||||
end |λ|
|
||||
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
|
||||
-- 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
|
||||
{str}
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end splitRegex
|
||||
end mReturn
|
||||
|
||||
-- regexMatches :: RegexPattern -> String -> [{location:Int, length:Int}]
|
||||
on regexMatches(strRegex, str)
|
||||
|
|
@ -173,22 +162,33 @@ on regexMatches(strRegex, str)
|
|||
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
|
||||
-- splitRegex :: RegexPattern -> String -> [String]
|
||||
on splitRegex(strRegex, str)
|
||||
set lstMatches to regexMatches(strRegex, str)
|
||||
if length of lstMatches > 0 then
|
||||
script preceding
|
||||
on |λ|(a, x)
|
||||
set iFrom to start of a
|
||||
set iLocn to (location of x)
|
||||
|
||||
-- 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
|
||||
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 |λ|
|
||||
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 mReturn
|
||||
end splitRegex
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
(defun buckets (numbers)
|
||||
(loop with min = (apply #'min numbers)
|
||||
with max = (apply #'max numbers)
|
||||
with width = (/ (- max min) 7)
|
||||
for base from (- min (/ width 2)) by width
|
||||
repeat 8
|
||||
collect (cons base (+ base width))))
|
||||
|
||||
(defun bucket-for-number (number buckets)
|
||||
(loop for i from 0
|
||||
for (min . max) in buckets
|
||||
when (and (<= min number) (< number max))
|
||||
return i))
|
||||
|
||||
(defun sparkline (numbers)
|
||||
(loop with buckets = (buckets numbers)
|
||||
with sparks = "▁▂▃▄▅▆▇█"
|
||||
with sparkline = (make-array (length numbers) :element-type 'character)
|
||||
with min = (apply #'min numbers)
|
||||
with max = (apply #'max numbers)
|
||||
for number in numbers
|
||||
for i from 0
|
||||
for bucket = (bucket-for-number number buckets)
|
||||
do (setf (aref sparkline i) (char sparks bucket))
|
||||
finally (format t "Min: ~A, Max: ~A, Range: ~A~%" min max (- max min))
|
||||
(write-line sparkline)))
|
||||
|
||||
(defun string->numbers (string)
|
||||
(flet ((delimiterp (c)
|
||||
(or (char= c #\Space) (char= c #\,))))
|
||||
(loop for prev-end = 0 then end
|
||||
while prev-end
|
||||
for start = (position-if-not #'delimiterp string :start prev-end)
|
||||
for end = (position-if #'delimiterp string :start start)
|
||||
for number = (read-from-string string t nil :start start :end end)
|
||||
do (assert (numberp number))
|
||||
collect number)))
|
||||
|
||||
(defun string->sparkline (string)
|
||||
(sparkline (string->numbers string)))
|
||||
|
||||
(string->sparkline "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1")
|
||||
(string->sparkline "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5")
|
||||
22
Task/Sparkline-in-unicode/Haskell/sparkline-in-unicode-2.hs
Normal file
22
Task/Sparkline-in-unicode/Haskell/sparkline-in-unicode-2.hs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import Data.List.Split (splitOneOf)
|
||||
|
||||
sparkLine :: [Float] -> String
|
||||
sparkLine xs =
|
||||
(("▁▂▃▄▅▆▇█" !!) . floor . (/ range) . (7 *) . subtract min) <$> xs
|
||||
where
|
||||
min = minimum xs
|
||||
range = maximum xs - min
|
||||
|
||||
parseFloats :: String -> [Float]
|
||||
parseFloats = (read <$>) . filter (not . null) . splitOneOf " ,"
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
putStrLn
|
||||
((sparkLine . parseFloats) <$>
|
||||
[ "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"
|
||||
])
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
spkln 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
|
||||
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
|
||||
spkln 1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5
|
||||
▁▅█▆▅▃▂▇▄▅
|
||||
17
Task/Sparkline-in-unicode/Rust/sparkline-in-unicode.rust
Normal file
17
Task/Sparkline-in-unicode/Rust/sparkline-in-unicode.rust
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
const BARS: &'static str = "▁▂▃▄▅▆▇█";
|
||||
|
||||
fn print_sparkline(s: &str){
|
||||
let v = BARS.chars().collect::<Vec<char>>();
|
||||
let line: String = s.replace(",", " ").split(" ")
|
||||
.filter(|x| !x.is_empty())
|
||||
.map(|x| v[x.parse::<f64>().unwrap().ceil() as usize - 1])
|
||||
.collect();
|
||||
println!("{:?}", line);
|
||||
}
|
||||
|
||||
fn main(){
|
||||
let s1 = "1 2 3 4 5 6 7 8 7 6 5 4 3 2 1";
|
||||
print_sparkline(s1);
|
||||
let s2 = "1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5";
|
||||
print_sparkline(s2);
|
||||
}
|
||||
25
Task/Sparkline-in-unicode/S-lang/sparkline-in-unicode.slang
Normal file
25
Task/Sparkline-in-unicode/S-lang/sparkline-in-unicode.slang
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
% Just to demonstrate alternate ways of defining unicode:
|
||||
private variable spchrs = "\u{2581}\u{2582}\u{2583}\u{2584}\u{2585}\u{2586}\u{2587}\u{2588}";
|
||||
private variable spchrs_alt = "▁▂▃▄▅▆▇█";
|
||||
|
||||
define sparkline(arrstr)
|
||||
{
|
||||
variable a = strtok(arrstr, " \t,"), alen = length(a), out = "";
|
||||
a = atof(a);
|
||||
variable amin = min(a), amax = max(a), span = amax - amin, i, d;
|
||||
|
||||
_for i (0, alen-1, 1)
|
||||
if (span != 0) {
|
||||
% int() truncates; adding .5 here to round:
|
||||
d = int((a[i] - amin) * 7.0 / span + 0.5);
|
||||
out += substr(spchrs, d+1, 1);
|
||||
}
|
||||
else
|
||||
out += substr(spchrs, 4, 1);
|
||||
|
||||
print(out);
|
||||
}
|
||||
|
||||
if (not _slang_utf8_ok) error("Sorry, UTF8 mode is not on.");
|
||||
sparkline("1 2 3 4 5 6 7 8 7 6 5 4 3 2 1");
|
||||
sparkline("1.5, 0.5 3.5, 2.5 5.5, 4.5 7.5, 6.5 ");
|
||||
9
Task/Sparkline-in-unicode/Zkl/sparkline-in-unicode-1.zkl
Normal file
9
Task/Sparkline-in-unicode/Zkl/sparkline-in-unicode-1.zkl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
var sparks=[0x2581..0x2588].apply("toString",-8); // int.toString(-8)-->UTF-8
|
||||
var sl=(sparks.len()-1);
|
||||
|
||||
fcn sparkLine(xs){
|
||||
min:=(0.0).min(xs); max:=(0.0).max(xs); // min/max are float reguardless of xs
|
||||
range:=max-min; // float
|
||||
println("Range [",min,"-",max,"]", xs);
|
||||
xs.pump(String,'wrap(x){ sparks[(x - min)*sl/range] }).println();
|
||||
}
|
||||
3
Task/Sparkline-in-unicode/Zkl/sparkline-in-unicode-2.zkl
Normal file
3
Task/Sparkline-in-unicode/Zkl/sparkline-in-unicode-2.zkl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
one:="1 2 3 4 5 6 7 8 7 6 5 4 3 2 1".split(" ").apply("toInt");
|
||||
two:=("1.5, 0.5 3.5, 2.5 5.5 4.5 7.5, 6.5" - ",").split(" ").apply("toFloat");
|
||||
sparkLine(one); sparkLine(two);
|
||||
Loading…
Add table
Add a link
Reference in a new issue