Skip to content

Instantly share code, notes, and snippets.

View metametadata's full-sized avatar

Yuri Govorushchenko metametadata

View GitHub Profile
@metametadata
metametadata / multi-plus.core.clj
Created June 16, 2019 20:29
Multimethod helpers
(ns multi-plus.core)
(defn multimethod-map
"Converts multimethod into a map. :default dispatch value will be omitted.
Expected multimethod signature: (defmulti mm (fn [v] v)). It should be pure."
[mm]
(into {}
(for [[dispatch-value method] (methods mm)
:when (not= dispatch-value :default)]
[dispatch-value (method dispatch-value)])))
(ns spec-plus.core
(:require #?@(:clj [[clojure.spec.alpha :as s]]
:cljs [[cljs.spec.alpha :as s]])
[clojure.set :as set])
#?(:cljs (:require-macros [spec-plus.core])))
#?(:clj
(defn -cljs-env?
"Take the &env from a macro, and tell whether we are expanding into cljs.
Source: https://groups.google.com/d/msg/clojurescript/iBY5HaQda4A/w1lAQi9_AwsJ"
@metametadata
metametadata / rebel.clj
Last active January 1, 2019 18:39
Customized rebel-readline REPL
(ns repl.rebel
"Entry point for starting rebel-readline REPL.
Inspired by rebel-readline.main.
Usage: [LEIN_FAST_TRAMPOLINE=1] lein trampoline run -m repl.rebel [init-ns]"
(:require [clojure.repl :as clj-repl]
[rebel-readline.core :as rebel-core]
[rebel-readline.clojure.main :as rebel-clj-main]))
(defn -handle-sigint-form
@metametadata
metametadata / helpers.clj
Created November 14, 2018 12:21
Faster PostgreSQL upsert in HoneySQL and funcool/clojure.jdbc
(ns app.jdbc.helpers
(:require [jdbc.core :as jdbc]
[jdbc.proto :as jdbc-proto]
[honeysql.core :as sql]
; Load extensions
[honeysql-postgres.format]))
(defn -upsert-sql
[table keys pk]
@metametadata
metametadata / temp.cljs
Created February 22, 2018 18:24
with-temp-file in Lumo
(require '[lumo.io :as lumo-io])
(def -fs-extra (js/require "fs-extra"))
(defn with-temp-file
"Temporarily creates a file with the specified content and calls f."
[path content f]
(lumo-io/spit path content)
(try
(f)
@metametadata
metametadata / jenkins-api-token.groovy
Created February 6, 2018 03:34
Jenkins API token
// API token can also be found in GUI at http://<jenkins>/configure/me.
def user = "admin"
def token = User.get(user).getProperty(ApiTokenProperty.class).getApiToken()
@metametadata
metametadata / ip.py
Created February 6, 2018 03:31
Cross-platform local IP detection.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Prints the machine's IP address.
# Inspired by https://stackoverflow.com/a/28950776/4839573. Tested in Linux and MacOS.
import socket
import sys
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# Doesn't even have to be reachable.
@metametadata
metametadata / jenkins.groovy
Created February 6, 2018 03:28
Read port from "Jenkins URL" field that is set in http://<jenkins>/configure.def
def jenkinsPort = new URI(jenkins.model.JenkinsLocationConfiguration.get().getUrl()).port
@metametadata
metametadata / external-config.md
Last active January 9, 2020 07:06
ClojureScript compilation-time project configuration using external-config

Example of providing custom compilation-time configuration option :debug in ClojureScript project. It's a better alternative to goog defines.

project.clj:

,,,

:cljsbuild {:builds [{:id           "dev"
                      :source-paths ["src"]
                      :compiler     {:main            app.core
@metametadata
metametadata / reporter.clj
Created November 3, 2017 15:29
Prettifying Clojure test reports
; Prettify test reports
(ns unit.reporter
(:require
[clojure.test :refer [report with-test-out]]))
; Change the report multimethod to ignore namespaces that don't contain any tests.
; taken from: http://blog.jayfields.com/2010/08/clojuretest-introduction.html
(defmethod report :begin-test-ns [m]
(with-test-out
(when (some #(:test (meta %)) (vals (ns-interns (:ns m))))