RosettaCodeData/Task/Palindrome-detection/AppleScript/palindrome-detection.applescript

73 lines
1.8 KiB
AppleScript
Raw Permalink Normal View History

2016-12-05 22:15:40 +01:00
use framework "Foundation"
2017-09-23 10:01:46 +02:00
-- CASE-INSENSITIVE PALINDROME, IGNORING SPACES ? ----------------------------
2016-12-05 22:15:40 +01:00
-- isPalindrome :: String -> Bool
on isPalindrome(s)
s = intercalate("", reverse of characters of s)
end isPalindrome
2017-09-23 10:01:46 +02:00
-- toSpaceFreeLower :: String -> String
on spaceFreeToLower(s)
script notSpace
on |λ|(s)
s is not space
end |λ|
end script
2016-12-05 22:15:40 +01:00
2017-09-23 10:01:46 +02:00
intercalate("", filter(notSpace, characters of toLower(s)))
end spaceFreeToLower
2016-12-05 22:15:40 +01:00
2017-09-23 10:01:46 +02:00
-- TEST ----------------------------------------------------------------------
2016-12-05 22:15:40 +01:00
on run
2017-09-23 10:01:46 +02:00
isPalindrome(spaceFreeToLower("In girum imus nocte et consumimur igni"))
2016-12-05 22:15:40 +01:00
--> true
end run
2017-09-23 10:01:46 +02: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
2017-09-23 10:01:46 +02: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
2017-09-23 10:01:46 +02:00
-- 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
2016-12-05 22:15:40 +01:00
-- 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
2017-09-23 10:01:46 +02:00
property |λ| : f
2016-12-05 22:15:40 +01:00
end script
end if
end mReturn
2017-09-23 10:01:46 +02:00
-- toLower :: String -> String
on toLower(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLower