Pangram checker: Difference between revisions
Content added Content deleted
m (added parenthesis for sentence clarity.) |
|||
Line 177: | Line 177: | ||
1 |
1 |
||
</lang> |
</lang> |
||
=={{header|AppleScript}}== |
|||
Out of the box, AppleScript lacks many library basics – no regex, no higher order functions, not even string functions for mapping to upper or lower case. |
|||
From OSX 10.10 onwards, we can, however, use ObjC functions from AppleScript by importing the Foundation framework. We do this below to get a toLowerCase() function. If we also add generic filter and map functions, we can write and test a simple isPangram() function as follows: |
|||
<lang AppleScript>use framework "Foundation" |
|||
on run |
|||
map(isPangram, {¬ |
|||
"is this a pangram", ¬ |
|||
"The quick brown fox jumps over the lazy dog"}) |
|||
end run |
|||
-- isPangram :: String -> Bool |
|||
on isPangram(s) |
|||
length of ¬ |
|||
filter(mClosure(my charUnused, ¬ |
|||
{lowerCaseString:toLowerCase(s)}), ¬ |
|||
"abcdefghijklmnopqrstuvwxyz") = 0 |
|||
end isPangram |
|||
-- charUnUsed :: Character -> Bool |
|||
on charUnused(c) |
|||
my closure's lowerCaseString does not contain c |
|||
end charUnused |
|||
-- GENERIC HIGHER ORDER FUNCTIONS (FILTER AND MAP) |
|||
-- (a -> Bool) -> [a] -> [a] |
|||
on filter(f, xs) |
|||
set mf to 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 |
|||
if mf's lambda(v, i, xs) then |
|||
set end of lst to v |
|||
end if |
|||
end repeat |
|||
return lst |
|||
end filter |
|||
-- map :: (a -> b) -> [a] -> [b] |
|||
on map(f, xs) |
|||
set mf to mReturn(f) |
|||
set lng to length of xs |
|||
set lst to {} |
|||
repeat with i from 1 to lng |
|||
set end of lst to mf's lambda(item i of xs, i, xs) |
|||
end repeat |
|||
return lst |
|||
end map |
|||
-- Handler -> Record -> Script |
|||
on mClosure(f, recBindings) |
|||
script |
|||
property closure : recBindings |
|||
property lambda : f |
|||
end script |
|||
end mClosure |
|||
-- Lift 2nd class function into 1st class wrapper |
|||
-- handler function --> first class script object |
|||
on mReturn(f) |
|||
if class of f is script then return f |
|||
script |
|||
property lambda : f |
|||
end script |
|||
end mReturn |
|||
-- OBJC function: lowercaseStringWithLocale |
|||
-- toLowerCase :: String -> String |
|||
on toLowerCase(str) |
|||
set ca to current application |
|||
unwrap(wrap(str)'s ¬ |
|||
lowercaseStringWithLocale:(ca's NSLocale's currentLocale)) |
|||
end toLowerCase |
|||
-- wrap :: AS value -> NSObject |
|||
on wrap(v) |
|||
set ca to current application |
|||
ca's (NSArray's arrayWithObject:v)'s objectAtIndex:0 |
|||
end wrap |
|||
-- unwrap :: NSObject -> AS value |
|||
on unwrap(objCValue) |
|||
if objCValue is missing value then |
|||
return missing value |
|||
else |
|||
set ca to current application |
|||
item 1 of ((ca's NSArray's arrayWithObject:objCValue) as list) |
|||
end if |
|||
end unwrap |
|||
</lang> |
|||
{{Out}} |
|||
<pre>{false, true}</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |