Reverse words in a string: Difference between revisions

m
m (added whitespace to the task's preamble, changed the example text to not use quotation marks.)
Line 216:
<lang AppleScript>on run {}
unlines(map(_linesreverseWords, |lines|("---------- Ice and Fire ------------
 
fire, in end will world the say Some
ice. in say Some
desire of tasted I've what From
fire. favor who those with hold I
 
... elided paragraph last ...
 
Frost Robert -----------------------"), reverseWords))
end run
 
-- reverseWords :: String -> String
on reverseWords(str)
unwords(_reverse|reverse|(_words|words|(str)))
end reverseWords
 
-- |reverse| :: [a] -> [a]
on _reverse|reverse|(xs)
if class of xs is text then
(reverse of characters of xs) as text
Line 241:
reverse of xs
end if
end _reverse|reverse|
 
-- |lines| :: Text -> [Text]
on _lines|lines|(str)
splitOn(linefeed, str)
end _lines|lines|
 
-- |words| :: Text -> [Text]
on _words|words|(str)
splitOn(space, str)
end _words|words|
 
-- ulines :: [Text] -> Text
on unlines(lstLines)
intercalate(linefeed, lstLines)
end unlines
 
-- unwords :: [Text] -> Text
on unwords(lstWords)
intercalate(space, lstWords)
end unwords
 
-- splitOn :: Text -> Text -> [Text]
on splitOn(strDelim, strMain)
set {dlm, my text item delimiters} to {my text item delimiters, strDelim}
set lstParts to text items of strMain
set my text item delimiters to dlm
return lstParts
end splitOn
 
-- 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
 
-- [a]map ->:: (a -> b) -> [a] -> [b]
on map(xsf, fxs)
setscript mf to mReturn(f)
property calllambda : f
end script
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set end of lst to mf's calllambda(item i of xs, i, xs)
end repeat
return lst
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}}
 
9,659

edits