Skip to content

Instantly share code, notes, and snippets.

View scotthaleen's full-sized avatar
λ

スコット scotthaleen

λ
View GitHub Profile
@scotthaleen
scotthaleen / reflection.clj
Created March 21, 2019 17:17 — forked from sunng87/reflection.clj
clojure: access private field/method via reflection
(defn invoke-private-method [obj fn-name-string & args]
(let [m (first (filter (fn [x] (.. x getName (equals fn-name-string)))
(.. obj getClass getDeclaredMethods)))]
(. m (setAccessible true))
(. m (invoke obj args))))
(defn private-field [obj fn-name-string]
(let [m (.. obj getClass (getDeclaredField fn-name-string))]
(. m (setAccessible true))
(. m (get obj))))
@scotthaleen
scotthaleen / new-instance.clj
Created February 7, 2019 23:53
Clojure dynamically create new instances of a Class
(defn new-instance
" creates a new Instance of a Class
Passing a Class will result in Class.newInstance
(new-instance String)
Passing a Class and vector of args will attempt to match the Constructor
by creating a Class[] by calling `class` on each arg
@scotthaleen
scotthaleen / socket-send-macro.clj
Created August 24, 2018 16:36
Send form(s) to clojure socket repl
;;; https://clojure.org/reference/repl_and_main#_launching_a_socket_server
;;; -Dclojure.server.repl="{:port 5555 :accept clojure.core.server/repl}"
(defmacro send-forms [host port form & forms]
`(with-open [socket# (java.net.Socket. ~host ~port)]
(let [pw# (java.io.PrintWriter. (.getOutputStream socket#) true)
in# (java.io.BufferedReader. (java.io.InputStreamReader. (.getInputStream socket#)))
forms# (list ~(str form) ~@(map str forms))]
(reduce
@scotthaleen
scotthaleen / fizzbuzz-y-combinator.clj
Created May 24, 2018 21:20
clojure fizzbuzz with y-combinator
(defn fizzbuzz
"
rules - map of number and string ex. {3 \"fizz\" 5 \"buzz\"}
limit - number to count too
"
[rules limit]
(((fn [f] (f f))
(fn [f]
(fn [x]
@scotthaleen
scotthaleen / gist:2bae4707fbe944e7ee801f4fd42f351c
Created May 17, 2018 02:56 — forked from also/gist:7729708
how does /usr/libexec/java_home work?
# /usr/libexec/java_home -X
$ sudo opensnoop -n java_home
UID PID COMM FD PATH
501 79809 java_home 3 /System/Library/PrivateFrameworks/JavaLaunching.framework/Versions/A/JavaLaunching
501 79809 java_home 3 /dev/dtracehelper
501 79809 java_home 4 /System/Library/CoreServices/SystemVersion.bundle//English.lproj
501 79809 java_home -1 /System/Library/CoreServices/SystemVersion.bundle//Base.lproj
501 79809 java_home 4 /System/Library/CoreServices/SystemVersion.bundle/English.lproj/SystemVersion.strings
501 79809 java_home -1 /System/Library/CoreServices/SystemVersion.bundle/English.lproj/SystemVersion.stringsdict
501 79809 java_home 3 /usr/share/icu/icudt51l.dat
@scotthaleen
scotthaleen / scala_styles.md
Created September 19, 2017 03:05
scala style

Inference

Use type inference where possible, but put clarity first, and favour explicitness in public APIs.

Almost never annotate the type of a private field or a local variable

All public methods should have explicit type annotations.

@scotthaleen
scotthaleen / ufmongo.py
Last active May 19, 2017 20:31
ufmongo merger
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os, argparse
import logging
import csv
import json
from functools import partial
@scotthaleen
scotthaleen / docker_adjust_memory.sh
Created December 19, 2016 22:21
Adjust Docker Machine Memory & CPU
# Adjust Docker Machine Memory+CPU
# http://stackoverflow.com/questions/32834082/how-to-increase-docker-machine-memory-mac
docker-machine stop
VBoxManage modifyvm default --cpus 2
VBoxManage modifyvm default --memory 4096
docker-machine start
@scotthaleen
scotthaleen / ns_qualifed_keys.clj
Created December 9, 2016 15:28
fully qualify keys for a namespace
(defn ns-qualified-keys
"
takes a namespace and map of kv and converts it to namespace qualified keys
(ns-qualified-keys 'foo.bar {:port 80})
;;=> {:foo.bar/port 80}
"
[ns m]
(into {}
(for [[k v] m]
@scotthaleen
scotthaleen / lein_uberjar_git_rev.clj
Created December 6, 2016 17:52
add the latest git revision to the uberjar file name
;; add the latest git revision to the uberjar file name
(defproject foo "4.3"
;;....
:uberjar-name ~(str
"foo-%s"
(try
(str "-"