Skip to content

Instantly share code, notes, and snippets.

View rcook's full-sized avatar

Richard Cook rcook

View GitHub Profile
@rcook
rcook / LICENSE
Last active March 30, 2020 05:48
Exception rethrowing and finally clauses
The MIT License (MIT)
Copyright (c) 2019 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
@rcook
rcook / LICENSE
Last active March 30, 2020 05:48
My first MMA song
The MIT License (MIT)
Copyright (c) 2019 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
@rcook
rcook / LICENSE
Last active March 30, 2020 05:48
Unchecked casts in Java
The MIT License (MIT)
Copyright (c) 2019 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
@rcook
rcook / main.rs
Created March 3, 2019 17:34
Use of as_ref() in Rust
struct Foo {
title: String,
}
impl Foo {
fn new(title: &str) -> Foo {
Foo {
title: title.to_string(),
}
}
@rcook
rcook / Expr.hs
Last active September 1, 2024 18:13
Scalaz experimentation
#!/usr/bin/env stack
-- stack --resolver=lts-12.6 script
module Main (main) where
data Expr =
Val Int
| Add Expr Expr
| Sub Expr Expr
| Mul Expr Expr
@rcook
rcook / CFTest_Main.java
Last active March 30, 2020 05:48
Simple Checker Framework test program
package my.package;
final class Foo {
private final String value;
public Foo(final String value) {
this.value = value;
}
public String getValue() {
@rcook
rcook / ADTs.hs
Last active March 30, 2020 05:48
Scala experimentation
data Expr =
Val Int
| Add Expr Expr
| Mul Expr Expr
eval :: Expr -> Int
eval (Val x) = x
eval (Add x y) = eval x + eval y
eval (Mul x y) = eval x * eval y
@rcook
rcook / CheckHaddocksPrograms.hs
Last active February 12, 2019 17:38
Extract code blocks from Haddocks doc comments and compile them
#!/usr/bin/env stack
{-
stack --resolver=lts-13.3 script
--package Glob
--package process
--package temporary
-}
{-# OPTIONS_GHC -Wall -Werror #-}
@rcook
rcook / ParseDatabaseUrl.hs
Created February 9, 2019 23:10
Parse PostgreSQL database URL
parseDatabaseUrl :: String -> Maybe ConnectInfo
parseDatabaseUrl s = do
let (s1, s2) = break (== ':') s
unless (s1 == "postgres") (fail "Invalid scheme")
let (s3, s4) = break (\c -> c /= ':' && c /= '/') s2
unless (s3 == "://") (fail "Invalid separator")
let (user, s6) = break (== ':') s4
when (length user < 1) (fail "Invalid user")
let s7 = drop 1 s6
(password, s8) = break (== '@') s7
@rcook
rcook / AutoDeriving.hs
Last active March 30, 2020 05:48
Alternative Semigroup/Monoid instances via newtype wrappers
#!/usr/bin/env stack
-- stack --resolver=lts-12.6 script
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}