Skip to content

Instantly share code, notes, and snippets.

View danownsthisspace's full-sized avatar

Daniel Amber danownsthisspace

View GitHub Profile
@danownsthisspace
danownsthisspace / clojure-look-up-map.clj
Created October 3, 2021 18:26
Create a look up map with Clojure for faster record access
(def users [{:id 1
:email "[email protected]"
:first_name "Michael"
:last_name "Lawson"
:avatar "https://reqres.in/img/faces/1-image.jpg"}
{:id 2
:email "[email protected]"
:first_name "Lindsay"
:last_name "Ferguson"
:avatar "https://reqres.in/img/faces/2-image.jpg"}
(ns otca.shorts)
(defn wait-return [s r]
(Thread/sleep (* s 1000))
r)
#_(defn time-taken [expr]
(let [start (. System (nanoTime))
(ns scratch.core)
;;
;; x "once"
;; debouncer / rate limiter
;; namespaces
;; multiple windows
;; invalidation
;; peek
@danownsthisspace
danownsthisspace / stratch.clj
Created November 21, 2021 15:05
A function wrapper that ensures that a function can only be called once
(ns scratch.core)
(defn say-hi [username]
(let [s (str "hello, " username)]
(Thread/sleep (rand 100))
(println s)
s))
(comment (say-hi "Steve"))
@danownsthisspace
danownsthisspace / rate-limit-start.clj
Created December 4, 2021 13:29
Start of rate limit video. there is a bug where the wrap-once-in-a-while function is not thread safe
(ns scratch.core)
(defn say-hi [username]
(let [s (str "hello, " username)]
(Thread/sleep (rand 100))
(println s)
s))
(defn wrap-once-in-a-while [msecs-period f]
@danownsthisspace
danownsthisspace / advent-day1-clojure.clj
Last active December 21, 2021 08:24
Advent-of-code day1 2021
(ns scratch
(:require [clojure.string :as str]))
;; (def input [1, 2, 3, 4, 5, 7, 1, 9, 10])
(def input (map #(Integer/parseInt %) (str/split-lines (slurp "input-data.txt"))))
(take 3 input)
(def combined-input (loop [input-nums input
values-to-check []]
@danownsthisspace
danownsthisspace / first-attempt.clj
Created January 24, 2022 18:33
PRIME SIEVE ALGORITHM ( SIEVE OF ERATOSTHENES )
(defn filter-multiple [n v]
(if (= 1 n) v
(filter (fn [x]
(or
(= x n)
(not= 0 (mod x n)))) v)))
(defn sieve [input]
(let [limit (Math/sqrt input)
numbers (range 2 input)]