Skip to content

Instantly share code, notes, and snippets.

@m2ym
m2ym / varnumbers.pl
Last active August 29, 2015 14:04
varnumbers/2 in B-Prolog
% unnumber_vars/2 is not working correctly in B-Prolog 8.1. Here is
% an alternative implementation of unnumber_vars/2 named varnumbers/2,
% which is compatible with SWI-Prolog's varnumbers library.
max_varnumber('$VAR'(N), Max) =>
Max = N.
max_varnumber(Term, Max) =>
Term =.. [_ | Args0],
Args @= [Arg : Arg0 in Args0, [Arg], max_varnumber(Arg0, Arg)],
Max is max([-1 | Args]).
@m2ym
m2ym / PGOCaml_async.ml
Created August 15, 2014 15:39
PGOCaml with Async backend
open Core.Std
open Async.Std
module type PGOCaml_async_thread =
PGOCaml_generic.THREAD
with type 'a t = 'a Deferred.t
module PGOCaml_async_thread : PGOCaml_async_thread = struct
type 'a t = 'a Deferred.t
@m2ym
m2ym / async_pool.ml
Last active August 29, 2015 14:05
Simple resource pooling with Async
open Core.Std
open Async.Std
type 'a t = {
capacity : int;
generate : unit -> 'a Deferred.t;
validate : 'a -> bool Deferred.t;
mutable in_use : int;
free : 'a Queue.t;
waiters : 'a Ivar.t Queue.t;
@m2ym
m2ym / fun_of_modular_implicits.ml
Last active August 29, 2015 14:06
Fun of Modular Implicits
module type Show = sig
type t
val show : t -> string
end
let show (implicit S : Show) x = S.show x
module Show_instances = struct
implicit module Show_unit = struct
type t = unit
@m2ym
m2ym / ゆるふわJavaScriptプログラミング.ml
Created October 1, 2014 14:49
ゆるふわJavaScriptプログラミング in OCaml
open Easyjs
let () = begin%js
let foobar = {| jQuery("#foobar") |} in
let value = of_string "Hello, World!" in
let callback _ = {| ${foobar}.text(${value}) |} in
ignore {| window.setTimeout(${fn callback}, 1000) |}
end
@m2ym
m2ym / ppx_test_async.ml
Created December 16, 2014 05:59
ppx_test for Async
module Test = struct
include Ppx_test.Test
let eval = Async_unix.Thread_safe.block_on_async_exn
let test_unit loc name deferred =
test_unit loc name (fun () -> eval deferred)
let test loc name deferred =
test loc name (fun () -> eval deferred)
@m2ym
m2ym / slow_recursive_query.sql
Created January 5, 2015 02:06
Slow recursive query
WITH RECURSIVE t AS (
SELECT source, target FROM edge
UNION
SELECT e.source, t.target FROM edge e INNER JOIN t ON e.target = t.source
)
SELECT target FROM t WHERE source = 123
@m2ym
m2ym / guess.ml
Created January 24, 2015 21:23
libguess を ocaml-ctypes で使ってみる
$ sudo apt-get install libguess-dev
$ ocamlfind ocamlc -package ctypes.foreign -linkpkg -custom -cclib -lguess -o guess guess.ml
$ ./guess "こんにちは世界"
UTF-8
$ ./guess $(echo "こんにちは世界" | nkf -Ws)
SJIS
$ ./guess $(echo "こんにちは世界" | nkf -We)
EUC-JP
@m2ym
m2ym / extract.ml
Created January 25, 2015 10:52
Extract webpage metadata in OCaml
open Core.Std
let libguess_determine_encoding =
let open Ctypes in
Foreign.foreign "libguess_determine_encoding" (string @-> int @-> string @-> returning string)
type metadata = {
title : string option;
type_ : string option;
description : string option;
@m2ym
m2ym / show.ml
Created April 8, 2015 09:01
Try to implement Haskell's show in OCaml using ppx_overload
module type Show = sig
val show : 'a -> string
end
module Show = struct
external show : 'a -> string = "%OVERLOADED"
module Int = struct
let show = string_of_int
end