Skip to content

Instantly share code, notes, and snippets.

(ns roman-nums.core
(:require
[clojure.test :refer [testing are]]))
(def ^:private roman-digits
[[1000 "M"] [900 "CM"]
[500 "D"] [400 "CD"]
[100 "C"] [90 "XC"]
[50 "L"] [40 "XL"]
[10 "X"] [9 "IX"]
@erdos
erdos / split-by-key1.clj
Last active November 20, 2016 22:16
split-by-key returning groups may not cover whole input
(do
;; number of groups
(def n (.availableProcessors (Runtime/getRuntime)))
;; memoized round-robin style group number generator for items
(let [a (atom (cycle (range n)))]
(def get-id-for
(memoize (fn [_] (first (swap! a next))))))
(defn split-by-key
@erdos
erdos / bcug-2016-11-24.md
Last active November 23, 2016 07:53
BCUG presentation - we dont need no types

Budapest Clojure User Group - meetup presentation proposal for 2016-11-24

Schema vs. Spec

Validating data in dynamic typed programming languages is an important aspect of making sure our programs are safe and correct. In the short presentation we will take a look at two solutions for validating data in Clojure.

Prismatic Schema is a mature library with simple syntax and strong support in the Clojure ecosystem. The new competitor is core.specs supporting in the core language with promising new features. After the presentation we will discuss personal experiences with the advantages and disadvantages of both solutions.

@erdos
erdos / lis.clj
Created June 6, 2017 17:01
Longest Increasing Subsequence in Clojure
(defn filt [sequences]
(mapv (partial apply min-key peek) (vals (group-by count sequences))))
(defn rfn [seqs x]
(filt (concat [[x]] seqs (for [cs seqs :when (> x (peek cs))] (conj cs x)))))
(defn longest-subseq [xs]
(apply max-key count [] (reduce rfn nil xs)))
@erdos
erdos / rpn.clj
Created June 7, 2017 08:35
Simple Reverse Polish Notation Calculator in Clojure
(defn rpn [s]
(first (reduce (fn [[s1 s2 & ss :as stack] op]
(case op
+ (cons (+ s1 s2) ss)
- (cons (- s1 s2) ss)
* (cons (* s1 s2) ss)
/ (cons (/ s1 s2) ss)
, (cons op stack)))
() (read-string (str \( s \))))))
@erdos
erdos / document.xml
Created May 1, 2018 16:06
ooxml gridspan example
<?xml version="1.0" encoding="UTF-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
<w:body>
<w:p>
<w:pPr>
<w:pStyle w:val="Normal" />
<w:rPr />
</w:pPr>
<w:r>
<w:rPr />
@erdos
erdos / hungarian.clj
Last active April 24, 2019 08:10
egesz szamok magyar nyelvu szoveges kiirasa
(ns hungarian
(:require [clojure.string]))
(defn- fmt [num]
(cond
(>= num 1000_000_000)
(let [szorzo (quot num 1000_000_000)
maradek (rem num 1000_000_000)]
(str (fmt szorzo) "milliárd" (when (pos? maradek) (str "-" (fmt maradek)))))
@erdos
erdos / tester.clj
Created September 28, 2018 13:55
make all private vars visible for unit testing
(let [target (the-ns 'testable-namespace)]
(doseq [[k v] (ns-map target)
:when (and (var? v) (= target (.ns v)))]
(eval `(defn ~(symbol (str "-" k)) [~'& args#] (apply (deref ~v) args#)))))
;; Now if testable-namespace has a function valled fun1
;; then i am able call -fun1 from the current namespace.
@erdos
erdos / zippers.clj
Created June 26, 2019 18:40
Budapest Clojure User Group 2019 June
; root
; / \ \
; 1 [2 3] [4 5]
; /\ /\
; 2 3 4 5
(require '[clojure.zip :as zip])
@erdos
erdos / SuffixArray.java
Created February 2, 2020 21:39
O(nlogn) suffix array algo
package dev.erdos.algo;
import java.util.Arrays;
public class SuffixArray {
// O(n*logn) algorithm for constructing a suffix array.
// Original source: https://cp-algorithms.com/string/suffix-array.html#toc-tgt-3
public static int[] calculateSuffixArray(char[] s) {