Clojure

Change select-keys to return same type of map as it is given

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Declined
  • Affects Version/s: Release 1.5
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Patch:
    Code and Test

Description

select-keys always returns a map built up from {}. This will sometimes not be the same type of map as the one given as the first arg, e.g. if the first arg is a sorted-map, the returned map will not be. There are situations where it would be nice if it preserved the kind of map it was given, e.g. the developer wishes to preserve the sorting order of the keys.

Discussion thread on Clojure group: https://groups.google.com/forum/#!topic/clojure/l_V1N1nRF-c

  1. clj-1287-1.diff
    01/Nov/13 9:36 AM
    3 kB
    Andy Fingerhut
  2. clj-1287-3.diff
    14/Feb/14 12:11 PM
    3 kB
    Andy Fingerhut

Activity

Hide
Alex Miller added a comment -

We do not wish to make this change.

Show
Alex Miller added a comment - We do not wish to make this change.
Hide
Andy Fingerhut added a comment -

Patch clj-1287-3.diff is identical to the earlier clj-1287-2.diff described in an earlier comment, except it updates some diff context lines so that it applies cleanly to the latest Clojure master as of today.

Show
Andy Fingerhut added a comment - Patch clj-1287-3.diff is identical to the earlier clj-1287-2.diff described in an earlier comment, except it updates some diff context lines so that it applies cleanly to the latest Clojure master as of today.
Hide
Andy Fingerhut added a comment -

I had not thought about records when making the first patch. It does cause select-keys to fail if given a record, whereas without the patch select-keys works fine on records. clj-1287-2.diff is similar to the earlier patch, but if its argument is a record, it still returns a correct value based upon building up an unsorted map starting from {}. If its argument is not a record, it assumes empty will work on it and uses (empty map) to start with.

Show
Andy Fingerhut added a comment - I had not thought about records when making the first patch. It does cause select-keys to fail if given a record, whereas without the patch select-keys works fine on records. clj-1287-2.diff is similar to the earlier patch, but if its argument is a record, it still returns a correct value based upon building up an unsorted map starting from {}. If its argument is not a record, it assumes empty will work on it and uses (empty map) to start with.
Hide
Alex Miller added a comment -

What happens with records? Does this introduce a new failure case there (due to lack of empty support in records)?

Show
Alex Miller added a comment - What happens with records? Does this introduce a new failure case there (due to lack of empty support in records)?
Hide
Andy Fingerhut added a comment -

clj-1287-1.diff changes the starting set {} to (empty map) in select-keys, and adds tests for the new behavior. The only reason select-keys is moved later in core.clj is to move it after the definition of function 'empty'. Before this patch 'select-keys' is defined before 'declare', even, so we cannot simply put a '(declare empty)' before select-keys.

Show
Andy Fingerhut added a comment - clj-1287-1.diff changes the starting set {} to (empty map) in select-keys, and adds tests for the new behavior. The only reason select-keys is moved later in core.clj is to move it after the definition of function 'empty'. Before this patch 'select-keys' is defined before 'declare', even, so we cannot simply put a '(declare empty)' before select-keys.

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: