Comma quibbling: Difference between revisions

Content deleted Content added
Chunes (talk | contribs)
m →‎{{header|Factor}}: give the comma-quibble word fewer responsibilities
Chunes (talk | contribs)
Line 1,224:
 
=={{header|Factor}}==
<lang factor>USING: combinatorsformatting generalizations io kernel qw sequences ;
IN: rosetta-code.comma-quibble
: quibble-list ( seq -- str )
 
: wrap ( str -- str' ) "{" prepend "}" append ;
 
: quibble-pair ( seq -- str ) " and " join wrap ;
 
: quibble-list ( seq -- str )
[ but-last ] [ last ] bi [ ", " join ] dip { } 2sequence
quibble-pair ;
 
: comma-quibble ( seq -- str )
dup length {
{ 0 [ drop "{}" ] }
{ 1 [ first wrap ] }
{ 2 [ quibble-pairfirst2 " and " glue ] }
[ drop quibbleunclip-listlast [ ", " join ] dip " and " glue ]
} case "{%s}" sprintf ;
 
{ } qw{ ABC } qw{ ABC DEF } qw{ ABC DEF G H }
[ comma-quibble print ] 4 napply</lang>
{{out}}
<pre>