18 June 2018

Posted on 20 June 2018 by Alexander Ignatyev

abs()’s issue

abs() function ins C/C++ is not defined for minimum negative int as the standard says:

The abs, labs, and llabs functions compute the absolute value of an integer j. If the result cannot be represented, the behavior is undefined.

gcc returns the same value in this case.

Heap’s data structure

Heap’s implementations do not have some useful functions like change_key or remove_element in many languages. E.g. in C++ and Python.

Go’s implementaion actualy has both of the functions named as Fix() and Remove(): Package heap.

Combining hash values

XOR is not always the best choice to combine hash values. E.g. for a pair of int it will always returns 0 if values are the same.

Boost uses the following approach to avoid such collisions:

template <class T>
inline void hash_combine(std::size_t& seed, const T& v)
    std::hash<T> hasher;
    seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);

seed is a previous result of hasher or this algorithm.

Some good advices are available in Writing a hash function in Java: a practical guide to implementing hashCode() (suitable not only for Java, of course).

Facebook’s ReasonML

Facebook decided to create Frankenstein: a hybrid of JavaScript and ML. It looks a bit scary to me:

// A FizzBuzz implementation in ReasonML
let fizzbuzz = (i) =>
  switch ([i mod 3, i mod 5]) {
    | [0, 0] => "FizzBuzz"
    | [0, _] => "Fizz"
    | [_, 0] => "Buzz"
    | _    => string_of_int(i)

for (i in 1 to 100) {

This piece of code taken from the article: ReasonML - React as first intended.

ML family

There are quite a few languages in ML family: