Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
124
Task/Phrase-reversals/AppleScript/phrase-reversals-1.applescript
Normal file
124
Task/Phrase-reversals/AppleScript/phrase-reversals-1.applescript
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
-- REVERSED PHRASES, COMPONENT WORDS, AND WORD ORDER ---------------------
|
||||
|
||||
-- reverseString, reverseEachWord, reverseWordOrder :: String -> String
|
||||
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"}))
|
||||
|
||||
-->
|
||||
|
||||
-- "lasrever esarhp edoc attesor
|
||||
-- attesor edoc esarhp lasrever
|
||||
-- reversal phrase code rosetta"
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS ---------------------------------------------------------
|
||||
|
||||
-- A list of functions applied to a list of arguments
|
||||
-- (<*> | ap) :: [(a -> b)] -> [a] -> [b]
|
||||
on |<*>|(fs, xs)
|
||||
set {nf, nx} to {length of fs, length of xs}
|
||||
set acc to {}
|
||||
repeat with i from 1 to nf
|
||||
tell mReturn(item i of fs)
|
||||
repeat with j from 1 to nx
|
||||
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]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- 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
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
set aString to "rosetta code phrase reversal"
|
||||
|
||||
set astid to AppleScript's text item delimiters
|
||||
|
||||
set AppleScript's text item delimiters to ""
|
||||
set phrase1 to (reverse of characters of aString) as text
|
||||
|
||||
set AppleScript's text item delimiters to space
|
||||
set phrase2 to (reverse of words of phrase1) as text
|
||||
|
||||
set phrase3 to (reverse of words of aString) as text
|
||||
|
||||
set AppleScript's text item delimiters to linefeed
|
||||
set output to {phrase1, phrase2, phrase3} as text
|
||||
|
||||
set AppleScript's text item delimiters to astid
|
||||
|
||||
return output
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
"lasrever esarhp edoc attesor
|
||||
attesor edoc esarhp lasrever
|
||||
reversal phrase code rosetta"
|
||||
Loading…
Add table
Add a link
Reference in a new issue