Reverse words in a string: Difference between revisions
Content added Content deleted
m (added whitespace to the task's preamble, changed the example text to not use quotation marks.) |
|||
Line 216: | Line 216: | ||
<lang AppleScript>on run {} |
<lang AppleScript>on run {} |
||
unlines(map( |
unlines(map(reverseWords, |lines|("---------- Ice and Fire ------------ |
||
fire, in end will world the say Some |
fire, in end will world the say Some |
||
ice. in say Some |
ice. in say Some |
||
desire of tasted I've what From |
desire of tasted I've what From |
||
fire. favor who those with hold I |
fire. favor who those with hold I |
||
... elided paragraph last ... |
... elided paragraph last ... |
||
Frost Robert -----------------------") |
Frost Robert -----------------------"))) |
||
end run |
end run |
||
-- String -> String |
-- reverseWords :: String -> String |
||
on reverseWords(str) |
on reverseWords(str) |
||
unwords( |
unwords(|reverse|(|words|(str))) |
||
end reverseWords |
end reverseWords |
||
-- [a] -> [a] |
-- |reverse| :: [a] -> [a] |
||
on |
on |reverse|(xs) |
||
if class of xs is text then |
if class of xs is text then |
||
(reverse of characters of xs) as text |
(reverse of characters of xs) as text |
||
Line 241: | Line 241: | ||
reverse of xs |
reverse of xs |
||
end if |
end if |
||
end |
end |reverse| |
||
-- Text -> [Text] |
-- |lines| :: Text -> [Text] |
||
on |
on |lines|(str) |
||
splitOn(linefeed, str) |
splitOn(linefeed, str) |
||
end |
end |lines| |
||
-- Text -> [Text] |
-- |words| :: Text -> [Text] |
||
on |
on |words|(str) |
||
splitOn(space, str) |
splitOn(space, str) |
||
end |
end |words| |
||
-- [Text] -> Text |
-- ulines :: [Text] -> Text |
||
on unlines(lstLines) |
on unlines(lstLines) |
||
intercalate(linefeed, lstLines) |
intercalate(linefeed, lstLines) |
||
end unlines |
end unlines |
||
-- [Text] -> Text |
-- unwords :: [Text] -> Text |
||
on unwords(lstWords) |
on unwords(lstWords) |
||
intercalate(space, lstWords) |
intercalate(space, lstWords) |
||
end unwords |
end unwords |
||
-- Text -> Text -> [Text] |
-- splitOn :: Text -> Text -> [Text] |
||
on splitOn(strDelim, strMain) |
on splitOn(strDelim, strMain) |
||
set {dlm, my text item delimiters} to {my text item delimiters, strDelim} |
set {dlm, my text item delimiters} to {my text item delimiters, strDelim} |
||
set lstParts to text items of strMain |
set lstParts to text items of strMain |
||
set my text item delimiters to dlm |
set my text item delimiters to dlm |
||
lstParts |
|||
end splitOn |
end splitOn |
||
-- Text -> [Text] -> Text |
-- interCalate :: Text -> [Text] -> Text |
||
on intercalate(strText, lstText) |
on intercalate(strText, lstText) |
||
set {dlm, my text item delimiters} to {my text item delimiters, strText} |
set {dlm, my text item delimiters} to {my text item delimiters, strText} |
||
set strJoined to lstText as text |
set strJoined to lstText as text |
||
set my text item delimiters to dlm |
set my text item delimiters to dlm |
||
strJoined |
|||
end intercalate |
end intercalate |
||
-- |
-- map :: (a -> b) -> [a] -> [b] |
||
on map( |
on map(f, xs) |
||
script mf |
|||
⚫ | |||
⚫ | |||
set lst to {} |
set lst to {} |
||
set lng to length of xs |
set lng to length of xs |
||
repeat with i from 1 to lng |
repeat with i from 1 to lng |
||
set end of lst to mf's |
set end of lst to mf's lambda(item i of xs, i, xs) |
||
end repeat |
end repeat |
||
return lst |
return lst |
||
end map |
end map</lang> |
||
-- lift 2nd class function into 1st class wrapper |
|||
-- handler function --> first class script object |
|||
on mReturn(f) |
|||
script mf |
|||
⚫ | |||
⚫ | |||
end mReturn</lang> |
|||
{{out}} |
{{out}} |
||