92 lines
2.1 KiB
AppleScript
92 lines
2.1 KiB
AppleScript
-------------------- ALPHABETIC SERIES -------------------
|
||
on run
|
||
unlines(map(concat, ¬
|
||
({enumFromTo("a", "z"), ¬
|
||
enumFromTo("🐟", "🐐"), ¬
|
||
enumFromTo("z", "a"), ¬
|
||
enumFromTo("α", "ω")})))
|
||
end run
|
||
|
||
-------------------- GENERIC FUNCTIONS -------------------
|
||
|
||
-- 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 :: Enum a => a -> a -> [a]
|
||
on enumFromTo(m, n)
|
||
if class of m is integer then
|
||
enumFromToInt(m, n)
|
||
else
|
||
enumFromToChar(m, n)
|
||
end if
|
||
end enumFromTo
|
||
|
||
-- enumFromToChar :: Char -> Char -> [Char]
|
||
on enumFromToChar(m, n)
|
||
set {intM, intN} to {id of m, id of n}
|
||
set xs to {}
|
||
repeat with i from intM to intN by signum(intN - intM)
|
||
set end of xs to character id i
|
||
end repeat
|
||
return xs
|
||
end enumFromToChar
|
||
|
||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||
on mReturn(f)
|
||
-- 2nd class handler function lifted into 1st class script wrapper.
|
||
if script is class of f then
|
||
f
|
||
else
|
||
script
|
||
property |λ| : f
|
||
end script
|
||
end if
|
||
end mReturn
|
||
|
||
-- map :: (a -> b) -> [a] -> [b]
|
||
on map(f, xs)
|
||
-- The list obtained by applying f
|
||
-- to each element of 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 |λ|(item i of xs, i, xs)
|
||
end repeat
|
||
return lst
|
||
end tell
|
||
end map
|
||
|
||
-- signum :: Num -> Num
|
||
on signum(x)
|
||
if x < 0 then
|
||
-1
|
||
else if x = 0 then
|
||
0
|
||
else
|
||
1
|
||
end if
|
||
end signum
|
||
|
||
-- unlines :: [String] -> String
|
||
on unlines(xs)
|
||
-- A single string formed by the intercalation
|
||
-- of a list of strings with the newline character.
|
||
set {dlm, my text item delimiters} to ¬
|
||
{my text item delimiters, linefeed}
|
||
set s to xs as text
|
||
set my text item delimiters to dlm
|
||
s
|
||
end unlines
|