Comma quibbling: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: future-proof for 5.36, explicit :prototype) |
|||
Line 1,839: | Line 1,839: | ||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
Long solution: |
|||
<syntaxhighlight lang="futurebasic"> |
<syntaxhighlight lang="futurebasic"> |
||
include "NSLog.incl" |
include "NSLog.incl" |
||
Line 1,875: | Line 1,876: | ||
NSLog( @"%@", fn CommaQuibber( @"[\"ABC\", \"DEF\"]" ) ) |
NSLog( @"%@", fn CommaQuibber( @"[\"ABC\", \"DEF\"]" ) ) |
||
NSLog( @"%@", fn CommaQuibber( @"[\"ABC\", \"DEF\", \"G\", \"H\"]" ) ) |
NSLog( @"%@", fn CommaQuibber( @"[\"ABC\", \"DEF\", \"G\", \"H\"]" ) ) |
||
HandleEvents |
|||
</syntaxhighlight> |
|||
Short solution: |
|||
<syntaxhighlight lang="futurebasic"> |
|||
local fn CommaQuibbler( s as CFArrayRef ) as CFStringRef |
|||
CFStringRef result = NULL |
|||
select ( len(s) ) |
|||
case 0: exit fn = @"{}" |
|||
case 1: exit fn = fn StringWithFormat( @"{%@}", s[0] ) |
|||
case 2: exit fn = fn StringWithFormat( @"{%@ and %@}", s[0], s[1] ) |
|||
case else |
|||
result = fn StringWithFormat( @"{%@}", fn ArrayComponentsJoinedByString( s, @", " ) ) |
|||
CFRange lastComma = fn StringRangeOfStringWithOptions( result, @",", NSBackwardsSearch ) |
|||
result = fn StringByReplacingCharactersInRange( result, lastComma, @" and" ) |
|||
end select |
|||
end fn = result |
|||
print fn CommaQuibbler( @[] ) |
|||
print fn CommaQuibbler( @[@"ABC"] ) |
|||
print fn CommaQuibbler( @[@"ABC", @"DEF"] ) |
|||
print fn CommaQuibbler( @[@"ABC", @"DEF", @"G", @"H"] ) |
|||
HandleEvents |
HandleEvents |