Skip to content

Instantly share code, notes, and snippets.

@keigoi
keigoi / monad_test.ml
Last active September 29, 2016 10:51
ppx_typeclass test
(* Monads with camlspotter's ppx_typeclass *)
(* see ppx_typeclass http://bitbucket.org/camlspotter/ppx_typeclass/ *)
(* Set-up higher-kinded polymorphism, see https://ocamllabs.github.io/higher/lightweight-higher-kinded-polymorphism.pdf *)
type ('p, 'f) app = .. (* Type-level defunctionalization *)
module Newtype1 (T: sig type 'a t end) () = struct
type 'a s = 'a T.t
type t
type (_, _) app += App : 'a s -> ('a, t) app
(* try at http://try.ocamlpro.com/js_of_ocaml/ *)
open Graphics
let () = open_graph " 900x600"
let rec k c =
clear_graph ();
moveto 450 300;
draw_string (string_of_int c);
ignore (Dom_html.window##setTimeout(Js.wrap_callback (fun _ -> k (c+1)), 0.1))
let () = k 0 ;;
function xpath(elm, expr) {
var it = document.evaluate(expr, elm, null, XPathResult.ANY_TYPE, null);
var typ = it.resultType;
switch(typ) {
case XPathResult.NUMBER_TYPE:
let rec fib n =
if n <= 1 then
1
else
let x = fib (n-1)
and y = fib (n-2)
in x+y
let main n = print_int (fib n)
@keigoi
keigoi / Makefile
Last active November 4, 2016 23:15
SMLYACC=smlyacc
SMLLEX=smllex
SMLSHARP=smlsharp
OBJS=calc.grm.o calc.lex.o calc.o
SMI=calc.grm.smi calc.lex.smi calc.smi
all: calc
@keigoi
keigoi / Hello.java
Last active November 21, 2016 23:42
// check this with facebook infer 0.9.4
public class Hello {
public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();
Object n = null;
Object[] arr = {o1, o1, o1, o1, o1};
for(int i=0; i<arr.length; i++) {
if(arr[i].equals(o2)) {
n.equals(n); // never reached but reported as an error
@keigoi
keigoi / MonadTest.cr
Last active December 10, 2016 00:35
order enforcement with/without a parameterized monad
# A monad in Crystal
class Id(T)
getter :value
def initialize(@value : T)
end
def bind(&x : T -> Id(U)) forall U
yield @value
end
@keigoi
keigoi / Lens.kt
Last active December 12, 2016 05:05
An attempt of simple implementation of lens on Java 8
class IxMonad<X,Y,T>(val x:T) {}
class Cons<HD,TL>(val hd : HD, val tl : TL) {}
class Nil();
fun <X,Y,T> ret(x: T) : IxMonad<X,Y,T> {
return IxMonad<X,Y,T>(x)
}
fun <T> run(m : IxMonad<Nil,Nil,T>) : T {
@keigoi
keigoi / TypeError.java
Last active December 11, 2016 03:06
Java 8 type inference
import java.util.function.Consumer;
import java.util.ArrayList;
public class TypeError {
public static <A> void let(A a, Consumer<A> f) {
f.accept(a);
}
(* try this with 4.02.0+modular-implicits *)
module type Show = sig
type t
val show : t -> string
end
implicit module Lam{X:Show} = struct
type t = [`Var of string | `App of X.t * X.t | `Abs of string * X.t]
let show : t -> string = function
|`Var(x) -> x