Skip to content

Instantly share code, notes, and snippets.

View borkdude's full-sized avatar

Michiel Borkent borkdude

View GitHub Profile
@borkdude
borkdude / export.clj
Created May 17, 2022 13:27
export macro
foo=> (defmacro export [& var-names]
(let [var-names (set var-names)]
(doseq [[k v] (ns-publics *ns*)]
(when-not (contains? var-names k)
(alter-meta! v assoc :private true)))))
#'foo/export
foo=> (defn foo [])
#'foo/foo
foo=> (defn bar [])
#'foo/bar
@borkdude
borkdude / inc_number.joyride.cljs
Last active May 11, 2022 20:23
Increment number on line in joyride
(require '["vscode" :as vscode]
'[clojure.string :as str])
(defn insert-num
[]
(let [editor vscode/window.activeTextEditor
position (.-active (.-selection editor))
doc (.-document editor)
line (-> (.lineAt doc position) (.-text))
character (last (str/trim line))
@borkdude
borkdude / extension.js
Created April 22, 2022 19:24
Nbb in VSCode extension
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const path = require('path');
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
@borkdude
borkdude / reaction.cljs
Last active April 12, 2022 07:48
Reagent ratom / reaction
(require '[promesa.core :as p]
'[reagent.ratom :as ratom])
(def app-state (ratom/atom {:state-var-1 {:var-a 2
:var-b 3}
:state-var-2 {:var-a 7
:var-b 9}}))
(def app-var2a-reaction (ratom/make-reaction
#(get-in @app-state [:state-var-2 :var-a])))
@borkdude
borkdude / class_to_package.clj
Last active April 10, 2022 16:47
Retrieve the package string from a .class file
(require '[clojure.java.io :as io]
'[clojure.string :as str])
(defn class->package
"Implementation by Marco Marini."
[class-file]
(with-open [dis (java.io.DataInputStream. (io/input-stream class-file))]
;; skip first 8 bytes
(.skipBytes dis 8)
(let [constant-pool-count (.readUnsignedShort dis)
@borkdude
borkdude / deploy.sh
Created March 18, 2022 11:04 — forked from jackrusher/deploy.sh
Writing and deploying Google Cloud Functions in Clojure using NBB
# command line to deploy the project
gcloud functions deploy hello --runtime nodejs14 --trigger-http
@borkdude
borkdude / find_git_dirs.clj
Last active March 16, 2022 20:40
find_git_dirs.clj
(require '[babashka.fs :as fs])
(let [counter (volatile! 0)]
(fs/walk-file-tree "." {:pre-visit-dir (fn [d _]
(if (= ".git" (fs/file-name d))
(do (vswap! counter inc )
:skip-subtree)
:continue))})
@counter)
@borkdude
borkdude / polyglot.cljs
Created January 31, 2022 10:15
GraalVM Node + nbb + JVM Clojure
$ $GRAALVM_HOME/bin/node --jvm --polyglot --vm.cp=$(clojure -Spath) $(which nbb)
user=> (.-version js/process)
"v14.18.1"
user=> (require '["path" :as path])
nil
user=> (path/resolve ".")
"/Users/borkdude"
user=> (def Clojure (js/Java.type "clojure.java.api.Clojure"))
#'user/Clojure
user=> (def clj:require (.var Clojure "clojure.core" "require"))
@borkdude
borkdude / dev.docgen.clj
Created January 24, 2022 19:12
Babashka-compatible notebook solution from https://www.loop-code-recur.io/live-clojure-cookbooks/
(ns dev.docgen
(:require [clojure.pprint :refer [pprint]]
[rewrite-clj.zip :as z]
[clojure.string :as str]
[clojure.java.io :as io])
(:import (java.io File)))
(defn -form->markdown
"Clojure forms are evaluated and the result is automatically inserted in the generated markdown.
There is a special handling for 'def, we show their var binding instead"
(ns guestbook
(:require [cheshire.core :as cheshire]
[hiccup2.core :as hiccup]
[clojure.string :as str]))
(require '[babashka.pods :as pods])
(pods/load-pod "./pod-babashka-postgresql")
(def db {:dbtype "postgresql"
:user "guestbook"