digitalmars.D.bugs - [Issue 16079] New: memoize should cache objects too
- via Digitalmars-d-bugs (30/30) May 26 2016 https://issues.dlang.org/show_bug.cgi?id=16079
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 puremagic.com Reporter: greensunny12 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 --
May 26 2016