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: Line 1,224:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: combinators generalizations io kernel qw sequences ;
<lang factor>USING: formatting generalizations qw ;
IN: rosetta-code.comma-quibble
: quibble ( 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 {
dup length {
{ 0 [ drop "{}" ] }
{ 0 [ drop "" ] }
{ 1 [ first wrap ] }
{ 1 [ first ] }
{ 2 [ quibble-pair ] }
{ 2 [ first2 " and " glue ] }
[ drop quibble-list ]
[ drop unclip-last [ ", " join ] dip " and " glue ]
} case ;
} case "{%s}" sprintf ;


{ } qw{ ABC } qw{ ABC DEF } qw{ ABC DEF G H }
{ } qw{ ABC } qw{ ABC DEF } qw{ ABC DEF G H }
[ comma-quibble print ] 4 napply</lang>
[ quibble print ] 4 napply</lang>
{{out}}
{{out}}
<pre>
<pre>