Skip to content

Instantly share code, notes, and snippets.

@yawaramin
yawaramin / sample-db.sql
Created October 3, 2013 00:51
Sample Access ANSI-92 SQL database creation script
alter table tblAddresses
drop constraint Addresses_NeedAtLeastOnePart;
alter table tblPeople
drop constraint People_AddressID;
alter table tblPeople
drop constraint People_NeedAtLeastOneName;
alter table tblPeople
@yawaramin
yawaramin / ParseTags.hs
Last active September 18, 2016 14:59
Parse text tags
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified Data.Map as M
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
{- A document part is like a piece of syntax. -}
data DocPart =
Tag T.Text [T.Text] | Run T.Text
@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),
/*
Low-priority implicits
Thanks to
http://www.slideshare.net/NLJUG/scala-design-patterns-age-mooij for
pointing me in the right direction
*/
trait Ord[A] {
/** Returns a1 <= a2 */
def lteq(a1: A, a2: A): Boolean
@yawaramin
yawaramin / Main.scala
Last active April 22, 2016 18:11
Asynchronous and streaming file word counter
import java.nio.file.{ Files, Path, Paths }
import resource._
import scala.collection.JavaConversions.iterableAsScalaIterable
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.{ Await, Future }
object Main {
/** An asynchronous function (inspired by Finagle). */
type Async[A, B] = A => Future[B]
open Cmdliner
let mycli _ _ = print_endline "Success!"
let interface =
let docv = "INTERFACE" in
let doc = "parse AST as an interface (true or false)" in
Arg.(required & pos 0 (some bool) None & info ~docv ~doc [])
@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,
@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 / doctool.md
Last active October 3, 2017 03:43
A language-agnostic documentation tool

I propose here a new convention and design for a tool to auto-generate API documentation for source code in any language, with no dependencies or intricate build commands. Here are my assumptions:

  • Source code in plain text format
  • Language supports comments, either single- or multi-line (multi-line would be easier to use for documentation)
  • A convention to interpret comments starting with a certain character as documentation comments, e.g. for OCaml we would use (** to mark the start of documentation comments.

Given the above, we can create a tool that has the following UI:

doctool [--output-dir DIR] --start STARTSTR --end ENDSTR --lang LANGUAGE FILE...
@yawaramin
yawaramin / scala-local-imports.md
Last active September 24, 2018 18:58
Local imports in Scala

Local imports in Scala

https://github.com/ghik/opinionated-scala/wiki/Packages-and-imports#local-imports

Local imports are very nice tool to minimize the impact of your imports and protect yourself from accidentally polluting too wide namespaces with imports that you need only in small, local context. That's especially important when using wildcard imports (see later).

Cay Horstmann, Scala for the Impatient, §7.8 (Imports can be anywhere)

In Scala, an import statement can be anywhere, not just at the top of a file.