RosettaCodeData/Task/Pangram-checker/AppleScript/pangram-checker-1.applescript

72 lines
1.8 KiB
AppleScript
Raw Permalink Normal View History

2016-12-05 22:15:40 +01:00
use framework "Foundation" -- ( for case conversion function )
2018-06-22 20:57:24 +00:00
-- PANGRAM CHECK -------------------------------------------------------------
2016-12-05 22:15:40 +01:00
-- isPangram :: String -> Bool
on isPangram(s)
script charUnUsed
2018-06-22 20:57:24 +00:00
property lowerCaseString : my toLower(s)
on |λ|(c)
2016-12-05 22:15:40 +01:00
lowerCaseString does not contain c
2018-06-22 20:57:24 +00:00
end |λ|
2016-12-05 22:15:40 +01:00
end script
length of filter(charUnUsed, "abcdefghijklmnopqrstuvwxyz") = 0
end isPangram
2018-06-22 20:57:24 +00:00
-- TEST ----------------------------------------------------------------------
2016-12-05 22:15:40 +01:00
on run
map(isPangram, {¬
"is this a pangram", ¬
"The quick brown fox jumps over the lazy dog"})
--> {false, true}
end run
2018-06-22 20:57:24 +00:00
-- GENERIC FUNCTIONS ---------------------------------------------------------
2016-12-05 22:15:40 +01:00
-- filter :: (a -> Bool) -> [a] -> [a]
on filter(f, xs)
tell mReturn(f)
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
2018-06-22 20:57:24 +00:00
if |λ|(v, i, xs) then set end of lst to v
2016-12-05 22:15:40 +01:00
end repeat
return lst
end tell
end filter
-- 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
2018-06-22 20:57:24 +00:00
set end of lst to |λ|(item i of xs, i, xs)
2016-12-05 22:15:40 +01:00
end repeat
return lst
end tell
end map
-- 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
2018-06-22 20:57:24 +00:00
property |λ| : f
2016-12-05 22:15:40 +01:00
end script
end if
end mReturn
2018-06-22 20:57:24 +00:00
-- toLower :: String -> String
on toLower(str)
2016-12-05 22:15:40 +01:00
set ca to current application
2018-06-22 20:57:24 +00:00
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLower