Skip to content

Instantly share code, notes, and snippets.

View hkoba's full-sized avatar

Kobayasi, Hiroaki hkoba

View GitHub Profile
@hkoba
hkoba / why_labels_omitted.ml
Created February 12, 2014 23:24
Why this ocaml code emits warning 6 for String.sub? (labels were omitted in the application of this function.)
(* -*- coding: utf-8 -*- *)
open Core.Std
module Strrange = struct
let (//) opt def = match opt with Some x -> x | None -> def
let min x y = if x < y then x else y
type t = {base: string; mutable start: int; mutable finish: int}
let create ?(start=0) ?finish ?(empty=false) str =
@hkoba
hkoba / strncmp.ml
Last active August 29, 2015 13:56
Compare sub regions of two strings in OCaml. (But there will be something in core, I guess)
(* -*- coding: utf-8 -*- *)
open Core.Std
module Util = struct
let min x y = if x < y then x else y
let apply_if opt fn v =
match opt with
| None -> v
| Some bound -> fn bound v
@hkoba
hkoba / bool_or_chain_bench1.ml
Created February 19, 2014 07:17
Benchmark for expression cascading. Boolean conjunction sequence vs Optional match.
(* -*- coding: utf-8 -*- *)
open Core.Std
open Core_bench.Std
let (|>) x f = f x
let run_bench tests =
Bench.bench
~ascii_table:true
~display:Textutils.Ascii_table.Display.column_titles
@hkoba
hkoba / sexparse2.ml
Created February 19, 2014 13:46
Short S-expression parser in OCaml, take2. (This time with string support). Basically for my self training.
(* -*- coding: utf-8 -*- *)
open Core.Std
module Util = struct
(** Perl5's defined-or operator *)
let (//) opt def = match opt with Some x -> x | None -> def
let min x y = if x < y then x else y
let apply_if opt fn v =
@hkoba
hkoba / camldebug-with-args.el
Created March 16, 2014 04:21
camldebug should accept arguments for debugging target.
(defun camldebug (command-line)
"Run camldebug on program FILE in buffer *camldebug-FILE*.
The directory containing FILE becomes the initial working directory
and source-file directory for camldebug. If you wish to change this, use
the camldebug commands `cd DIR' and `directory'."
(interactive
(list (read-from-minibuffer "Run ocamldebug on command-line: ")))
(let* ((words (split-string-and-unquote command-line))
(path (expand-file-name (car words)))
@hkoba
hkoba / sdltk-textinput-minimum.patch
Created March 21, 2014 09:02
Minimum patch to enable kanji input on AndroWish, Tcl/tk on Android.
diff --git a/SdlTkInt.c b/SdlTkInt.c
index 2ccb1da..f4e04bf 100644
--- a/SdlTkInt.c
+++ b/SdlTkInt.c
@@ -653,6 +653,12 @@ skipTranslation:
case SDL_KEYDOWN:
case SDL_KEYUP:
state = SDL_GetMouseState(&x, &y);
+ {
+ /* To make sure all key-related events has empty charValuePtr. */
@hkoba
hkoba / tksqlite-cte-with.patch
Created April 7, 2014 09:47
tksqlite patch: to show results from sql query starting "with" - a.k.a. "CTE, common table expression". See http://www.sqlite.org/lang_with.html
--- a/tksqlite.tcl
+++ b/tksqlite.tcl
@@ -8703,7 +8703,8 @@ proc Sqlite::publish {query tableName rawdataName} {
set _temp [string map {, { } \" {}} $query]
set _temp [SQLParser::removeComment $_temp]
set sqlcmd [string tolower [lindex $_temp 0]]
- if {$sqlcmd ne "select" &&
+ if {$sqlcmd ne "select" &&
+ $sqlcmd ne "with" &&
$sqlcmd ne "pragma" &&
@hkoba
hkoba / selfref.ml
Last active August 29, 2015 13:58
OCaml tip - To test equality of self referencing data, we can't use (=) since it causes Out_of_memory. In this specific case, we should use (==).
type 'a ring_t = {
value: 'a;
mutable next: 'a ring_t;
mutable prev: 'a ring_t;
}
let init value =
let rec x = {value; next = x; prev = x} in
x
@hkoba
hkoba / pslist-still-using-deleted-libssl.zsh
Last active August 29, 2015 13:58
List all processes still using deleted libssl, in Zsh. (To fight against heartbleed and future vulnerability...)
#!/bin/zsh
typeset -A dup; dup=()
grep libssl /proc/*/maps|grep deleted|while read fn rest; do
pid=${${fn#/proc/}//\/*/}
((dup[$pid]++)) && continue
echo -n $pid " "; readlink /proc/$pid/exe
done
@hkoba
hkoba / output.txt
Created April 9, 2014 14:46
Just to prove OCaml's gc can collect self referencing record.
## Test array size=1048576
(filled - start).live_words = 6291460
(end - filled).live_words = -6291452
(end - start).live_words = 8
(filled - start).live_blocks = 2097153
(end - filled).live_blocks = -2097151
(end - start).live_blocks = 2