Skip to content

Instantly share code, notes, and snippets.

@xentec
Last active February 9, 2016 14:47
Show Gist options
  • Save xentec/f8f3df9d5b638a6b0b7f to your computer and use it in GitHub Desktop.
Save xentec/f8f3df9d5b638a6b0b7f to your computer and use it in GitHub Desktop.
/* Return the number of running processes of the system */
ulong getProcNum()
{
version(linux)
{
import std.file, std.algorithm, std.ascii;
return "/proc".dirEntries(SpanMode.shallow).count!(a => a.name.baseName.all!isDigit);
}
}
/* Determine the closest element in _arr_ to _n_ */
Number closest
(Number)
(in Number[] arr, Number n)
@safe pure @nogc nothrow
{
import std.math: abs;
size_t h = arr.length / 2;
return h ? n.closest(abs(n-arr[h-1]) < abs(n-arr[h]) ? arr[0..h] : arr[h..$]) : arr[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment