Sparkline in unicode: Difference between revisions
→{{header|AppleScript}}: Added two test samples recently suggested in Discussion page, updated code to normalize their rendering
(→{{header|Haskell}}: Updated minimal Haskell version for stronger parallel to second Python version) |
(→{{header|AppleScript}}: Added two test samples recently suggested in Discussion page, updated code to normalize their rendering) |
||
Line 36:
=={{header|AppleScript}}==
<lang AppleScript>use
use framework "Foundation"
use scripting additions
on run
unlines(map(unlines, ¬
map(compose(sparkLine, readFloats), ¬
{"0, 1, 19, 20", "0, 999, 4000, 4999, 7000, 7999", ¬
"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"})))
end run
-- sparkLine :: [
on sparkLine(xs)
set
set
set
set n to length of xs
set mid to (n div 2)
set w to (mx - mn) / 8
script bound
on |λ|(x)
end |λ|
end script
set lbounds to map(bound, enumFromTo(1, 7))
script spark
end
on |λ|(i)
item i of "▁▂▃▄▅▆▇"
end |λ|
end script
maybe("█", indexedBlock, findIndex(flipGT, lbounds))
end |λ|
end script
script str
on |λ|(x)
x as string
end |λ|
end script
{concat(map(spark, xs)), ¬
unwords(map(str, xs)), ¬
"Min " & mn as string, ¬
"Mean " & roundTo(mean(xs), 2) as string, ¬
"Median " & bool(item mid of xs, ((item mid of xs) + ¬
(item (mid + 1) of xs)) / 2, even(n)), ¬
"Max " & mx as string, ""}
end sparkLine
-- GENERIC -------------------------------------------------
-- Just :: a -> Maybe
on Just(x)
{type:"Maybe", Nothing:false, Just:x}
end Just
-- Nothing :: Maybe a
on Nothing()
{type:"Maybe", Nothing:true}
end Nothing
-- bool :: a -> a -> Bool -> a
on bool(f, t, p)
if p then
t
else
end if
end
-- compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
on compose(f, g)
script
property mf : mReturn(f)
property mg : mReturn(g)
end |λ|
end script
end compose
-- concat :: [[a]] -> [a]
-- concat :: [String] -> String
on concat(xs)
set lng to length of xs
if 0 < lng and string is class of (item 1 of xs) then
set acc to ""
else
set acc to {}
end if
repeat with i from 1 to lng
set acc to acc & item i of xs
end repeat
acc
end concat
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m ≤ n then
set lst to {}
repeat with i from m to n
set end of lst to i
end repeat
return lst
else
return {}
end if
end enumFromTo
-- even :: Int -> Bool
on even(x)
0 = x mod 2
end even
-- Takes a predicate function and a list and
-- returns Just( the 1-based index of the first
-- element ) in the list satisfying the predicate
-- or Nothing if there is no such element.
-- findIndex(isSpace, "hello world")
--> {type:"Maybe", Nothing:false, Just:6}
-- findIndex(even, [3, 5, 7, 8, 9])
--> {type:"Maybe", Nothing:false, Just:4}
-- findIndex(isUpper, "all lower case")
--> {type:"Maybe", Nothing:true}
-- findIndex :: (a -> Bool) -> [a] -> Maybe Int
on findIndex(p, xs)
tell mReturn(p)
set lng to length of xs
repeat with i from 1 to lng
if |λ|(item i of xs) then return Just(i)
end repeat
return Nothing()
end tell
end findIndex
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 119 ⟶ 196:
end tell
end foldl
-- map :: (a -> b) -> [a] -> [b]
Line 140 ⟶ 209:
end map
--
on
script
end |λ|
end script
foldl(result, 0, xs) / (length of xs)
end mean
-- | The 'maybe' function takes a default value, a function, and a 'Maybe'
-- value. If the 'Maybe' value is 'Nothing', the function returns the
-- default value. Otherwise, it applies the function to the value inside
-- the 'Just' and returns the result.
-- maybe :: b -> (a -> b)
on maybe(v, f, mb)
if Nothing of mb
tell mReturn(f) to |λ|(Just
-- Lift 2nd class handler function into
-- mReturn ::
on mReturn(f)
if script is class of f
f
else
Line 184 ⟶ 244:
end mReturn
--
on readFloats(s)
script asReal
on |λ|(n)
n as real
end |λ|
end script
map(asReal, splitRegex("[\\s,]+", s))
end readFloats
-- regexMatches :: String -> String -> [[String]]
on regexMatches(strRegex, strHay)
set ca to current application
-- NSNotFound handling and and High Sierra workaround due to @sl1974
set NSNotFound to a reference to 9.22337203685477E+18 + 5807
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:strHay
script matchString
tell (m's rangeAtIndex:i)
set intFrom to its location
if NSNotFound ≠ intFrom then
text (intFrom + 1) thru (intFrom + (its |length|)) of strHay
else
missing value
end if
end tell
end |λ|
end script
end |λ|
end script
script asRange
on |λ|(x)
range() of x
end |λ|
end script
map(asRange, (oRgx's matchesInString:oString ¬
options:0 range:{location:0, |length|:oString's |length|()}) as list)
end regexMatches
-- roundTo :: Float -> Int -> Float
on roundTo(x, n)
set d to 10 ^ n
(round (x * d)) / d
end roundTo
-- sort :: Ord a => [a] -> [a]
on sort(xs)
((current application's NSArray's arrayWithArray:xs)'s ¬
sortedArrayUsingSelector:"compare:") as list
end sort
-- splitRegex :: Regex -> String -> [String]
on splitRegex(strRegex, str)
set lstMatches to regexMatches(strRegex, str)
Line 229 ⟶ 332:
{str}
end if
end splitRegex
-- unlines :: [String] -> String
on unlines(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set str to xs as text
set my text item delimiters to dlm
str
end unlines
-- unwords :: [String] -> String
on unwords(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, space}
set s to xs as text
set my text item delimiters to dlm
return s
end unwords</lang>
{{Out}}
<pre>▁▁██
0.0 1.0 19.0 20.0
Min 0.0
Mean 10.0
Median 10.0
Max 20.0
▁▁▅▅██
0.0 999.0 4000.0 4999.0 7000.0 7999.0
Min 0.0
Mean 4166.17
Median 4499.5
Max 7999.0
▁▂▃▄▅▆▇█▇▆▅▄▃▂▁
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 1.0
Min 1.0
Mean 4.27
Median 7.0
Max 8.0
▂▁▄▃▆▅█▇
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|AutoHotkey}}==
|