6

Just found this javascript one-liner gem:
array.sort((a, b) => ([...new Set([...this.strip(a.name), ...a.tags])].filter(x => inKeywords.includes(x))).length == ([...new Set([...this.strip(b.name), ...b.tags])].filter(x => inKeywords.includes(x))).length ? 0 : (([...new Set([...this.strip(a.name), ...a.tags])].filter(x => inKeywords.includes(x))).length < ([...new Set([...this.strip(b.name), ...b.tags])].filter(x => inKeywords.includes(x))).length ? 1 : -1))

If that doesn't make you angry you might be in a coma

Comments
  • 1
    I feel like you are just sorting objects via their name properties ascii values. Nothing fancy
  • 3
    Someone needs to be introduced to reduce.
  • 1
    I don't even-
    What the fuck does that do
  • 1
    @PrivateGER
    Inside of a sort, creates a size(1) tuple of an array by spreading a Set consisting of known tags+name of the given argument into an array. Apparently there's a function on the inKeywords object named included that expects that structure, and will likely do an O(n^m) comparison to satisfy the filter predicate.

    It does this for both args and then compares the length of the filter expression size, which I can only assume to be size(1) or size(0) given the resulting filter expression is operating on a size(n) tuple at position 0 and will therefore only apply the predicate to a single argument (he's exploiting binary coincidence). This expression is for some ungodly reason executed twice in a cascading ternary that allows it to be less than or equal than the resulting length. My guess is because the author of the function isn't aware that <== exists.

    All of that is done to determine the order in which objects containing tags are sorted.

    Before anyone says it, autism has higher standards than this. This is just shit. If I had to guess, the algorithm started as an attempt to O(1) compare all the values in the set to another set, but then realized there were sets on both sides of the predicate and you needed to compare all of members of both sets. Rather than update the code, he decided to lavaflow the Set into an array, but didn't realize this needs a double spread. He then updates the argument to accept the tuple instead and destructures the arg in the includes predicate function. It's the essence of laziness.
  • 0
    What the actual fuck
  • 0
  • 1
    @SortOfTested reduce is same as fold in good languages right?
  • 2
    @yellow-dog
    Yes. It's your garden variety catamorphism.
  • 1
    @SortOfTested Catamorphisms, especially if they are of the garden variety are definitely more pettable than hylomorphisms. Don't let them near your trees though, they either completely reduce them with their nails, or get stuck in the branches. Coincidentally, if you are chasing after tails, you might want to adopt a garden variety paramorphism.
  • 0
    @SortOfTested I'm impressed. I'm not sure if I'm more impressed by the story of how this thing was created of yours or that you actually read the code.

    I saw the code and was thinking, I'm happy not to have to deal with such a thing and I've seen way worse in my past. Maybe even created worth code decades ago.

    But you impressed me, doesn't happen often. Made my day :)
  • 0
    Now let's agree that one line must not exceed 80 characters.
Add Comment