Here is how you get the amount of free memory available in Erlang:
17> application:start(os_mon).
...
18> memsup:get_system_memory_data().
[{total_memory,2125283328},
{free_memory,188440576},
{system_total_memory,2125283328}]
Here is how you get the amount of free memory available in Erlang:
17> application:start(os_mon).
...
18> memsup:get_system_memory_data().
[{total_memory,2125283328},
{free_memory,188440576},
{system_total_memory,2125283328}]
Here is how you join a list of strings in elisp:
(mapconcat (lambda (x) x) load-path "\n")
It is equivalent to this Python code:
This is probably old stuff for most of you, but here is my Python implementation of the LZW algorithm.
# The initial LZW tables that maps codes to strings and vice-versa. code_to_str = [chr(i) for i in range(256)] str_to_code = dict((chr(i), i) for i in range(256)) def compress(seq): ''' Returns an LZW compressed list of the input character sequence. ''' output = [] table = dict(str_to_code) s = '' for ch in seq: it = s + ch if it in table: s = it else: output.append(table[s]) table[it] = len(table) s = ch output.append(table[s]) return output def decompress(seq): ''' Returns a decompressed list of the LZW compressed input. ''' table = code_to_str[:] prevcode = seq[0] output = [] output.append(table[prevcode]) for code in seq[1:]: try: entry = table[code] except IndexError: # The lzw special case when code is not yet defined. entry = table[prevcode] entry += entry[0] output.append(entry) table.append(table[prevcode] + entry[0]) prevcode = code return output
Used like this:
data = open('somefile.txt').read() data = compress(data) print decompress(data)
Explanation for how the algorithm works can be found in other places on the net.
This article is related to two patches to cairo, #12717 and #12722. Many times in C, you want values in a certain range, or some that are not above or below a certain value. The process which ensures that values are within a certain range is called clamping.
Let us say that you have function that sets the position of something.
Further assume that the position must be contained within obj:s clip rectangle. That could be accomplished using this code:
A mathematical way to write the clipping would be something like:
obj->x = x, obj->rect.x <= x <= obj->rect.x + obj->rect.width obj->y = y, obj->rect.y <= y <= obj->rect.y + obj->rect.height
Time to introduce the MIN and MAX functions and see how they can help us. In C, they can be implemented as macros.
It should not be any surprise that
and
is exactly equivalent. We can therefore rewrite the boundary checking code in set_pos to take advantage of these macros:
But wait, there is more! The code can be further rewritten if we introduce a CLAMP macro.
Now we can make the set_pos function even more readable:
Note how similar this code is to the imagined mathematical definition. It is also worth noting that the CLAMP macro doesn't work if the value of the upper bound is lower than the lower bound. That is, if obj->rect.width or obj->rect.height is negative, then the macro will give erroneous results.