Need synchronization for an increment-only counter?

If you just used an int or long variable then you would need synchronization – incrementing involves read / increment-locally / write, which is far from an atomic operation. (Even if the variable is volatile to avoid memory model concerns of staleness, you’d still have three distinct operations, with the possibility of being pre-empted between … Read more

Update Counter collection in python with string, not letter

You can update it with a dictionary, since add another string is same as update the key with count +1: from collections import Counter c = Counter([‘black’,’blue’]) c.update({“red”: 1}) c # Counter({‘black’: 1, ‘blue’: 1, ‘red’: 1}) If the key already exists, the count will increase by one: c.update({“red”: 1}) c # Counter({‘black’: 1, ‘blue’: … Read more

Pandas groupby.size vs series.value_counts vs collections.Counter with multiple series

There’s actually a bit of hidden overhead in zip(df.A.values, df.B.values). The key here comes down to numpy arrays being stored in memory in a fundamentally different way than Python objects. A numpy array, such as np.arange(10), is essentially stored as a contiguous block of memory, and not as individual Python objects. Conversely, a Python list, … Read more

Summing list of counters in python

The sum function has the optional start argument which defaults to 0. Quoting the linked page: sum(iterable[, start]) Sums start and the items of an iterable from left to right and returns the total Set start to (empty) Counter object to avoid the TypeError: In [5]: sum(counter_list, Counter()) Out[5]: Counter({‘b’: 5, ‘c’: 4, ‘a’: 1})

simple hit counter for page views in rails

UPDATE The code in this answer was used as a basis for http://github.com/charlotte-ruby/impressionist Try it out It would probably take you less time to code this into your app then it would to pull the data from Analytics using their API. This data would most likely be more accurate and you would not have to … Read more

Get the index (counter) of an ‘ng-repeat’ item with AngularJS?

Angularjs documentation is full of examples, you just need to take some time and explore it. See this example here : ngRepeat example , it’s the same case. <ul> <li ng-repeat=”question in questions | filter: {questionTypesId: questionType, selected: true}”> <div> <span class=”name”> {{$index + 1}} {{ question.questionText }} </span> </div> <ul> <li ng-repeat=”answer in question.answers”> … Read more

C++ compile time counters, revisited

After further investigation, it turns out there exists a minor modification that can be performed to the next() function, which makes the code work properly on clang++ versions above 7.0.0, but makes it stop working for all other clang++ versions. Have a look at the following code, taken from my previous solution. template <int N> … Read more