Skip to content

Instantly share code, notes, and snippets.

View ichramm's full-sized avatar

Juan Ramirez ichramm

View GitHub Profile
@ichramm
ichramm / remove-all-but-last.clj
Created June 4, 2020 13:54
Removes all elements matching a predicate, except the last one
(defn remove-all-but-last
[pred coll]
(let [[_ count index] (reduce #(if (pred %2)
[(inc (% 0)) (inc (% 1)) (% 0)]
[(inc (% 0)) (% 1) (% 2)])
[0 0 nil]
coll)]
(if (> count 1)
(keep-indexed #(and (or (not (pred %2)) (= index %1) nil) %2) coll)
coll)))
@ichramm
ichramm / extract_pdf_text.clj
Created May 13, 2020 19:09
extract pdf text
(import org.apache.pdfbox.pdmodel.PDDocument)
(import org.apache.pdfbox.text.PDFTextStripper)
(import java.net.URL)
(defn extract-pdf-text
[url]
(with-open [pd (PDDocument/load (.openStream (URL. url)))]
(let [stripper (PDFTextStripper.)]
(.getText stripper pd))))
@ichramm
ichramm / normal_deltas.clj
Created May 13, 2020 03:54
normal distribution of deltas
; data is a list of numbers
(let [deltas (loop [res '() rem data]
(if (empty? rem)
res
(recur (concat res
(map #(Math/abs (- % (first rem))) (rest rem)))
(rest rem))))
total (count deltas)
mean (/ (reduce + 0 deltas) (count deltas))
std-dev (-> (reduce #(+ % (Math/pow (- %2 mean) 2))
@ichramm
ichramm / p05e09.cpp
Last active May 11, 2020 13:49
p05e09
monitor coso_mojo
bool enviando;
condition cond_enviar;
int cant_esperando;
@ichramm
ichramm / destrcuture-or-index-defrecord.clj
Created April 28, 2020 16:25
destructure or index defrecord in clojure
(defrecord Right [value]
Either
(success? [_]
true)
; support destructuring: (let [[val err] (f/attempt->....))] ...)
clojure.lang.Indexed
(nth [_ i]
(case i 0 value 1 nil (throw (IndexOutOfBoundsException.))))
@ichramm
ichramm / gist:e46440e3bdfe93a0859f67d9a766f046
Created April 28, 2020 13:31 — forked from cemerick/gist:3750288
Extending defrecord types in Clojure
;; Records are just types that provide default implementations of certain
;; key interfaces that allow them to stand in for maps.
;; This set of interfaces/protocols is not closed though; you can certainly make them
;; useful in places where maps aren't, e.g. w/ sequential destructuring:
=> (defrecord Point [x y]
clojure.lang.Indexed
(nth [_ i] (case i 0 x 1 y
(throw (IndexOutOfBoundsException.))))
(nth [_ i default]
@ichramm
ichramm / errors.clj
Created April 22, 2020 20:45 — forked from adambard/errors.clj
An example of functional error handling in clojure.
(ns example.errors)
(defn clean-address [params]
"Ensure (params :address) is present"
(if (empty? (params :address))
[nil "Please enter your address"]
[params nil]))
(defn clean-email [params]
"Ensure (params :email) matches *@*.*"
@ichramm
ichramm / start-datomic-transactor.sh
Created February 4, 2020 13:22
start datomic transactor
#!/bin/bash
$DATOMIC_DIR/bin/transactor \
./resources/db/sql-transactor.properties &
sleep 1
$DATOMIC_DIR/bin/console -p 8082 \
docker "datomic:sql://?jdbc:postgresql://localhost:5432/DB_NAME?user=datomic&password=datomic" &
@ichramm
ichramm / error-classifer.clj
Last active December 27, 2019 17:28
error classifier
(ns error-classifier
(:require [clojure.test :refer :all]))
(def known-errors ["Anonymous access not allowed"
"Invalid trace, not on the current datasource"
"sync placeholders were requested with a limit of"
"constraint \"company_campaign_dismisses_pkey\""
"constraint \"company_placeholder_customers_reached_pkey"
"constraint \"company_campaign_banner_customers_reached_pkey\""
"constraint \"company_channel_customers_reached_pkey\""
@ichramm
ichramm / stream-with-more.clj
Last active November 21, 2019 20:09
stream paginated
;;;;
;; Client
;;;;
(defn stream-seq-response-with-more!
"Returns a lazy sequence that wraps the streaming of data from the server"
[conn results have-more?]
(lazy-seq
(if (empty? results)