Skip to content

Instantly share code, notes, and snippets.

@Engelberg
Engelberg / Logic2.clj
Created March 7, 2013 05:52
Solving logic puzzle with rules in more efficient order
(defn logic-puzzle []
(let [people [:amaya :bailey :jamari :jason :landon]]
(for [[fortune time cosmopolitan us-weekly vogue] (permutations people) ; magazines
:when (not= fortune :jamari)
[asiago blue-cheese mascarpone mozzarella muenster] (permutations people) ; cheeses
:when (= blue-cheese fortune)
:when (not= muenster vogue)
reservations (permutations people)
@Engelberg
Engelberg / Logic1.clj
Created March 7, 2013 05:49
Solving logic puzzle with for comprehension
(defn solve-logic-puzzle []
(let [people [:amaya :bailey :jamari :jason :landon]]
(first
(for [[fortune time cosmopolitan us-weekly vogue] (permutations people) ; magazines
[asiago blue-cheese mascarpone mozzarella muenster] (permutations people) ; cheeses
; We bind the reservations in two steps, so we have a name for the overall order
reservations (permutations people)
:let [[five six seven seven-thirty eight-thirty] reservations]
; THE CONSTRAINTS IN PLAIN ENGLISH
@Engelberg
Engelberg / electrs on dojo
Last active October 5, 2022 18:26
Installing local electrum server that relies on the bitcoin daemon inside of Samourai Dojo for its data
I am successfully running a local electrum server, getting its data from dojo. This is useful for private use of hardware wallets.
Step 1: As Laurent MT suggested in the Samourai telegram group, you need to edit the docker-compose.yaml file, adding to the bitcoind section the following two lines
ports:
- "127.0.0.1:28256:28256"
Step 2: Follow install directions for electrs, an electrum server written in rust.
electrs install directions can be found here: https://github.com/romanz/electrs/blob/master/doc/usage.md
@Engelberg
Engelberg / Poor man's hardware wallet
Last active June 5, 2019 23:53
"Poor man's hardware wallet"
For those who cannot afford a hardware wallet, the next best thing is to use Electrum
to create a 2-of-2 multisig wallet with one key on your phone and one on your desktop.
To use, create the transaction on your desktop client, and then display the QR code
for the partially signed transaction. Scan the QR code in your phone's Electrum,
sign it, and broadcast with a few taps.
Given the added difficulty of a hacker accessing both your desktop and your phone,
you get significantly better security and the process of using the wallet remains
relatively straightforward.
@Engelberg
Engelberg / cond-example.clj
Created April 3, 2017 06:07
refactoring with better-cond
; The sample to refactor
(if-let [x (foo)]
(if-let [y (bar x)]
(if-let [z (goo x y)]
(do
(qux x y z)
(log "it worked")
true)
(do
(log "goo failed")
@Engelberg
Engelberg / core_subset.clj
Created August 2, 2018 23:06
Comparison Clojure to Java
public class CoreSubsetObjective implements Objective<SubsetSolution, CoreSubsetData>{
/**
* Evaluates the given subset solution using the underlying data, by computing the average
* distance between all pairs of selected items. If less than two items are selected,
* the evaluation is defined to have a value of 0.0.
*
* @param solution subset solution
* @param data core subset data
* @return evaluation with a value set to the average distance between all pairs of selected items;
@Engelberg
Engelberg / Frequencies.md
Last active March 22, 2018 22:06
An analysis of different methods for implementing frequencies in Clojure

First, here is the cleverly concise version of freqs that I showed in class, which builds a bunch of maps that map each element to the number 1, and then merges them together using +:

(defn freqs [l]
  (apply merge-with + (for [item l] {item 1})))

Here is one way of coding it using loop-recur:

(ns utils.cond
"A collection of variations on Clojure's core macros. Let's see which features
end up being useful."
{:author "Christophe Grand"}
(:refer-clojure :exclude [cond when-let if-let]))
(defmacro if-let
"A variation on if-let where all the exprs in the bindings vector must be true.
Also supports :let."
([bindings then]
@Engelberg
Engelberg / wordlist.txt
Created February 21, 2017 07:30
Esperanto wordlist from Baza Radikaro with unique 3-letter prefixes
abelo
abio
abomeno
abrikoto
absoluta
abunda
acida
adapti
adicii
adjektivo
@Engelberg
Engelberg / core.clj
Last active January 5, 2017 09:30
Twenty four using partitions
(ns twentyfour.core
(:use clojure.math.combinatorics))
(def ops ['+ '- '* '/])
(def commutative #{'+ '*})
;; We can generate all the possible expressions efficiently with combinatorics' partitions
;; partitions automatically handles duplicates for us, which keeps the process efficient
(defn expressions [nums]