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(_lines("---------- Ice and Fire ------------
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 -----------------------"), reverseWords))
Frost Robert -----------------------")))
end run
end run


-- String -> String
-- reverseWords :: String -> String
on reverseWords(str)
on reverseWords(str)
unwords(_reverse(_words(str)))
unwords(|reverse|(|words|(str)))
end reverseWords
end reverseWords


-- [a] -> [a]
-- |reverse| :: [a] -> [a]
on _reverse(xs)
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 _reverse
end |reverse|


-- Text -> [Text]
-- |lines| :: Text -> [Text]
on _lines(str)
on |lines|(str)
splitOn(linefeed, str)
splitOn(linefeed, str)
end _lines
end |lines|


-- Text -> [Text]
-- |words| :: Text -> [Text]
on _words(str)
on |words|(str)
splitOn(space, str)
splitOn(space, str)
end _words
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
return lstParts
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
return strJoined
strJoined
end intercalate
end intercalate


-- [a] -> (a -> b) -> [b]
-- map :: (a -> b) -> [a] -> [b]
on map(xs, f)
on map(f, xs)
set mf to mReturn(f)
script mf
property lambda : f
end script
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 call(item i of xs, i, xs)
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
property call : f
end script
end mReturn</lang>
{{out}}
{{out}}