Skip to content

Instantly share code, notes, and snippets.

@yawaramin
yawaramin / unicode_example.ml
Last active August 26, 2023 13:44
Example of encoding Unicode graphemes as string and taking its length
(* Re: https://twitter.com/llaisdy/status/1558536851560054786?s=20&t=us8J3LvoJTlwVwod5bOqeQ *)
(* Use this if using the REPL, otherwise use dune to build with the library dependency *)
#require "uutf";;
(* Converts an array of ints (Unicode graphemes) into a UTF-8 encoded string. *)
let utf8_to_string uchars =
let buf = Buffer.create (2 * Array.length uchars) in
Array.iter (fun uchar -> Uutf.Buffer.add_utf_8 buf (Uchar.of_int uchar)) uchars;
Buffer.contents buf
@yawaramin
yawaramin / openapi.k
Created August 11, 2023 02:57
OpenAPI spec schemas in KCL - https://kcl-lang.io/
"""
Bindings for OpenAPI 3.0.0 - https://swagger.io/specification/#schema-object
ℹ️ JSON Schema validation for OpenAPI 3.0.0 must conform to this version:
https://datatracker.ietf.org/doc/html/draft-wright-json-schema-validation-00
"""
schema Ref:
$ref: str
@yawaramin
yawaramin / comment
Created February 16, 2021 03:28
Get or set file comment (Linux)
#!/usr/bin/env sh
usage() {
cat <<EOF
USAGE
$(basename $0) FILENAME [COMMENT]
Get or set file comment.
ARGUMENTS
@yawaramin
yawaramin / sig
Last active June 12, 2022 05:03
Bourne Shell script to print out Merlin's inferred signature of an OCaml file (module)
#!/usr/bin/env sh
# Works with merlin version 2.5.4. Using protocol described at
# https://github.com/ocaml/merlin/blob/master/doc/dev/OLD-PROTOCOL.md#type-checking
usage ()
{
echo Usage: either of the following will work:
echo
echo ' sig module.ml'
@yawaramin
yawaramin / ecb_rates.sql
Last active June 17, 2021 01:28
European Central Bank historical rates import SQLite script
-- Run with:
-- $ sqlite3 ecb_rates.db
-- sqlite> .read ecb_rates.sql
drop table if exists eurofxref_hist;
drop table if exists ecb_rates;
create table ecb_rates (
date text not null,
curr text not null,
(* Good morning everyone, I'm currently learning ocaml for one of my CS class and needed to implement
an avl tree using ocaml. I thought that it would be interesting to go a step further and try
to verify the balance property of the avl tree using the type system. Here's the resulting code
annotated for people new to the ideas of type level programming :)
*)
(* the property we are going to try to verify is that at each node of our tree, the height difference between
the left and the right sub-trees is at most of 1. *)
@yawaramin
yawaramin / Formik.re
Last active May 1, 2020 21:10
ReasonML/BuckleScript bindings for Formik
[@bs.module "formik"]
[@react.component]
external make: (
~initialValues: Js.t('a),
~validate: (. Js.t('a)) => Js.Dict.t(string),
~onSubmit: (. Js.t('a), {. "setSubmitting": [@bs.meth] bool => unit }) => unit,
~children: (. {. "isSubmitting": bool }) => React.element,
) => React.element = "Formik";
module Form = {
@yawaramin
yawaramin / sql.fs
Last active November 6, 2019 19:34
Type-safe SQL query using phantom types to model query parts as state transitions
(*
An incomplete implementation of a type-safe SQL query in F#.
The idea is that a query is built up clause by clause, by representing
each additional clause being added on to the query as a state transition
between different types of queries. We capture these different types as
phantom types to make sure that only valid transitions (query clause
additions) as defined by us can be carried out.
The final result is a 'total query' that can be converted to a string,
// @flow
import type { $Request, $Response, Middleware, NextFunction, Router } from 'express'
type Path = express$Path | express$Path[]
export type TypedMiddleware<A> =
| ((req: $Request & A, res: $Response, next: NextFunction) => mixed)
| ((error: Error, req: Request & A, res: $Response, next: NextFunction) => mixed)
@yawaramin
yawaramin / ParseTags.rs
Last active July 30, 2019 00:08
Parse text tags (Rust)
/// Parsed fragment of a document, containing borrowed slices of text
/// from the original document itself.
#[derive(Debug, PartialEq)]
pub enum DocPart<'a> {
/// Placeholder markup containing variable or expression that can be
/// interpolated with values.
Tag(&'a str, Vec<&'a str>),
/// Plain text.
Run(&'a str),