Skip to content

Instantly share code, notes, and snippets.

View eneroth's full-sized avatar

Henrik Eneroth eneroth

View GitHub Profile
@eneroth
eneroth / gist:9261567
Last active August 29, 2015 13:56
Lyssna på enter (kolla konsollen)
;; Rendera en låda
;; ---------------
(defn command-box
[]
[:input#commandbox {}]) ;; "commandbox" är IDt
(render-component [command-box] (get-element "body"))
;; Lyssna på events fran elementet
;; -------------------------------
(let [grunka #(mate (rand-nth fittest) (rand-nth fittest))
pryl (repeatedly num-matings grunka)]
(scores goal (mutate-population pryl)))
(defn add [a b]
(+ a b))
(let [add-1 (partial add 1)]
(println (add-1 6))
(println (add-1 12)))
@eneroth
eneroth / Increment (Plain React).js
Last active February 19, 2021 11:14
Incrementing timer in React, in JavaScript and ClojureScript (Reagent)
var Timer = React.createClass({
getInitialState: function() {
return {secondsElapsed: 0};
},
tick: function() {
this.setState({secondsElapsed: this.state.secondsElapsed + 1});
},
componentDidMount: function() {
this.interval = setInterval(this.tick, 1000);
},

Keybase proof

I hereby claim:

  • I am eneroth on github.
  • I am eneroth (https://keybase.io/eneroth) on keybase.
  • I have a public key ASAcj6_a7ptkxlPrJD0OE87QcZzU8sBMesZE5fUb5UH-7wo

To claim this, I am signing this object:

;; Load config
(defn load-config [config-path]
(let [ch (chan)]
(go
(let [{:keys [succeeded data]} (<! (path/resolve config-path))
abs-path data]
(if-not succeeded
(failure ch data (str "load-config: couldn't resolve path '" config-path "'"))
(let [{:keys [succeeded data]} (<! (file/read abs-path :utf-8))
config-content data]
@eneroth
eneroth / strip_nil.clj
Last active August 2, 2018 14:06
Recursively strips values that are nil from a data structure. Will strip entire branches if they recursively evaluate to nil
(defn safe-seq
[thing]
(if (seqable? thing)
(seq thing)
true))
(defprotocol StripNil
(strip-nil [data]
"Recursively strips values that are nil from a datastructure.
@eneroth
eneroth / p_apply.clj
Last active October 10, 2018 17:23
Parallel application of a function over arguments, constrained by worker count
(defn- process-one
"Takes a channel and a function. Expects vectors of [index arguments]
on the channel. Takes from the channel, and applies the function
to the arguments part of the vector. Returns an output-channel,
on which the index and the result of the function application is returned."
[in-ch function]
(let [out-ch (chan)]
(go
(loop [item (<! in-ch)]
(if item
@eneroth
eneroth / is-balanced.clj
Created September 16, 2018 11:40
Uses reduce to check if brackets are balanced
(def brackets {\{ \}
\[ \]
\( \)})
(defn checker [stack char]
(if (some #{char} (keys brackets)) ;; Is the character an opening bracket?
(conj stack char)
(let [closing-char (get brackets (last stack))]
(if (= closing-char char)
@eneroth
eneroth / deep_merge.clj
Last active November 20, 2018 04:24
Deep merge with vector, seq and set handling
(defn- deep-merge
"Takes a collection of maps and deep merges them. I.e...
[{:a {:b 1}}
{:a {:c 2}}]
... will be merged into ...
[{:a {:b 1
:c 2}}]