Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #29933

Re: Favourite Pattern for Processing Zero, Once, Multiple

From Shao Miller <sha0.miller@gmail.com>
Newsgroups comp.lang.c
Subject Re: Favourite Pattern for Processing Zero, Once, Multiple
Date 2013-01-11 00:38 -0500
Organization A noiseless patient Spider
Message-ID <kco8gq$mao$1@dont-email.me> (permalink)
References <kcngpa$2jl$1@dont-email.me> <0.e4da14736427397ca6e1.20130111032010GMT.878v80quc5.fsf@bsb.me.uk>

Show all headers | View raw


On 1/10/2013 22:20, Ben Bacarisse wrote:
> Shao Miller <sha0.miller@gmail.com> writes:
>
>> Suppose you have a function 'bar' that returns a value that means one of:
>>    - Do 'foo' zero times
>>    - Do 'foo' one time
>>    - Do 'foo' more than one time
>>
>> What's your favourite way of handling such a value?  For example:
>>
>>    while ((more = bar()) != cv_zero_times) {
>>        /* Do it at least once */
>>        foo();
>>
>>        /* Do it more than once? */
>>        if (more != cv_more_times)
>>          break;
>>      }
>
> I'd re-think the whole thing.  It's confusing to me since a return of
> "more than one time" does not seem to me any such thing (unless you've
> got your loop wrong).  It seems to mean "do it once and call bar again"
> which is messy because it implies an awkward linkage between bar and
> foo.
>
> Does this crop up for real?  I'd try to find a cleaner way to write the
> whole bar() + foo() interaction.
>

Thanks, BartC, for your response (I sent you an e-mail, but "no such user").

Thanks, Ben, for your response.  Yes, the linkage is awkward!

Yes, 'foo' was just an example of a "do-something."  The actual problem 
that got me thinking about this is a bit boring, but I wondered if 
anyone might have a pattern to suggest.

Consider 'bar' as translating a sequence of characters, one at a time. 
The caller uses it for translation, but it's the caller's responsibility 
to do something useful with the translated characters.

Sometimes 'bar' will emit no translated characters, but simply track 
some state.  ("Deal with zero translated characters.")

Sometimes 'bar' will emit a single translated character that's a simple 
mapping from the input character.  ("Deal with one translated character.")

Sometimes 'bar' will have multiple characters to emit, and the caller 
needs to call it multiple times to get all of the characters.  ("Here's 
one translated character, but call me again because I have more.")

Could be useful for escape sequences: "123${special}ABC" where the '$' 
through until the 'l' wouldn't emit anything, but then the subsequent 
'}' would cause some special sequence to be emitted, where 'bar' would 
know what to emit, but would have to be queried repeatedly until it 
finished emitting the special sequence.

One way to deal with this would be to pass a call-back and context to 
'bar' so that it could do the emitting itself, but that requires the 
programmer to create such a call-back function.

Another way is to use EOF-style, and the case of a single character 
output means that 'bar' is always called a second time simply to return 
end-of-sequence, which thus suffers a bit of a performance drawback for 
that case.

So I thought it was a bit of an interesting challenge and I wonder what 
strategies folks might've or might come up with, as a general pattern.

Thanks for your time.

-- 
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily,"  -- Richard Harter

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Favourite Pattern for Processing Zero, Once, Multiple Shao Miller <sha0.miller@gmail.com> - 2013-01-10 17:53 -0500
  Re: Favourite Pattern for Processing Zero, Once, Multiple "BartC" <bc@freeuk.com> - 2013-01-10 23:26 +0000
    Re: Favourite Pattern for Processing Zero, Once, Multiple Shao Miller <sha0.miller@gmail.com> - 2013-01-10 18:49 -0500
      Re: Favourite Pattern for Processing Zero, Once, Multiple "BartC" <bc@freeuk.com> - 2013-01-11 00:23 +0000
  Re: Favourite Pattern for Processing Zero, Once, Multiple Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-11 03:20 +0000
    Re: Favourite Pattern for Processing Zero, Once, Multiple Shao Miller <sha0.miller@gmail.com> - 2013-01-11 00:38 -0500
      Re: Favourite Pattern for Processing Zero, Once, Multiple Bart van Ingen Schenau <bart@ingen.ddns.info.invalid> - 2013-01-11 12:10 +0000
        Re: Favourite Pattern for Processing Zero, Once, Multiple Shao Miller <sha0.miller@gmail.com> - 2013-01-11 13:37 -0500
      Re: Favourite Pattern for Processing Zero, Once, Multiple "BartC" <bc@freeuk.com> - 2013-01-11 12:15 +0000
        Re: Favourite Pattern for Processing Zero, Once, Multiple Shao Miller <sha0.miller@gmail.com> - 2013-01-11 13:56 -0500
          Re: Favourite Pattern for Processing Zero, Once, Multiple Fred K <fred.l.kleinschmidt@gmail.com> - 2013-01-11 11:15 -0800
            Re: Favourite Pattern for Processing Zero, Once, Multiple Shao Miller <sha0.miller@gmail.com> - 2013-01-11 14:28 -0500
          Re: Favourite Pattern for Processing Zero, Once, Multiple Greg Martin <greg@softsprocket.com> - 2013-01-11 11:22 -0800
            Re: Favourite Pattern for Processing Zero, Once, Multiple Shao Miller <sha0.miller@gmail.com> - 2013-01-11 15:23 -0500
              Re: Favourite Pattern for Processing Zero, Once, Multiple Greg Martin <greg@softsprocket.com> - 2013-01-11 13:09 -0800
          Re: Favourite Pattern for Processing Zero, Once, Multiple "BartC" <bc@freeuk.com> - 2013-01-11 22:09 +0000
          Re: Favourite Pattern for Processing Zero, Once, Multiple Paul N <gw7rib@aol.com> - 2013-01-12 04:49 -0800
            Re: Favourite Pattern for Processing Zero, Once, Multiple Shao Miller <sha0.miller@gmail.com> - 2013-01-13 20:30 -0500
  Re: Favourite Pattern for Processing Zero, Once, Multiple glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-11 21:02 +0000
  Re: Favourite Pattern for Processing Zero, Once, Multiple Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2013-01-13 20:28 +0200
    Re: Favourite Pattern for Processing Zero, Once, Multiple Shao Miller <sha0.miller@gmail.com> - 2013-01-13 16:12 -0500

csiph-web