Re: Increasing efficiency in C

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 03/04/04


Date: Thu, 4 Mar 2004 17:53:48 -0500 (EST)


On Thu, 4 Mar 2004, jacob navia wrote:
>
> I am writing map_string. Will take a function and return a string
> built with the results of applying the function to each character.

You *do* realize this is a one-liner, right?

void mapstr(char *d, char *s, int(*f)(int))
{
    while (*s)
      *d++ = f(*s++);
    *d = '\0';
    return;
}

  Possible enhancements: Allow 'mapstr(s,t,0)' as a synonym
for 'strcpy(s,t)'. Control for the possibility that f(k)
equals zero for some k!=0. If d is NULL, allocate and return
space for the resulting string using 'malloc' or a static buffer.
<OT>
Implement 'foldl' and/or 'foldr' over strings (although 'foldr'
would probably be silly, and both really require template
programming to be useful, which C doesn't have).
</OT>

-Arthur



Relevant Pages

  • Re: OT: Iraqi elections
    ... > length of string from one corner to the opposing corner, ... the ancient pyramids of Egypt could only have been built by aliens were: ... * To join/leave the list, search archives, change list settings, * ...
    (comp.sys.hp.mpe)
  • Re: OT: Iraqi elections
    ... > length of string from one corner to the opposing corner, ... the ancient pyramids of Egypt could only have been built by aliens were: ... You simply flood it and fill in the low spots and level off the high areas. ...
    (comp.sys.hp.mpe)
  • Re: Writing idiomatically for the cello and other string instruments
    ... writing for string instrument, specifically the cello. ... Also, get with a cello player and talk, ... Most of us sadly dont have a string player easily available- ...
    (rec.music.theory)
  • Re: Rewrite last line in text file
    ... Now parse the buffer backwards, ... TFileStream to that point ... start writing your replacement string. ...
    (borland.public.delphi.language.objectpascal)
  • Re: Larkin, Power BASIC cannot be THAT good:
    ... features you need built in? ... our ethernet-version products. ... tcp/ip session, one line to send a string, one line to get a string, ... All built in, called with plain English words (like ...
    (sci.electronics.design)