Transcrypt icon indicating copy to clipboard operation
Transcrypt copied to clipboard

implement build-in id() function (example included)

Open faerot opened this issue 4 years ago • 0 comments

Python function id() is very useful and it is trivial to implement using WeakMap, all you need to is is adding to __builtin__.js this code:

var _id_registry = new WeakMap();
var _id_counter = 0;

export function id(obj) {
    var id = _id_registry.get(obj);
    if (id === undefined) {
        id = ++_id_counter;
        _id_registry.set(obj, id);
    }
    return id;
}

faerot avatar Apr 20 '20 07:04 faerot