Skip to content

Instantly share code, notes, and snippets.

View mattmoss's full-sized avatar
💻
Coding things, making stuff…

Matthew Moss mattmoss

💻
Coding things, making stuff…
View GitHub Profile
@mattmoss
mattmoss / globgrep.v
Last active March 29, 2021 18:59
Basic implementation of globgrep (https://zserge.com/posts/better-c-benchmark/) using V (vlang.io)
module main
import os
fn glob(pattern string, text string) bool {
plen := pattern.len
tlen := text.len
mut p := 0
mut t := 0
mut np := 0

Keybase proof

I hereby claim:

  • I am mattmoss on github.
  • I am mattmoss (https://keybase.io/mattmoss) on keybase.
  • I have a public key ASBJyGO254oviU7KjO6BVBTiBpnO8T_ikM6Y7iWmej61XAo

To claim this, I am signing this object:

@mattmoss
mattmoss / slideshow.groovy
Last active October 5, 2017 16:35
Building a reveal.js slideshow w/ Groovy
import groovy.xml.MarkupBuilder
class Slideshow {
Map options
StringWriter writer = new StringWriter()
MarkupBuilder markup = new MarkupBuilder(writer)
boolean inGroup = false
boolean inSlide = false
@mattmoss
mattmoss / gist:4068423
Created November 13, 2012 21:10
Using conch to start/stop a server in a clojure.test fixture
(ns test.connection
(:require [clojure.test :refer :all]
[conch.core :as conch]
[conch.sh :as sh]))
(def ^:private process-name "./server.sh")
(def ^:private process-dir "/Users/mattmoss/samples/")
(def ^:private patt-server-url #"Server bound to (http://localhost:\d+)")
@mattmoss
mattmoss / gist:4063931
Created November 13, 2012 04:26
Losing lines from process :out
(ns user (:require [conch.core :as sh]))
;; p is a process returned from (sh/proc ...)
(defn read-lines-until [p re]
(loop [line (sh/read-line p :out)]
(when line
(if-let [match (re-find re line)]
match
(recur (sh/read-line p :out))))))
@mattmoss
mattmoss / map_filter_map.clj
Created November 1, 2012 02:06
Given two sequences of equal length and corresponding items, can one sequence be filtered based on (a predicate fn applied to) the second?
;; Given two sequences (seq-a and seq-b) of equal length and corresponding
;; items, can one sequence be filtered according to a predicate function (pred?)
;; applied to the second?
(map second
(filter #(pred? (first %))
(map list seq-a seq-b)
@mattmoss
mattmoss / handle-pool.clj
Created August 21, 2012 19:00
Data and methods to generate unique numeric ids/handles upon request, reusing released handles.
(defn inc-wrap
"increment with wraparound to 0; limit should be positive"
[x limit]
(if (< x limit) (inc x) 0))
(defn handle-pool
[limit]
(ref {:next 0 :limit limit :in-use #{}}))
(defn- unique-handle
@mattmoss
mattmoss / restart-agent within agent error-handler
Created August 8, 2012 19:40
Why does x end with value 1 rather than 0?
(defn test-agent-restart []
(let [x (agent 0 :error-handler (fn [a ex] (println ex) (restart-agent a 0)))]
(println @x)
(send x inc)
(await x)
(println @x)
(send x (fn [_] (throw (Exception. "whatever"))))
(await x)
(println @x)))
@mattmoss
mattmoss / gist:3013845
Created June 28, 2012 20:54
How to handle Java Listeners (anon interfaces) of multiple functions from off the main thread...
(defn- some-listener
[event-1 event-2 event-3]
(reify SomeListener
(onEvent1 [this x] (send event-1 (constantly x)))
(onEvent2 [this x] (send event-2 (constantly x)))
(onEvent3 [this x y] (send event-3 (constantly [x y])))))
(defn make-thing
[]
(let [event-1 (agent nil)
(defn gen-id-dispatch
[& args]
(let [n (count args)]
(cond
(= n 1) (class (first args))
(= n 2) :pair
:else nil)))
(defmulti gen-id #'gen-id-dispatch :default nil)