Lurker News
Preferably an elegant snippet rather than an entire (well engineered) codebase.

Comments (380) 116 threads

Earlier comments
canada_dry1 reply#
https://github.com/achael/eht-imaging

This python code was part of the imaging, analysis, and simulation software for radio interferometry that led to the historical first 'image' of a black hole.

seisvelas1 reply#
What about the code did you find so striking? Any part in particular you recommend checking out?
canada_dry#
Overall I refer to it as a good example of relatively complex - yet highly readable - python code.

It's utterly digestable due to its use of meaningful variable names, logical breakdown of functions and absence of 'clever' nontrivial one-liners.

_bxg13 replies#
I like this bit of JavaScript for uniquifying an array:

  array.filter((item, index, arr) => arr.indexOf(item) === index)
It works because indexOf returns the index of the first occurrence of the item, so you're asking whether this occurrence is the first occurrence.
throwaway_n1 reply#
This is O(n^2) though
_bxg11 reply#
All of JS's list processing functions are pretty inefficient, since at the bare minimum each one creates and copies to a new array (as opposed to, say, Rust iterators). You use them when elegance is more important than performance; N^2 is fine when N is eight.
kadoban#
That's always such a dangerous proposition though. Sure _you_ remember it's only fine when N is 8, but then the next guy comes along, or the input constraints change, or future you forgets because it _is_ fairly elegant looking.

I try not to leave grenades laying around too often, myself.

superfrank1 reply#
Personally I like this one, although it only works for an array of primitive types.

    const unique = [...new Set(arr)];
_bxg11 reply#
The other advantage of the filter one is it can be stuck in the middle of a bunch of other list operations - map(), sort(), slice(), other filters(). Also, you can use findIndex() instead of indexOf() to match any arbitrary predicate, instead of exact equivalence.
superfrank#
Oh, for sure. I know .filter() has it's place, so if you need anything beyond making the array unique, I'd definitely use your solution.

The one I posted just blew my mind a little the first time I saw it, so I love to share it. I guess I never really considered spreading a set.

SzamarCsacsi#
I like this neat little code to filter out falsy values:

  array.filter(Boolean)
enriquto#
the recursive "find" function on a disjoint set forest with path compression, by R.E.Tarjan. It brings tears to my eyes every time I read it.

    int dsf_find(int *t, int a)
    {
     if (a != t[a])
      t[a] = dsf_find(t, t[a]);
     return t[a];
    }

Load more comments