Ticket #197 (new defect)

Opened 1 year ago

Last modified 1 year ago

Builtins: Should Vector's intrinsic::sort have a default "sane" comparator function?

Reported by: lth Assigned to: lth
Type: defect Priority: major
Milestone: Component: Spec
Version: 4 Keywords:
Cc: brendan,jeffdyer,graydon

Description (last modified by lth) (diff)

spec/library/Vector.html: Right now the comparefn argument to Vector's sort method is required, because its optionality on Array (with the necessity of having a cast-to-string semantics for predictability) has some problems. Since vectors are monotyped it is possible to provide a default that is saner, along these lines:

  /* T is the base type of the vector */
  function comparator(a, b) {
    if (T is Numbers) {
      if (a < b) return -1;
      if (b < a) return 1;
      return 0;
    }
    if (T is Booleans) {
      if (!a && b) return -1;
      if (a && !b) return 1;
      return 0;
    }
    if (T is Strings) {
      if (a < b) return -1;
      if (b < a) return 1;
      return 0;
    }
    /* default behavior -- from Array.sort */
    return comparator(string(a), string(b))
  }

Obviously this is optimizable as part of the Vector instantiation procedure, type dispatch does not have to happen in here. But you get the idea.

The questions are:

  • Do we want a "sane" default?
  • Is the above what we want?
  • Are there other cases we want to worry about (eg Dates)?
  • Do we want cast-to-string as the fallback, or some error behavior?

Attachments

Change History

Changed 1 year ago by lth

  • description changed from spec/library/Vector.html: Right now the comparefn argument to Vector's sort method is required, because its optionality on Array (with the necessity of having a cast-to-string semantics for predictability) has some problems. Since vectors are monotyped it is possible to provide a default that is saner, along these lines: /* T is the base type of the vector */ function comparator(a, b) { if (T is Numbers) { if (a < b) return -1; if (b < a) return 1; return 0; } if (T is Booleans) { if (!a && b) return -1; if (a && !b) return 1; return 0; } if (T is Strings) { if (a < b) return -1; if (b < a) return 1; return 0; } /* default behavior -- from Array.sort */ return comparator(string(a), string(b)) } Obviously this is optimizable as part of the Vector instantiation procedure, type dispatch does not have to happen in here. But you get the idea. The questions are: * Do we want a "sane" default? * Is the above what we want? * Are there other cases we want to worry about (eg Dates)? * Do we want cast-to-string as the fallback, or some error behavior? to spec/library/Vector.html: Right now the comparefn argument to Vector's sort method is required, because its optionality on Array (with the necessity of having a cast-to-string semantics for predictability) has some problems. Since vectors are monotyped it is possible to provide a default that is saner, along these lines: {{{ /* T is the base type of the vector */ function comparator(a, b) { if (T is Numbers) { if (a < b) return -1; if (b < a) return 1; return 0; } if (T is Booleans) { if (!a && b) return -1; if (a && !b) return 1; return 0; } if (T is Strings) { if (a < b) return -1; if (b < a) return 1; return 0; } /* default behavior -- from Array.sort */ return comparator(string(a), string(b)) } }}} Obviously this is optimizable as part of the Vector instantiation procedure, type dispatch does not have to happen in here. But you get the idea. The questions are: * Do we want a "sane" default? * Is the above what we want? * Are there other cases we want to worry about (eg Dates)? * Do we want cast-to-string as the fallback, or some error behavior?
Note: See TracTickets for help on using tickets.