Skip to content

Instantly share code, notes, and snippets.

View stathissideris's full-sized avatar

Stathis Sideris stathissideris

  • London
View GitHub Profile
(def x [["/foo"
["/bar" :bar]
["/baz"
["/quux" :quux]]]
["/yo"
[["/man" :man]
["/manman" :manman]]]])
(require '[clojure.zip :as z]
'[clojure.string :as str])
@stathissideris
stathissideris / core.clj
Last active November 26, 2020 22:26
Basic Clojure example for Skija
(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 September 9, 2020 09:21
Example of a tiny menu system using read-line in Clojure
;; 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 September 9, 2020 05:51
An attempt to use Datascript for state management in a Swing app (no React)
(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)
{: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 March 10, 2019 18:24
pastebox for Clojure
;; 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 September 28, 2018 12:21
Hierarchical table "syntax" for Clojure Pedestal
(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 August 19, 2018 23:11
Sudoku solver clojure
(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 June 6, 2018 09:24
Alias Clojure namespace without requiring it (helps with long namespaced keywords)
(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 February 11, 2018 09:26
Simple pedestal interceptors to log how the context changes as it gets passed around in the chain
(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))))