Skip to content

Instantly share code, notes, and snippets.

@jaked
jaked / SketchSystems.spec
Created February 5, 2022 00:56
My Awesome Sketch
My Awesome Sketch
First State
some event -> Second State
Second State
// I was reading through these examples: http://apocalisp.wordpress.com/2010/06/08/type-level-programming-in-scala/
// and I thought it would be nice to more quickly get to something useful, to show the power of the techniques.
object SizedListExample {
// Type of all Non-negative integers
sealed trait Nat
// This is zero.
sealed trait _0 extends Nat
// Successor to some non-negative number
sealed trait Succ[N <: Nat] extends Nat
trait Comparable {
type T
def compare(a: T, b: T): Int
}
trait IInterval {
type T
type Endpoint
# -*- coding: utf-8 -*-
# (The MIT License)
#
# Copyright © 2012-2013 Felix Ren-Chyan Chern
#
# 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
@jaked
jaked / gist:6110141
Last active December 20, 2015 09:48
(defn count-ways-to-make-change [coins sum]
(defn sum-counts [counts]
(reduce + (map * coins counts)))
(defn next-counts [counts]
(loop [counts counts
digit 0]
(if (>= digit (count counts))
nil
(let [next (assoc counts digit (inc (get counts digit)))]
(if (<= (sum-counts next) sum)
@jaked
jaked / gist:6084411
Last active December 20, 2015 06:18
little Clojure hack to find functions by their behavior, like Smalltalk method finder
(defn find-functions [pred]
(let [pred (fn [fun]
(binding [*out* nil *err* nil clojure.test/*test-out* nil]
(try (pred fun)
(catch Throwable t false))))
excluded #{'clojure.core/read-line 'clojure.main/repl-read}]
(for [ns (all-ns)
[name fun] (ns-publics ns)
:let [sym (symbol (str (ns-name ns)) (str name))]
:when (not (excluded sym))
import java.util.concurrent.{BlockingQueue, ArrayBlockingQueue, CountDownLatch, Executors, TimeUnit}
/*
Abstracts away the common pattern of producing items into a queue that are
consumed concurrently by a pool of workers.
*/
class ConcurrentBlockingQueueConsumer[T](queue: BlockingQueue[T], producer: Iterator[T], concurrencyLevel: Int) {
lazy val pool = Executors.newFixedThreadPool(concurrencyLevel)
def run()(consumer: (T) => Unit) {
type 'a stream = S of 'a * 'a stream Lazy.t
let rec map f s =
match s with
| lazy (S (h, t)) -> S (f h, lazy (map f t))
let rec nth s n =
match s, n with
| lazy (S (h, _)), 0 -> h
| lazy (S (_, t)), n -> nth t (n - 1)
module type Equal = sig
type fst
type snd
module Coerce :
functor (F : sig type 'a t end) -> sig
val f : fst F.t -> snd F.t
end
end
type ('a, 'b) equal = (module Equal with type fst = 'a and type snd = 'b)
@jaked
jaked / gist:166609
Created August 12, 2009 16:51 — forked from avsm/gist:166441
(* defines the Ast.binding for a function of form:
let fun_name ?(opt_arg1) ?(opt_arg2) final_ident = function_body ...
*)
let function_with_label_args _loc ~fun_name ~final_ident ~function_body ~return_type opt_args =
let opt_args = opt_args @ [ <:patt< $lid:final_ident$ >> ] in
let rec fn _loc = function (* could lose _loc and use List.fold_right *)
|hd::tl -> <:expr< function $hd$ -> $fn _loc tl$ >>
|[] -> <:expr< ( $function_body$ : $return_type$ ) >>
in
<:binding< $lid:fun_name$ = $fn _loc opt_args$ >>