Skip to content

Instantly share code, notes, and snippets.

View rand00's full-sized avatar

rand00

View GitHub Profile
@rand00
rand00 / pattern_matching.ml
Created October 30, 2019 23:13
Why Is Array/Object Destructuring So Useful And How To Use It (a JS example of destructuring shown in OCaml)
(*took examples from https://youtu.be/NIq3qLaHCIs*)
let printf = Printf.printf
let example1 =
let alphabet = ['a'; 'b'; 'c'; 'd'; 'e'; 'f'] in
match alphabet with
| a :: b :: _ ->
printf "a = %c and b = %c\n" a b
@rand00
rand00 / euler15.ml
Created November 20, 2018 07:56
project euler no. 15
let combinations combine l l' =
CCList.cartesian_product [l; l']
|> CCList.map (fun [p; p'] -> combine p p')
let paths xmax ymax =
let rec aux x y prev_paths =
if x > xmax || y > ymax then []
else if x = xmax && y = ymax then prev_paths
else
let next_paths = List.flatten [
@rand00
rand00 / lib_dill_example_lwt.ml
Created October 26, 2018 11:43
Structured concurrency semantics?
(*Example from http://libdill.org/ rewritten in Lwt*)
open Lwt
let rec worker s =
Lwt_io.printl s >>= fun () ->
Lwt_unix.sleep @@ Random.float 0.5 >>= fun () ->
worker s
let main =
let _ = worker "Hello!" in
@rand00
rand00 / pv.ml
Last active October 22, 2016 19:24
Hiding private variants v1
module type S = sig
type priv
type exposed = [ `A | `Priv of priv ]
val mkB : unit -> priv
val f : priv -> priv
val g : priv -> string
@rand00
rand00 / accgen.ml
Created September 5, 2015 21:44
Solution to Paul Grahams accumulator generator problem, for OCaml.. http://www.paulgraham.com/accgen.html
module Num = struct
type t = [ `F of float | `I of int ]
let to_float = function
`F f -> f
| `I i -> float i
let add n n' = match n, n' with
`F f, `F f' -> `F (f +. f')
@rand00
rand00 / gist:e8a382e3aa1b64a53727
Created September 30, 2014 13:03
Elisp function for copying Emacs-selection to the X primary selection (clipboard)
(defun yank-to-x-clipboard ()
(interactive)
(if (region-active-p)
(progn
(shell-command-on-region
(region-beginning) (region-end)
"xsel -pi")
(message "Yanked region to clipboard!")
(deactivate-mark))
(message "You havn't marked any text.")))