Skip to content

Instantly share code, notes, and snippets.

Avatar

Stathis Sideris stathissideris

  • London
View GitHub Profile
@stathissideris
stathissideris / core.clj
Last active Nov 26, 2020
Basic Clojure example for Skija
View core.clj
(ns skija.core
(:import [org.jetbrains.skija Canvas Surface Paint Color4f
EncodedImageFormat]
[java.nio.file Path Files OpenOption]))
(defn write-bytes [#^bytes b path]
(Files/write (Path/of "output.png" (make-array String 0)) b (make-array OpenOption 0)))
(defn show []
(let [surface (Surface/makeRasterN32Premul 100 100)
@stathissideris
stathissideris / read-line-menu.clj
Created Sep 9, 2020
Example of a tiny menu system using read-line in Clojure
View read-line-menu.clj
;; Usage:
(menu {:prompt "Which database"
:options ["Localhost" "Remote" {:id "o" :text "Other"}]})
;; Implementation
(require '[clojure.string :as str])
(defn menu [{:keys [prompt options]}]
(let [options (map (fn [o idx]
@stathissideris
stathissideris / not-posh-at-all.clj
Last active Sep 9, 2020
An attempt to use Datascript for state management in a Swing app (no React)
View not-posh-at-all.clj
(ns not-posh
(:require [clojure.string :as str]
[clojure.data :as data]
[datascript.core :as d]
[clojure.set :as set]
[seesaw.core :as ss]))
(defn- diff [a b]
;;TODO make diff ignore :swing/component key but include it in result
(let [[removed added] (data/diff a b)
View duna.clj
{:resources
[{:duna/type :provider/aws
:region "eu-west-1"}
{:duna/type :data/aws_ami
:duna/id :ubuntu
:most_recent true
:filter [{:name "name"
:values ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]}
{:name "virtualization-type"
:values ["hvm"]}]
@stathissideris
stathissideris / pastebox.clj
Last active Mar 10, 2019
pastebox for Clojure
View pastebox.clj
;; This will open a window with a single textfield where you can paste some HTML,
;; press <enter> and you should see a valid Clojure string get printed in your REPL,
;; without having to worry about escaping double quotes etc.
;; AND it fits a single tweet. :)
(import '[javax.swing JFrame JTextField]
'[java.awt.event ActionListener])
(doto (JFrame.)
(.add (doto (JTextField.)
@stathissideris
stathissideris / pedestal.clj
Created Sep 28, 2018
Hierarchical table "syntax" for Clojure Pedestal
View pedestal.clj
(defn- add-path-prefix [[path & more] prefix]
(vec (concat [(str prefix path)] more))) ;;TODO handle stray slashes
(defn- prepend-interceptors [[path verb chain & more] interceptors]
(let [new-chain (if (vector? chain)
(vec (concat interceptors chain))
(vec (concat interceptors [chain])))]
(vec (concat [path verb new-chain] more))))
@stathissideris
stathissideris / solver.clj
Created Aug 19, 2018
Sudoku solver clojure
View solver.clj
(ns sat.core
(:require [rolling-stones.core :as sat :refer [!]]
[clojure.string :as str]))
(def rows 9)
(def cols 9)
(def values 9)
(defn possible-square-values
@stathissideris
stathissideris / alias.clj
Created Jun 6, 2018
Alias Clojure namespace without requiring it (helps with long namespaced keywords)
View alias.clj
(defn ensure-namespace-alias [ns alias-sym]
(when-not (find-ns ns)
(create-ns ns))
(alias alias-sym ns))
(ensure-namespace-alias 'this.is.a.long.namespace 'long)
::long/foo => :this.is.a.long.namespace/foo
@stathissideris
stathissideris / pedestal-chain-diff.clj
Created Feb 11, 2018
Simple pedestal interceptors to log how the context changes as it gets passed around in the chain
View pedestal-chain-diff.clj
(require '[io.pedestal.log :as log]
'[clojure.data :as diff]
'[io.pedestal.interceptor.chain :as chain])
(defn log-diffs [previous current]
(let [[deleted added] (diff/diff (dissoc previous ::chain/queue ::chain/stack ::previous-ctx)
(dissoc current ::chain/queue ::chain/stack ::previous-ctx))]
(when deleted (log/debug :deleted deleted))
(when added (log/debug :added added))))
@stathissideris
stathissideris / trace-forms.clj
Last active Nov 30, 2017
naive Clojure macro for tracing all forms
View trace-forms.clj
;; there are quite a few cases where this code would not work, use with caution and check the macro expansion
(require '[clojure.walk :as walk])
(defn- clean-dont [form]
(walk/postwalk
(fn [form]
(if (and (list? form) (= 'dont (first form)))
(second form)
form))