String case: Difference between revisions

Content deleted Content added
No edit summary
Hout (talk | contribs)
→‎{{header|AppleScript}}: Updated primitives
Line 329: Line 329:
=={{header|AppleScript}}==
=={{header|AppleScript}}==
{{Trans|JavaScript}}
{{Trans|JavaScript}}



AppleScript lacks built in string case functions, but since OS X 10.10 (Yosemite version, Oct 2014) it has been possible to use ObjC Foundation class methods directly in AppleScript code.
AppleScript lacks built in string case functions, but since OS X 10.10 (Yosemite version, Oct 2014) it has been possible to use ObjC Foundation class methods directly in AppleScript code.


<lang applescript>use framework "Foundation"
<lang applescript>use framework "Foundation"

-- toUpperCase :: Text -> Text
on toUpperCase(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
uppercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toUpperCase

-- toLowerCase :: Text -> Text
on toLowerCase(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
lowercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toLowerCase

-- toCapitalized :: Text -> Text
on toCapitalized(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
capitalizedStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toCapitalized




-- TEST -----------------------------------------------------------------------
-- TEST
on run
on run
-- testCase :: Handler -> String
-- testCase :: Handler -> String
script testCase
script testCase
on lambda(f)
on |λ|(f)
mReturn(f)'s lambda("alphaBETA αβγδΕΖΗΘ")
mReturn(f)'s |λ|("alphaBETA αβγδΕΖΗΘ")
end lambda
end |λ|
end script
end script
map(testCase, {toUpperCase, toLowerCase, toCapitalized})
map(testCase, {toUpper, toLower, toTitle})
end run
end run




-- GENERIC FUNCTIONS
-- GENERIC FUNCTIONS ----------------------------------------------------------

-- 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

-- toTitle :: String -> String
on toTitle(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
capitalizedStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toTitle

-- toUpper :: String -> String
on toUpper(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
uppercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toUpper

-- map :: (a -> b) -> [a] -> [b]
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
on map(f, xs)
Line 379: Line 379:
set lst to {}
set lst to {}
repeat with i from 1 to lng
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
set end of lst to |λ|(item i of xs, i, xs)
end repeat
end repeat
return lst
return lst
Line 392: Line 392:
else
else
script
script
property lambda : f
property |λ| : f
end script
end script
end if
end if
end mReturn</lang>
end mReturn</lang>

{{Out}}
{{Out}}
<lang AppleScript>{"ALPHABETA ΑΒΓΔΕΖΗΘ", "alphabeta αβγδεζηθ", "Alphabeta Αβγδεζηθ"}</lang>
<lang AppleScript>{"ALPHABETA ΑΒΓΔΕΖΗΘ", "alphabeta αβγδεζηθ", "Alphabeta Αβγδεζηθ"}</lang>