Comma quibbling: Difference between revisions

Content deleted Content added
Chunes (talk | contribs)
Chunes (talk | contribs)
→‎{{header|Factor}}: simplify with a fancy pattern matcher
Line 1,224:
 
=={{header|Factor}}==
This example uses the <code>inverse</code> vocabulary, which builds on the concept of invertible quotations as the basis for pattern matching. It is discussed at length in this approachable [http://micsymposium.org/mics_2009_proceedings/mics2009_submission_72.pdf paper].
<lang factor>USING: formatting generalizationsinverse qw sequences ;
: (quibble) ( seq -- strseq' )
dup length {
{ 0 [ drop{ } ] [ "" ] }
{ 1 [ first1array ] [ ] }
{ 2 [ first22array ] [ " and " glue ] }
[ drop unclip-last [swap (quibble) ", " join ] dip " and " glue ]
} case "{%s}" sprintfswitch ;
 
: quibble ( seq -- str ) (quibble) "{%s}" sprintf ;
 
{ } qw{ ABC } qw{ ABC DEF } qw{ ABC DEF G H }