Skip to content

Instantly share code, notes, and snippets.

View joshuakfarrar's full-sized avatar
💻
Purely Functional

Joshua K. Farrar joshuakfarrar

💻
Purely Functional
View GitHub Profile
@joshuakfarrar
joshuakfarrar / argon2.clj
Last active September 30, 2023 03:17
implementing Bouncy Castle's argon2 in clojure, from the Leiningen repl
(import org.bouncycastle.crypto.generators.Argon2BytesGenerator)
(import org.bouncycastle.crypto.params.Argon2Parameters)
(import org.bouncycastle.crypto.params.Argon2Parameters$Builder)
(import org.bouncycastle.util.encoders.Hex)
(defn argon2-hash
[version iterations memory parallelism password salt output-length]
(let [builder (doto (Argon2Parameters$Builder. Argon2Parameters/ARGON2_i)
(.withVersion version)
(.withIterations iterations)
(ns key-generator.core
(:import (java.security KeyPairGenerator Key)
(java.util Base64)))
(def ^:private algorithm->key-type
{:RS256 "RSA"
:ES256 "EC"})
(def ^:private algorithm->key-size
{:RS256 512
import numpy as np
# Input image: 5x5 grayscale image
image = np.array([[10, 20, 30, 40, 50],
[60, 70, 80, 90, 100],
[110, 120, 130, 140, 150],
[160, 170, 180, 190, 200],
[210, 220, 230, 240, 250]])
# Filter or Kernel: 3x3
@joshuakfarrar
joshuakfarrar / Main.scala
Created March 20, 2023 20:38
An example of CommutativeMonad and tailRecM for "fold but with early termination"
import cats._
import cats.effect.{IO, IOApp, ExitCode}
import cats.syntax.all._
object Main extends IOApp.Simple {
case class CustomState[A](value: A, remaining: Int)
implicit val customStateMonad: CommutativeMonad[CustomState] = new CommutativeMonad[CustomState] {
def pure[A](a: A): CustomState[A] = CustomState(a, 0)
@joshuakfarrar
joshuakfarrar / Main.scala
Last active March 20, 2023 20:37
A calculation DSL using the Free monad.
import cats._
import cats.effect.{IO, IOApp}
import cats.free.Free
sealed trait Calculation[A]
case class Add(value: Int) extends Calculation[Unit]
case class Subtract(value: Int) extends Calculation[Unit]
case class Multiply(value: Int) extends Calculation[Unit]
case class Divide(value: Int) extends Calculation[Unit]
case object Clear extends Calculation[Unit]
// source: https://stackoverflow.com/questions/71608818/purely-functional-immutable-doubly-linked-list-in-javascript
var head = (function CircularList(previous, item, ...items) {
if (!items.length) return { item, next: () => head, previous };
var rest = CircularList(() => current, ...items); // Recursion
var current = { ...rest, previous };
return { ...rest, item, next: () => current };
})(() => head, 1, 2, 3, 4);
@joshuakfarrar
joshuakfarrar / carousel.js
Last active March 20, 2022 19:41
purely functional carousel for JavaScript Arrays
@joshuakfarrar
joshuakfarrar / _updater.js
Last active March 15, 2022 09:28
CyTube metadata updater, processing external datasets into custom JSON is the tricky part!
'use strict';
const _ = require('lodash');
const Promise = require('bluebird');
const data = require('./format.json')
const opts = require('./opts');
const config = require('yaml').parse(require('fs').readFileSync('config.yaml', 'utf-8'));
const knex = require('knex')({
state change_funding change_tuition net_hike unaccountable_hike
Colorado -510 4425 4935 3915
California 250 3983 3733 3733
Virginia -1218 4587 5805 3369
New Hampshire -1342 4686 6028 3344
Hawaii 1532 4716 3184 3184
Vermont -755 3711 4466 2956
Rhode Island -881 3756 4637 2875
Oregon -693 3334 4027 2641
Massachusetts -1117 3524 4641 2407
@joshuakfarrar
joshuakfarrar / compress.scala
Last active March 2, 2022 20:08
another code golf-type interview question
// compress a string by counting its repeating characters
def compress(a: String): String = a.foldLeft("") { (acc, l) =>
acc.lastOption match {
case Some(c) =>
if (c == l) => {
acc.slice(acc.lastIndexOf(l) - 1, acc.lastIndexOf(l)).headOption match {
case Some(d) => c match {
case l => if (Character.isDigit(d)) {
s"${acc.slice(0, acc.lastIndexOf(l) - 1)}${Character.digit(d, 10) + 1}" :+ l // add 1 to repeating characters we have seen
} else {