Skip to content

Instantly share code, notes, and snippets.

@khigia
khigia / bday-palindrome
Created April 13, 2011 16:34
Given a number, find notation bases for which number is palindrome
-- e.g. Find bases for which bday is palindre: findPal YYYYMMDD [2..1000]
-- or compile and run: echo YYYYMMDD | bdaypalbase
findPal bases n =
filter (Nothing /=) [isPal n b | b <- bases]
isPal n b =
if pal then Just (n, b, digits) else Nothing
where
digits = toDigits n b
@khigia
khigia / Dictionary lookup with default
Created October 16, 2009 02:11
C# dictionary lookup with default
public static V GetValueOrDefault<K, V>(this Dictionary<K, V> dic, K key, V defaultVal)
{
V ret;
return dic.TryGetValue (key, out ret) ? ret : defaultVal;
}
@khigia
khigia / NumericUpDown MouseWheel
Created October 16, 2009 01:57
C# NumericUpDown MouseWheel event
class MyNumericUpDown : NumericUpDown
{
protected override void OnMouseWheel (MouseEventArgs e)
{
// remove scrolllines scaling, taking care of case scrolllines is 0
int delta = e.Delta / Math.Max (Math.Abs (SystemInformation.MouseWheelScrollLines), 1);
base.OnMouseWheel (new MouseEventArgs (e.Button, e.Clicks, e.X, e.Y, delta));
}
}
@khigia
khigia / shell timeout
Created April 24, 2009 16:30
timeout on command run from bash
#!/bin/bash
if [ "$#" -lt 2 ]
then
cat<<EOF
Usage: $0 N <cmd>
Run <cmd> with a timeout of N seconds.
return result of <cmd>.
(<cmd> run in background process and is killed on timeout).
EOF
@khigia
khigia / gist:673
Created July 22, 2008 06:10
Ocaml ternary search tree type
type 'a tst_t =
| E
| N of 'a tst_t * 'a tst_t * 'a tst_t * char * 'a option