Skip to content

Instantly share code, notes, and snippets.

View dexterous's full-sized avatar

Saager Mhatre dexterous

View GitHub Profile
@dexterous
dexterous / main.clj
Created June 4, 2025 09:44
Prime Factorization in Clojure
(defn factors! [n]
(when-not (integer? n)
(throw (IllegalArgumentException.
(format "`%s` is not a natural number." (pr-str n)))))
(loop [n n, c (biginteger 2), fs (transient [])]
(cond
(neg? n) (recur (- n) c (conj! fs -1))
(> c n) (seq (persistent! fs)) ; XXX: only #{0, 1}
(> c (Math/sqrt n)) (seq (persistent! (conj! fs n)))
(zero? (rem n c)) (recur (/ n c) c (conj! fs c))
@dexterous
dexterous / Employee.java
Created June 3, 2025 21:11
Tap pattern in Java using Functional Interfaces
import java.util.Collections;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
class Employee {
private Set<Org> orgs;
private Org primaryOrg;
@dexterous
dexterous / lambda-combinators.js
Last active May 29, 2025 11:28
Lambda Combinators in JS
// https://en.wikipedia.org/wiki/Combinatory_logic#Examples_of_combinators
const I = a => a, // identity
K = a => _ => a, // constantly
V = a => b => c => c(a)(b), // cons
B = a => b => c => a(b(c)), // compose
S = x => y => z => x(z)(y(z)); // apply x to y inside env z
// alternative implementation of identity
const _I = S(K)(K)
@dexterous
dexterous / main.go
Created May 27, 2025 10:33
Go provider-consumer as funcs
package main
import (
"context"
"log"
"runtime"
"sync"
"time"
)
;; From https://github.com/ring-clojure/ring/blob/a915a44/ring-core/src/ring/middleware/session/cookie.clj#L29
(defn- hmac
"Generates a Base64 HMAC with the supplied key on a string of data."
[key data]
(let [mac (Mac/getInstance hmac-algorithm)]
(.init mac (SecretKeySpec. key hmac-algorithm))
(codec/base64-encode (.doFinal mac data))))
;; I would prefer to write it like this.
(defn- hmac
# CLOSURES IN RUBY Paul Cantrell https://innig.net
# Email: username "paul", domain name "innig.net"
# I recommend executing this file, then reading it alongside its output.
#
# Alteratively, you can give yourself an unreasonable Ruby test by deleting all
# the comments, then trying to guess the output of the code!
#
# (Naive HR departments, please do not use that idea as a hiring quiz.)

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@dexterous
dexterous / rotate.clj
Created August 31, 2022 06:21
Rotate a vector in Clojure
(defn rotate [v n]
(cond
(zero? n) v
(pos? n) (vec (concat (nthrest v n) (take n v)))
(neg? n) (rotate v (+ (count v) n))))
(defn how-not-to-factorial [x]
(condp #(< %2 %1) x
1 (throw (IllegalArgumentException.))
3 x
(loop [acc (bigint x) prev (dec x)]
(if (= prev 2) (* acc 2) (recur (* acc prev) (dec prev))))))
;; when you want to factorial...
(defn just-factorial [n] (apply * (range 1N (inc n))))
@dexterous
dexterous / devops-maturity.md
Last active June 4, 2021 04:41
Article text from "Is DevOps Falling into the Maturity Trap?"

@DevOpsSummit #CloudNative #Serverless #DevOps

BY JASON BLOOMBERG

APRIL 29, 2018 07:00 PM EDT

Guest post by Intellyx Principal Analyst Charles Araujo

I was at a conference recently when I saw it. I sighed and shook my head.