Re: Increasing efficiency in C
From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 03/04/04
- Next message: jacob navia: "Re: Increasing efficiency in C"
- Previous message: Arthur J. O'Dwyer: "[OT] Re: Increasing efficiency in C"
- In reply to: jacob navia: "Re: Increasing efficiency in C"
- Next in thread: jacob navia: "Re: Increasing efficiency in C"
- Reply: jacob navia: "Re: Increasing efficiency in C"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: jacob navia: "Re: Increasing efficiency in C"
- Previous message: Arthur J. O'Dwyer: "[OT] Re: Increasing efficiency in C"
- In reply to: jacob navia: "Re: Increasing efficiency in C"
- Next in thread: jacob navia: "Re: Increasing efficiency in C"
- Reply: jacob navia: "Re: Increasing efficiency in C"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|