Anadromes: Difference between revisions

Content added Content deleted
(Realize in F#)
m (→‎{{header|AppleScript}}: Nicer logic in the second repeat.)
Line 159: Line 159:
set wordArray to (wordArray's filteredArrayUsingPredicate:(filter))
set wordArray to (wordArray's filteredArrayUsingPredicate:(filter))
-- Derive a list of reversed remaining words. This is what takes most of the time.
-- Derive a list of the reversed remaining words. This is what takes most of the time.
script o
script o
property wordList : wordArray as list
property wordList : wordArray as list
Line 166: Line 166:
set AppleScript's text item delimiters to ""
set AppleScript's text item delimiters to ""
repeat with i from 1 to (count o's wordList)
repeat with i from 1 to (count o's wordList)
set item i of o's wordList to (item i of o's wordList)'s characters's reverse as text
set o's wordList's item i to (o's wordList's item i's characters's reverse) as text
end repeat
end repeat
Line 177: Line 177:
set o's wordList to (wordSet's allObjects()'s sortedArrayUsingSelector:("localizedStandardCompare:")) as list
set o's wordList to (wordSet's allObjects()'s sortedArrayUsingSelector:("localizedStandardCompare:")) as list
-- Construct the output line by line, omitting palindromes and word pairs that have been covered already.
-- Construct the output line by line, omitting palindromes and already matched word pairs.
set output to {}
set output to {}
repeat with thisWord in o's wordList
repeat with i from 1 to (count o's wordList)
set reversedWord to thisWord's characters's reverse as text
set thisWord to o's wordList's item i
set o's wordList's item i to missing value
if not ((thisWord's contents is reversedWord) or (output contains {reversedWord & " <--> " & thisWord})) then
set end of output to thisWord & " <--> " & reversedWord
set reversedWord to (thisWord's characters's reverse) as text
if (reversedWord is in o's wordList) then set output's end to thisWord & " <--> " & reversedWord
end if
end repeat
end repeat
set AppleScript's text item delimiters to linefeed
set AppleScript's text item delimiters to linefeed