Skip to content

Instantly share code, notes, and snippets.

@ijt
ijt / SwapElts.hs
Created March 10, 2012 04:52
Swap two elements of a list in Haskell, with a QuickCheck test
module SwapElts where
-- If you have to use this function, arrays may be a better choice.
swapElts i j ls = [get k x | (k, x) <- zip [0..length ls - 1] ls]
where get k x | k == i = ls !! j
| k == j = ls !! i
| otherwise = x
-- This is a nice example of how to efficiently generate test cases.
-- A naive approach would have been to take separate arguments for
@ijt
ijt / stringsim.rs
Created October 3, 2019 00:36
Rust program to compute the trigram Jaccard similarity between two strings
//! The stringsim program prints out the trigram similarity of two strings
//! using what appears to be the same algorithm used by Postgres.
//! https://www.postgresql.org/docs/9.1/pgtrgm.html
use std::collections::HashSet;
use std::hash::Hash;
fn main() {
let args: Vec<String> = ::std::env::args().collect();
if args.len() != 1+2 {
@ijt
ijt / http_get.go
Last active August 23, 2021 12:37
Example of using http.Get in go (golang)
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
)
@ijt
ijt / io_quickcheck_example.hs
Created May 11, 2011 22:12
Simple example of monadic IO with QuickCheck in Haskell
#!/usr/bin/env runhaskell
-- Synopsis:
-- $ cabal install QuickCheck
-- $ runhaskell io_quickcheck_example.hs
--
-- Author: Issac Trotts <issac.trotts@gmail.com>
import Directory
import System.Environment
@ijt
ijt / arr_insert.py
Created February 17, 2012 17:52
A binary heap in Python
class Heap(object):
def __init__(self, size):
self.num = 0
self.size = size
self.data = [None] * size
def __repr__(self):
return '<Thing: %s>' % (self.data,)
def insert(arr, x):
@ijt
ijt / lit-element.html
Created May 22, 2020 18:10
Minimal HTML page using LitElement
<!doctype html>
<html>
<head></head>
<body>
<my-element></my-element>
<script type="module">
import {LitElement, html} from 'https://unpkg.com/@polymer/lit-element@latest/lit-element.js?module';
class MyElement extends LitElement {
render() {
<script type="module">
import {html, render} from 'https://unpkg.com/lit-html?module';
const myTemplate = (name) => html`<p>Hello ${name}</p>`;
render(myTemplate('World'), document.body);
</script>
@ijt
ijt / hijax.html
Created March 16, 2020 19:02
Intercept Ajax calls and make them do something else
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<div id="d">nothing here yet</div>
<script>
var send = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(body) {
@ijt
ijt / logging_example.hs
Created June 29, 2011 03:10
Example of logging in Haskell
#!/usr/bin/env runhaskell
-- This example uses the hslogger library.
-- For debugging it may be more convenient to use Debug.Trace instead since that
-- allows you to log debugging output from otherwise pure functions.
import System.IO (stderr, Handle)
import System.Log.Logger (rootLoggerName, setHandlers, updateGlobalLogger,
Priority(INFO), Priority(WARNING), infoM, debugM,
warningM, errorM, setLevel)
@ijt
ijt / FfiExample.hs
Created May 11, 2011 07:19
Example of calling C from Haskell using the FFI
{-# LANGUAGE ForeignFunctionInterface #-}
-- Simple example of calling C from Haskell.
--
-- $ ghci
-- > :load FfiExample.hs
-- > c_sin pi
-- 1.2246467991473532e-16
--
-- $ ghc --make FfiExample.hs