Skip to content

Instantly share code, notes, and snippets.

View joshuakfarrar's full-sized avatar
💻
EitherT[IO, Throwable, Human]

Joshua K. Farrar joshuakfarrar

💻
EitherT[IO, Throwable, Human]
View GitHub Profile
// h/t https://www.asciiart.eu/text-to-ascii-art Fraktur
private val banner = Seq(
"""
| .. .. ..
| :**888H: `: .xH"" x .d88" x .d88"
| X `8888k XX888 .d`` u. 5888R 5888R u.
|'8hx 48888 ?8888 @8Ne. .u ...ue888b '888R '888R ...ue888b
|'8888 '8888 `8888 %8888:u@88N 888R Y888r 888R 888R 888R Y888r
| %888>'8888 8888 `888I 888. 888R I888> 888R 888R 888R I888>
// this is our possible base64-encoded byte array — serialized — flash cookie
val flashCookie: Option[String] = request.headers
.get[Cookie]
.flatMap(_.values.find(_.name == "flash"))
.map(_.content)
// aquí tienes, voilà
flashCookie.map(_.decode).sequence.fold(
_ => BadRequest("failed to deserialize cookie, this is very bad"),
cookie => Ok(renderPage(cookie)) // or do whatever we want with our freshly baked Option[Map[String, String]]
@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')({