Skip to content

Instantly share code, notes, and snippets.

@harpocrates
harpocrates / vvm_java.sh
Created August 7, 2019 20:42
Wrapper script for calling `java` with JMX access on port 10000
#!/usr/bin/env bash
java \
-Dcom.sun.management.jmxremote.port=10000 \
-Dcom.sun.management.jmxremote.rmi.port=10000 \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-Djava.rmi.server.hostname=localhost \
$@
@harpocrates
harpocrates / Json.scala
Created June 27, 2019 14:32
Quick example demonstrating custom type-safe serialization with typeclasses and generic programming
import scala.reflect.ClassTag
import shapeless._
import shapeless.labelled._
/*
* Simple Json type
*/
sealed trait Json
object Json {

Given a collection of elements X, a sharding function should map every element of X into a shard.

trait Sharder[X] {
  /// The final output must always be in the range [0, totalShards-1], hopefully evenly distributed in that range.
  def apply(totalShards: Int): X => Int
}

Note that we have to fix the number of shards we want before getting our sharding function. One thing we may want is to change the number of shards. When we do this, it is expected that a bunch of x: X will now map to new shards. Depending on how we intend to change the number of shards, we can be clever about defining Sharder[X] so as to minimize the number of x: X which move to a new shard.

@harpocrates
harpocrates / CTuples.hs
Created December 18, 2018 00:41
Constraint tuples aren't (always?) normalized
{-# LANGUAGE RankNTypes, ScopedTypeVariables, TypeApplications, ConstraintKinds #-}
module Main where
import Data.Constraint
import Unsafe.Coerce
newtype MyInt = MyInt Int
instance Eq MyInt where { _ == _ = True }
flip' :: forall a b. Dict (Eq a, Eq b) -> a -> a -> Bool
@harpocrates
harpocrates / Show1Stuff.hs
Created September 11, 2018 20:38
Newtype for deriving `Show1`
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DeriveFunctor #-}
@harpocrates
harpocrates / feynmann_integral_trick.tex
Created August 1, 2018 20:53
Feynmann's integral trick
\documentclass{article}
\usepackage{amsfonts}
\usepackage{amsmath}
\title{Feynmann's integral trick}
\date{August 2018}
\begin{document}
\maketitle
The trick is to define a new function $I \colon \mathbb{R} \to \mathbb{R}$ such that $I(a_0)$ is the
integral we are trying to evaluate and $I(a_1)$ reduces to something more easily evaluated. The next
@harpocrates
harpocrates / HaskellFn.rs
Last active May 15, 2018 15:39
An idea...
#![feature(fn_traits)]
#![feature(unboxed_closures)]
/// Wrapper around Haskell function. Note this _cannot_ be copied/cloned
#[repr(C)]
pub struct HaskellFn1<A,B>(extern "C" fn(A) -> B);
/// Closure behaviour
impl<A,B> FnOnce<A> for HaskellFn1<A,B> {
type Output = B;
@harpocrates
harpocrates / TruthTable.hs
Created April 4, 2018 23:06
Generate truth table for any function with boolean input(s) and output.
{-# LANGUAGE FlexibleInstances #-}
-- Get the truth-table of a function which operates over any number of 'Bool'
-- inputs and has a `Bool` output `Bool`.
class TruthTable b where
truthtable :: b -> [([Bool], Bool)]
instance TruthTable Bool where
truthtable b = [([],b)]
{-# OPTIONS_GHC -w #-}
{-# OPTIONS -XMagicHash -XBangPatterns -XTypeSynonymInstances -XFlexibleInstances -cpp #-}
{-# OPTIONS_GHC -XPartialTypeSignatures #-}
module Main (
parseTerm, parseSentence, parseEditList, main
) where
import Data.Foldable (toList)
import Data.List.NonEmpty (NonEmpty(..), (<|))
import qualified Data.List.NonEmpty as NEL
import qualified Data.Array as Happy_Data_Array
@harpocrates
harpocrates / Main.hs
Created January 28, 2018 19:28
Multi-stage TH
-- Notice how flipping the order of anything makes this not
-- compile - this demonstrates that GHC is doing the following
-- in order:
--
-- 1. Processing the `foo` declaration
-- 2. Processing the `bar` declaration
-- 3. Processing the `baz` declaration
--
-- So GHC is already doing multiple passes, right?