Skip to content

Instantly share code, notes, and snippets.

View cmcbride's full-sized avatar

Cameron McBride cmcbride

  • Boston, MA (USA)
View GitHub Profile
(* for use with Array. Easy way to create an index, and then sort the index *)
let index a = Array.init ( Array.length a) (fun i -> i)
let sort_index a idx =
let cmpi i j = compare a.(i) a.(j) in
Array.fast_sort cmpi idx
(* compute GCD. playing with OCaml *)
let rec gcd n m =
if m = 0 then
n
else
if n > m
then
gcd (n-m) m
else
(* find min/max of an array, type generic *)
let minmax a =
let start = a.(0), a.(0) in
let mm t e = (min (fst t) e , max (snd t) e ) in
Array.fold_left mm start a
;;
@cmcbride
cmcbride / const.c
Created May 14, 2011 22:45
C const
// This is a slight modification of the original at:
// http://www.cap-lore.com/Languages/const.c
//
// Does "int const *" refer to a constant pointer, or a constant integer?
// The following shows what gcc thinks.
// gcc 4 rejects any of the lines below that are commented out.
typedef const int * cip;
typedef int const * icp;
typedef int * const ipc;
typedef int const * const icpc;
@cmcbride
cmcbride / is_int.ml
Created May 16, 2011 01:13
ocaml: check if float is an int
(* ways to check if float is an integer *)
let is_int v =
v = (snd (modf v))
;;
let better_is_int v =
let c = classify_float (fst (modf v)) in
c == FP_zero
;;
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
void
print_size( char const * const desc, const size_t size )
{
printf(" %10s: %zu bytes\n", desc, size);
#include <stdio.h>
void
func( )
{
static int x = 0; // x is initialized only once across three calls of func()
printf( " %d", x ); // outputs the value of x
x = x + 1;
}
@cmcbride
cmcbride / indent.pro
Created May 22, 2011 18:36
current indent config for C
/* some basic formatting guidelines for C code */
--indent-level 4
--tab-size 8
--no-tabs
--line-length 100
--braces-on-if-line
--braces-on-struct-decl-line
--cuddle-do-while
// this is a simple bookmarklet to make using NASA ADS simple at CfA
// (assuming one has a CfA PIN to log into the proxy)
//
// to make it a bookmarklet, run it through:
// John Gruber's Bookmarklet-Builder
// http://daringfireball.net/2007/03/javascript_bookmarklet_builder
// OR
// http://subsimple.com/bookmarklets/jsbuilder.htm
var public_url = "adsabs.harvard.edu";
// this is a basic bookmarklet to simply CfA proxy use,
// especially with the NASA ADS system.
// (assuming one has a CfA PIN to log into the proxy)
//
// to make it a bookmarklet, run it through:
// John Gruber's Bookmarklet-Builder
// http://daringfireball.net/2007/03/javascript_bookmarklet_builder
// OR
// http://subsimple.com/bookmarklets/jsbuilder.htm
//