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
sort changes the key -> closing as invalid.
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/9e58ecab2aa6eb2d800d1e7e0635f7eee1a774aa Fix issue 16079 - memoize should work with arrays and objects https://github.com/dlang/phobos/commit/aefb5892a3ee1dbbb3433db951e261f3a1b5635c Merge pull request #4367 from wilzbach/fix_16079 Fix issue 16079 - memoize should work with arrays and objects
Commits pushed to stable at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/9e58ecab2aa6eb2d800d1e7e0635f7eee1a774aa Fix issue 16079 - memoize should work with arrays and objects https://github.com/dlang/phobos/commit/aefb5892a3ee1dbbb3433db951e261f3a1b5635c Merge pull request #4367 from wilzbach/fix_16079
Commit pushed to dmd-cxx at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/aefb5892a3ee1dbbb3433db951e261f3a1b5635c Merge pull request #4367 from wilzbach/fix_16079