Jump to content

Phrase reversals: Difference between revisions

→‎{{header|AppleScript}}: Rearranged to foreground three functions
(→‎JS ES6: Reshaped to foreground the core.)
(→‎{{header|AppleScript}}: Rearranged to foreground three functions)
Line 188:
Here is a simple illustration of unifying (and elevating) the function type by wrapping the built-in functions in user handlers (perhaps making some of them polymorphic where needed), and also obtaining first class status for ordinary user handler functions by 'lifting' them (for use as arguments in higher order functions) into a first class script object. (This process can be inlined, or abstracted out to an '''mReturn''' or '''mInject''' function).
 
<lang AppleScript>on-- runREVERSED PHRASES, COMPONENT WORDS, AND WORD ORDER ---------------------
 
-- reverseString, reverseEachWord, reverseWordOrder :: String -> String
set phrase to "rosetta code phrase reversal"
on stringReverse(s)
|reverse|(s)
end stringReverse
 
on reverseEachWord(s)
wordLevel(curry(my map)'s |λ|(my |reverse|))'s |λ|(s)
end reverseEachWord
 
on reverseWordOrder(s)
wordLevel(my |reverse|)'s |λ|(s)
end reverseWordOrder
 
 
-- wordLevel :: ([String] -> [String]) -> String -> String
on wordLevel(f)
script
on |λ|(x)
unwords(mReturn(f)'s |λ|(|words|(x)))
end |λ|
end script
end wordLevel
 
 
-- TEST ----------------------------------------------------------------------
on run
unlines(|<*>|({stringReverse, reverseEachWord, reverseWordOrder}, ¬
{"rosetta code phrase reversal"}))
unlines({¬-->
_reverse(phrase), ¬
unwords(map(_reverse, _words(phrase))), ¬
unwords(_reverse(_words(phrase)))})
-- "lasrever esarhp edoc attesor
-- attesor edoc esarhp lasrever
-- reversal phrase code rosetta"
end run
 
Line 202 ⟶ 229:
-- GENERIC FUNCTIONS ---------------------------------------------------------
 
-- A list of functions applied to a list of arguments
-- _reverse :: [a] -> [a]
-- (<*> | ap) :: [(a -> b)] -> [a] -> [b]
on _reverse(xs)
on |<*>|(fs, xs)
if class of xs is text then
set {nf, nx} to (reverse{length of charactersfs, length of xs) as text}
elseset acc to {}
repeat with i from reverse1 ofto xsnf
tell mReturn(item i of fs)
end if
repeat with j from 1 to nx
end _reverse
set end of acc to |λ|(contents of (item j of xs))
end repeat
end tell
end repeat
return acc
end |<*>|
 
-- curry :: (Script|Handler) -> Script
on curry(f)
script
on |λ|(a)
script
on |λ|(b)
|λ|(a, b) of mReturn(f)
end |λ|
end script
end |λ|
end script
end curry
 
-- 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
 
-- map :: (a -> b) -> [a] -> [b]
Line 217 ⟶ 271:
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda|λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
-- 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
 
-- _words :: String -> [String]
on _words(str)
words of str
end _words
 
-- unlines :: [String] -> String
on unlines(lstLines)
intercalate(linefeed, lstLines)
end unlines
 
-- unwords :: [String] -> String
on unwords(lstWords)
intercalate(space, lstWords)
end unwords
 
-- Lift 2nd class handler function into 1st class script wrapper
Line 253 ⟶ 284:
else
script
property lambda|λ| : f
end script
end if
end mReturn</lang>
 
-- reverse :: [a] -> [a]
on |reverse|(xs)
if class of xs is text then
(reverse of characters of xs) as text
else
reverse of xs
end if
end |reverse|
 
-- words :: String -> [String]
on |words|(s)
words of s
end |words|
 
-- unlines :: [String] -> String
on unlines(lstLines)
intercalate(linefeed, lstLines)
end unlines
 
-- unwords :: [String] -> String
on unwords(lstWords)
intercalate(space, lstWords)
end unwords</lang>
{{out}}
<pre>"lasrever esarhp edoc attesor
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.