Welcome to Web-News
A Web-based News Reader
Subject Re: Can we get rid of opApply?
From Denis Koroskin <2korden@gmail.com>
Date Tue, 20 Jan 2009 12:23:58 +0300
Newsgroups digitalmars.D

One nice thing that opApply capable of is it can avoid heap activity by stack-allocating data during iteration.

For example, given an array of ints, iterate over string representations of them:

struct IntegersAsString
{
    void opAplly(int delegate(string s) dg)
    {
        char[16] temp;

        foreach (i; array) {
            int len = sprintf(temp, "%d", i);
            int result = dg(temp[0..len]);
            if (result != 0) {
                return result;
            }
        }

        return 0;
    }

    private int[] array;
}

int array = [1, 1, 2, 3, 5, 8, 13];

// no heap allocation take place
foreach (string s; IntegersAsString(array)) {
    writeln(s);
}

How would you do that with ranges?

Recent messages in this thread
 
-# Can we get rid of opApply? Steven Schveighoffer 19-Jan-2009 03:19 pm
.|# Re: Can we get rid of opApply? Steven Schveighoffer 19-Jan-2009 03:22 pm
.-# Re: Can we get rid of opApply? Andrei Alexandrescu 19-Jan-2009 04:00 pm
.|-# Re: Can we get rid of opApply? Steven Schveighoffer 19-Jan-2009 04:17 pm
.||-# Re: Can we get rid of opApply? Chad J 19-Jan-2009 05:11 pm
.|||\# Re: Can we get rid of opApply? Andrei Alexandrescu 19-Jan-2009 09:33 pm
.||-# Re: Can we get rid of opApply? Andrei Alexandrescu 19-Jan-2009 11:38 pm
.||.|# Re: Can we get rid of opApply? Jason House 20-Jan-2009 09:29 am
.||.\# Re: Can we get rid of opApply? Steven Schveighoffer 20-Jan-2009 10:05 am
.|\# Re: Can we get rid of opApply? Daniel Keep 19-Jan-2009 06:42 pm
.-# Re: Can we get rid of opApply? (Current message) Denis Koroskin 20-Jan-2009 04:23 am
..-# Re: Can we get rid of opApply? Max Samukha 20-Jan-2009 04:51 am
..|-# Re: Can we get rid of opApply? dsimcha 20-Jan-2009 08:23 am
..|||# Re: Can we get rid of opApply? bearophile 20-Jan-2009 09:09 am
..||-# Re: Can we get rid of opApply? Andrei Alexandrescu 20-Jan-2009 09:44 am
..||.-# Re: Can we get rid of opApply? dsimcha 20-Jan-2009 09:59 am
..||.||# Re: Can we get rid of opApply? Andrei Alexandrescu 20-Jan-2009 10:32 am
..||.|-# Re: Can we get rid of opApply? Steven Schveighoffer 20-Jan-2009 11:36 am
..||.|.-# Re: Can we get rid of opApply? Andrei Alexandrescu 20-Jan-2009 12:08 pm
..||.|.||# Re: Can we get rid of opApply? Steven Schveighoffer 20-Jan-2009 12:23 pm
..||.|.|\# Re: Can we get rid of opApply? Max Samukha 20-Jan-2009 12:32 pm
..||.|.-# Re: Can we get rid of opApply? Max Samukha 20-Jan-2009 12:21 pm
..||.|..-# Re: Can we get rid of opApply? Jarrett Billingsley 20-Jan-2009 12:30 pm
..||.|..|-# Re: Can we get rid of opApply? Max Samukha 21-Jan-2009 02:35 am
..||.|..|.\# Re: Can we get rid of opApply? Max Samukha 21-Jan-2009 02:41 am
..||.|..-# Re: Can we get rid of opApply? Andrei Alexandrescu 20-Jan-2009 12:32 pm
..||.|...\# Re: Can we get rid of opApply? Max Samukha 21-Jan-2009 02:36 am
..||.-# Re: Can we get rid of opApply? Fawzi Mohamed 20-Jan-2009 10:07 am
..||..-# Re: Can we get rid of opApply? Steven Schveighoffer 20-Jan-2009 11:49 am
..||...-# Re: Can we get rid of opApply? Robert Fraser 20-Jan-2009 01:13 pm
..||....-# Re: Can we get rid of opApply? Steven Schveighoffer 20-Jan-2009 01:50 pm
..||.....-# Re: Can we get rid of opApply? Daniel Keep 20-Jan-2009 05:31 pm
..||......-# Re: Can we get rid of opApply? Steven Schveighoffer 20-Jan-2009 05:38 pm
..||.......-# Re: Can we get rid of opApply? Denis Koroskin 20-Jan-2009 06:01 pm
..||........\# Re: Can we get rid of opApply? Steven Schveighoffer 20-Jan-2009 06:18 pm
..|\# Re: Can we get rid of opApply? Andrei Alexandrescu 20-Jan-2009 09:43 am
..\# Re: Can we get rid of opApply? Andrei Alexandrescu 20-Jan-2009 09:42 am