[Issue 16079] New: memoize should cache objects too

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu May 26 06:47:59 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=16079

          Issue ID: 16079
           Summary: memoize should cache objects too
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: minor
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: greensunny12 at gmail.com

I don't see any reason why memoize should execute this code twice.

   int executed = 0;
    T median(T)(T[] nums) {
        import std.algorithm: sort;
        executed++;
        nums.sort();
        if (nums.length % 2)
            return nums[$ / 2];
        else
            return (nums[$ / 2 - 1]
                + nums[$ / 2]) / 2;
    }

    alias fastMedian = memoize!(median!int);

    assert(fastMedian([7, 5, 3]) == 5);
    assert(fastMedian([7, 5, 3]) == 5);

    assert(executed == 1); // ERROR, 2

--


More information about the Digitalmars-d-bugs mailing list