Posts in category Programming

Yet another HN rant..

This is exactly how you get professionally marketed crap like Mongo, Node, Hadoop or Java itself - you name it.

It is a fast-food way of building software - cheapest processed shit wrapped in a good SEO and naive user experience (quick and rewarding installation and effortless bootstrapping of a meaningless hello-world - I am so clever meme).

What matters in software in the long run, be it an application or even a programming language, is appropriate design decisions and strong emphasis on design around protocols (by duck-typed interfaces), using declarative (instead of imperative) mostly-functioal paradigm, bottom-up process with focus on modularity and code reuse, while sticking to the standard idioms and principle of less astonishment. This is what is still being taught at MIT, and this is how the internet works as a whole.

99% of Node or PHP code is a toxic amateurish crap, precisely due to ignorant over-confidence of the coders and lacking of proper CS education about right principles and programming paradigms, importance of louse coupling and proper encapsulation, which leads to modular, share-nothing, mostly functional designs with emphasis on immutability and persistence (that's why Clojure is so good - it has been, like Erlang, well-researched with tons of good stuff borrowed form Common Lisp).

Erlang or Haskell or Scala or Swift or Golang are on another side of the spectrum, which could be characterized by discipline of sticking to the right principles and rigorous attention to details (of which Haskell might be a bit too strict, but Erlang or Scala just right).

BTW, these observations about the impact of a proper design, based on right principles, which have been made almost 20 years ago still holds - http://norvig.com/java-lisp.html. Today we could state the same for Python3.5+, which, finally, evolved to be so carefully refined as old school Lisps, but emphasizing that Python3 is a prototyping language, while CL is both prototyping and implementation language.

No sane person should even touch PHP or Javascript or any other crap with implicit coercions, error supressions, etc, designed by amateurs (hello, MySQL, Mongo, Hadoop!) like one avoid chap processed junk food or drugs.

Do not copy-paste code

Do not copy-paste shit without understanding.

This comes from straight from The Rossetta Code

def binary_search(l, value, low = 0, high = -1):
    if not l: return -1
    if(high == -1): high = len(l)-1
    if low >= high:
        if l[low] == value: return low
        else: return -1
    mid = (low+high)//2
    if l[mid] > value: return binary_search(l, value, low, mid-1)
    elif l[mid] < value: return binary_search(l, value, mid+1, high)
    else: return mid

apart from retarded formatting it seems OK

but if we test the code

binary_search([2, 3], 1)

we will get

RecursionError: maximum recursion depth exceeded in comparison

surprise!

all we did is searched for an element which is less than the smallest one in the list.

The bug is due to arrogance of some C or C++ coder

>>> len([]) - 1
-1

which means that -1 is a legit value for high

sane people are using None

One more thing. If you do this

binary_search([3, 3], 3)]

the code will return 1 instead of 0

There is a more correct version:

def binary_search(xs, x, lo=0, hi=None):
    if hi is None:
        hi = len(xs) - 1
    if lo > hi:
        return None
    if xs[lo] == x:
        return lo
    mi = lo + (hi - lo) // 2
    if xs[mi] > x:
        return binary_search(xs, x, lo, mi-1)
    elif xs[mi] < x:
        return binary_search(xs, x, mi+1, hi)
    else:
        return mi