Skip to content

Instantly share code, notes, and snippets.

View roman01la's full-sized avatar
🇺🇦

Roman Liutikov roman01la

🇺🇦
View GitHub Profile
(ns user
#?(:cljs (:require-macros [user])
:clj (:require [cljs.source-map :as sm]
[clojure.data.json :as json]
[clojure.java.io :as io]
[clojure.string :as str]
[cljs.env :as env])))
#?(:clj
(defn map-location [env form & {:keys [offset]}]

Rum 0.11.5 release notes

Happy to announce that Rum 0.11.5 has been released with quite a few bug fixes and new features.

React Hooks

For anyone excited about this, now you can use React Hooks in Rum. Hooks work only in function-based React components, but because Rum already generates those when using defc macro it was trivial to add support for them without breaking changes.

Hooks are compatible only with defc components and only without any mixins, except rum/static. This distinction exists because mixins are meant to be used in lifecycle methods of React's class-based components. You should be using either mixins or hooks in a single component, mixing those together would generate class-based components and hooks won't work.

Another important note is that Clojure's equality semantics does not apply to dependencies collection is Hooks, at least as of now. Instead React compares them by identity as usual in JavaScript. For Rum users this means that values in d

mkdir hello-bundler
cd hello-bundler
echo '{:deps {org.clojure/clojurescript {:mvn/version "1.10.741"}}}' > deps.edn
@roman01la
roman01la / clojurescript-repl-workflow.md
Last active October 22, 2022 12:07
ClojureScript REPL Workflow

ClojureScript REPL Workflow

Short write up on using REPL when developing UIs in ClojureScript.

Hot-reload on save or Eval in REPL?

Everyone's standard approach to hot-reloading is to use a tool (Figwheel or shadow-cljs) that reloads changed namespaces automatically. This works really well: you change the code, the tool picks up changed files, compiles namespaces and dependants, notifies REPL client which then pulls in compiled changes, and re-runs a function that re-renders UI.

The other approach is to use ClojureScript's REPL directly and rely only on eval from the editor. This more or less matches Clojure style workflow. This approach might be useful when you don't want tools overhead or hot-reloading becomes slow for you or you just used to this style of interactions. Also changing code doesn't always mean that you want to reload all the changes. On the other hand it is very easy to change a couple of top-level forms and forget to eval one of them.

@roman01la
roman01la / index.md
Last active January 7, 2020 09:03
Abusing constants table in ClojureScript's compiler

Abusing constants table in ClojureScript's compiler

In React wrapper library UIx that I'm working on there's defui macro that compiles Hiccup directly into React's VirtualDOM. Apart from doing that the macro also hooks into the compiler to hoist constant parts of VirtualDOM across components and namespaces, so that those parts will be essentially interned (cached).

Here's an example of two components defined in different namespaces where both of them share a part of the structure.

(ns foo.core
  (:require [uix.core.alpha :refer [defui]]))
@roman01la
roman01la / day8.clj
Created December 9, 2019 16:35
AoC 2019, Day 8
(def input
(->> (.trim (slurp "input_2019_8"))
(map #(Character/digit ^char % 10))))
(def image-width 25)
(def image-height 6)
(def pixels (* image-width image-height))
(defn solve-day8-p1 [input]
(->> input
@roman01la
roman01la / day6.clj
Created December 6, 2019 11:49
AoC 2019, Day 6
(def input
(-> (slurp "input_2019_6")
clojure.string/trim
(clojure.string/split #"\n")))
(def orbits-graph
(->> input
(map #(clojure.string/split % #"\)"))
(reduce (fn [ret [v k]]
(assoc! ret k v))
@roman01la
roman01la / day5.clj
Created December 5, 2019 20:10
AoC 2019, Day 5
(defn prefix-params [default params]
(let [diff-ln (- (count default) (count params))]
(if-not (== 0 diff-ln)
(str (subs default 0 diff-ln) params)
params)))
(defn read-instruction [s]
(case s
"1" [1 0 0 1]
"2" [2 0 0 1]
@roman01la
roman01la / day4.clj
Last active December 4, 2019 08:08
AoC 2019, Day 4
(defn increasing? [d]
(re-matches #"1*2*3*4*5*6*7*8*9*" d))
(defn repeating-by? [op s]
(->> (frequencies s)
(some #(op 2 (val %)))))
(defn solve-day4-p1 [from to]
(->> (range from (inc to))
(map str)
@roman01la
roman01la / day03.clj
Last active December 4, 2019 00:22
AoC 2019, Day 3
(defn read-move [s]
[(keyword (subs s 0 1))
(read-string (subs s 1))])
(comment
(= [:R 1009] (read-move "R1009")))
(def input
(->> (slurp "input_2019_3")
(clojure.string/split-lines)