Skip to content

Instantly share code, notes, and snippets.

View stathissideris's full-sized avatar

Stathis Sideris stathissideris

  • London
View GitHub Profile
@stathissideris
stathissideris / tree-seq-extra.clj
Last active July 2, 2023 11:33
Like Clojure's tree-seq, but with depth info for each node or the full path (recursive - blow up the stack for deep trees)
(defn tree-seq-depth
"Returns a lazy sequence of vectors of the nodes in a tree and their
depth as [node depth], via a depth-first walk. branch? must be a fn
of one arg that returns true if passed a node that can have
children (but may not). children must be a fn of one arg that
returns a sequence of the children. Will only be called on nodes for
which branch? returns true. Root is the root node of the tree."
[branch? children root]
(let [walk (fn walk [depth node]
(lazy-seq
(def x [["/foo"
["/bar" :bar]
["/baz"
["/quux" :quux]]]
["/yo"
[["/man" :man]
["/manman" :manman]]]])
(require '[clojure.zip :as z]
'[clojure.string :as str])
@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 / 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 / channel-as-seq.clj
Last active October 20, 2020 19:14
clojure async channels as lazy (and blocking!) sequences
(defn seq!!
"Returns a (blocking!) lazy sequence read from a channel."
[c]
(lazy-seq
(when-let [v (<!! c)]
(cons v (seq!! c)))))
(comment
(def stream (chan))
(go (dotimes [x 16]
@stathissideris
stathissideris / HashOfHashes.java
Created August 26, 2011 10:48
Hash of hashes example in Java
package test;
import java.util.HashMap;
public class HashOfHashes {
public static void main(String[] args) {
//deep's type is a HashMap with String keys. Its values are Hashes with String keys and values
//Notice how you have to say all that twice, just to be sure :-/
HashMap<String, HashMap<String, String>> deep = new HashMap<String, HashMap<String, String>>();
@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.)