Skip to main content
Not sure if it is a good idea to post WRI code
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82

The most relevant definition that the previous commands display is

Self : DeleteDuplicatesBy[expr_, f_] := 
 Module[{r$}, 
  r$ = Which[! 
     Quiet[TrueQ[! AtomQ[expr] || AssociationQ[expr] || 
        Head[expr] === SparseArray]], 
    Message[DeleteDuplicatesBy::normal, 1, "HoldForm"[Self]]; 
    Macros`$FailRHS, True, 
    Macros`HoldSequence[
     Which[ListQ[expr], Values[GroupBy[expr, f, First]], 
      AssociationQ[expr], 
      Association[Values[GroupBy[Normal[expr], f@*Last, First]]], 
      Head[expr] === SparseArray, 
      DeleteDuplicates[expr, f[#1] === f[#2] &], True, 
      expr[[Values[
         GroupBy[Table[{f[expr[[i]]], i}, {i, Length[expr]}], First, 
          First]][[All, 2]]]]]]]; 
  Macros`ReleaseHoldSequence[r$] /; ! 
    MatchQ[r$, 
     Macros`$FailRHS | Macros`HoldSequence[Macros`$FailRHS]]]

If we predict where we will end up for a list, thisthe definition basically says

The most relevant definition that the previous commands display is

Self : DeleteDuplicatesBy[expr_, f_] := 
 Module[{r$}, 
  r$ = Which[! 
     Quiet[TrueQ[! AtomQ[expr] || AssociationQ[expr] || 
        Head[expr] === SparseArray]], 
    Message[DeleteDuplicatesBy::normal, 1, "HoldForm"[Self]]; 
    Macros`$FailRHS, True, 
    Macros`HoldSequence[
     Which[ListQ[expr], Values[GroupBy[expr, f, First]], 
      AssociationQ[expr], 
      Association[Values[GroupBy[Normal[expr], f@*Last, First]]], 
      Head[expr] === SparseArray, 
      DeleteDuplicates[expr, f[#1] === f[#2] &], True, 
      expr[[Values[
         GroupBy[Table[{f[expr[[i]]], i}, {i, Length[expr]}], First, 
          First]][[All, 2]]]]]]]; 
  Macros`ReleaseHoldSequence[r$] /; ! 
    MatchQ[r$, 
     Macros`$FailRHS | Macros`HoldSequence[Macros`$FailRHS]]]

If we predict where we will end up for a list, this basically says

If we predict where we will end up for a list, the definition basically says

Update for 10.3, removed IndexBy
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82

The behavior described here is the same from 10.0.0 up to at least 10.1.0, with the minor exception of IndexBy3.

Let's see what the definition of DeleteDuplicatesBy is. This can be seen by evaluating the following code

DeleteDuplicatesBy (*to autoload some stuff*)
ClearAttributes[DeleteDuplicatesBy, ReadProtected]
oldCP = $ContextPath; (*all context stuff is just to make things more readable*)
$ContextPath = 
 Join[$ContextPath, {"Macros`Evaluation`PackagePrivate`", 
   "GeneralUtilities`System`PackagePrivate`"}]
DeleteDuplicatesBy // Information
$ContextPath = oldCP

Or inFrom v10.1.0 onwards, this may be done conveniently by using

In version 10.0.0, the undocumented System` function IndexBy also does not work with held expressions. It also doesn't give a message when the first argument is bad but still applies some rules.

In version 10.1.0 IndexBy does not seem to do anything anymore. Curiously BenchmarkPlot still depends on that function. This causes BenchmarkPlot to fail for some inputs, including for the inputs given in Mr.Wizard's answer.

The behavior described here is the same from 10.0.0 up to at least 10.1.0, with the minor exception of IndexBy.

Let's see what the definition of DeleteDuplicatesBy is. This can be seen by evaluating the following code

DeleteDuplicatesBy (*to autoload some stuff*)
ClearAttributes[DeleteDuplicatesBy, ReadProtected]
oldCP = $ContextPath; (*all context stuff is just to make things more readable*)
$ContextPath = 
 Join[$ContextPath, {"Macros`Evaluation`PackagePrivate`", 
   "GeneralUtilities`System`PackagePrivate`"}]
DeleteDuplicatesBy // Information
$ContextPath = oldCP

Or in v10.1.0 this may be done conveniently by using

In version 10.0.0, the undocumented System` function IndexBy also does not work with held expressions. It also doesn't give a message when the first argument is bad but still applies some rules.

In version 10.1.0 IndexBy does not seem to do anything anymore. Curiously BenchmarkPlot still depends on that function. This causes BenchmarkPlot to fail for some inputs, including for the inputs given in Mr.Wizard's answer.

The behavior described here is the same from 10.0.0 up to at least 10.3.

Let's see what the definition of DeleteDuplicatesBy is. From v10.1.0 onwards, this may be done conveniently by using

IndexBy
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82

The behavior described here is the same from 10.0.0 up to at least 10.1.0, with the minor exception of IndexBy.

TheIn version 10.0.0, the undocumented System` function IndexBy also does not work with held expressions. It also doesn't give a message when the first argument is bad but still applies some rules.

In version 10.1.0 IndexBy does not seem to do anything anymore. Curiously BenchmarkPlot still depends on that function. This causes BenchmarkPlot to fail for some inputs, including for the inputs given in Mr.Wizard's answer.

The behavior described here is the same from 10.0.0 up to at least 10.1.0.

The undocumented System` function IndexBy also does not work with held expressions. It also doesn't give a message when the first argument is bad but still applies some rules.

The behavior described here is the same from 10.0.0 up to at least 10.1.0, with the minor exception of IndexBy.

In version 10.0.0, the undocumented System` function IndexBy also does not work with held expressions. It also doesn't give a message when the first argument is bad but still applies some rules.

In version 10.1.0 IndexBy does not seem to do anything anymore. Curiously BenchmarkPlot still depends on that function. This causes BenchmarkPlot to fail for some inputs, including for the inputs given in Mr.Wizard's answer.

PrintDefinitions
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading
Made q print in correspondence with previous output. Added behavior scope description.
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading
Misinterpreted CountsBy before, now fixed
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading
Mainly summary
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading
added 5 characters in body
Source Link
Rojo
  • 42.8k
  • 7
  • 98
  • 190
Loading
Changed conclusion about converting between association and list
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading
Minor
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading
KeySortBy
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading
KeySortBy
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading
KeySortBy
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading
Not so harsh
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading
SortBy
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading
Removed bad example, showed incorrect results
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading
Removed bad example, showed incorrect results
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading
Source Link
Jacob Akkerboom
  • 12.2k
  • 46
  • 82
Loading