D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 16079 - memoize should cache objects too
Summary: memoize should cache objects too
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 minor
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-05-26 13:47 UTC by Seb
Modified: 2018-01-05 13:30 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Seb 2016-05-26 13:47:59 UTC
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
Comment 1 Seb 2016-12-27 14:13:34 UTC
sort changes the key -> closing as invalid.
Comment 2 github-bugzilla 2017-07-18 22:28:47 UTC
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
Comment 3 github-bugzilla 2017-08-16 13:22:40 UTC
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
Comment 4 github-bugzilla 2018-01-05 13:30:32 UTC
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