Skip to content

Instantly share code, notes, and snippets.

@alpox
alpox / c20.clj
Last active April 26, 2024 16:12
(ns c20
(:require
[next.jdbc :as jdbc]
[honey.sql :as sql]
[tick.core :as t]
[next.jdbc.date-time]
[clojure.test :refer [is use-fixtures deftest]]))
(def db-spec
{:dbtype "postgres"
@alpox
alpox / c13.clj
Last active April 3, 2024 21:47
FCC Challenge 3. April 2024
(ns c13
(:require
[clojure.string :as str]
[clojure.spec.alpha :as s]
[clojure.test :refer [testing is]]
[clojure.test.check :as check]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop]
[com.gfredericks.test.chuck.generators :as gen']))
@alpox
alpox / c11.clj
Last active April 2, 2024 22:52
FCC Challenge - Arithmetic expression calculator
(ns c11
(:require
[clojure.spec.alpha :as s]
[clojure.walk :as walk]
[clojure.math :as math]
[clojure.test :as t]))
(def ops #{\* \/ \+ \- \^})
(defn coerce [token]
@alpox
alpox / challenge_6.clj
Last active March 16, 2024 13:34
FCC 15. March 2024 Challenge
(ns c6
(:require
[camel-snake-kebab.core :as csk]
[clojure.data.json :as json]
[clojure.string :as str]
[clojure.test :refer [are is testing]]
[babashka.fs :as fs]
[babashka.http-client :as http]
[malli.core :as m]
[malli.dev.pretty :as pretty]
#!/usr/bin/env bb
(ns c2
(:require [babashka.fs :as fs]))
(def start (first *command-line-args*))
(defn line-prefix [levels]
(apply str
(for [[global-level written] (map-indexed vector levels)
:let [global-level (inc global-level)
@alpox
alpox / windowedFetch.js
Last active June 26, 2022 11:02
Windowed fetch with slots
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
const createWindowedFetch = (windowSize = 1000, reqPerWindow = 30) => {
const slots = [0];
const start = new Date().getTime();
let lastSlot = start;
return async (url, requestInit) => {
let firstFreeSlotIndex = slots.findIndex(
(allocated) => allocated < reqPerWindow
@alpox
alpox / pipeline-gist-immutable.js
Last active June 12, 2022 11:44
Immutable Linked List with Pipelining
function forEachInList(list, fn) {
if (!list) return null;
let current = list;
let i = 0;
while(true) {
fn(current.data, i, current, list);
if (!current.next) break;
current = current.next;
i++;
}
@alpox
alpox / pipeline-gist.js
Last active June 12, 2022 10:51
Linked list with Pipelineing
function forEachInList(list, fn) {
let current = list;
let i = 0;
while(true) {
fn(current.data, i, current.node, list);
if (!current.next) break;
current = current.next;
i++;
}
}
@alpox
alpox / example.clj
Last active November 21, 2023 20:29
Clojure -> Python Async
(defn sleep [s]
(-> (asyncio/sleep s)
(pp/then (fn [_] s))))
(defn fun []
(pp/let [x (sleep 1)
z (pp/resolved 5)
y (sleep 2)]
(+ x y z)))
@alpox
alpox / asyncAwait.js
Created February 27, 2022 21:42
Async await
const asyncFun = fun => (...args) => {
const gen = fun(...args);
return drain(gen);
}
function ensurePromise(maybePromise) {
return maybePromise.then
? maybePromise
: Promise.resolve(maybePromise);