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 / Deck.scala
Created January 20, 2022 18:15
using the StateT Monad, we are able to interleave IO into our recursive simulation of State transitions
import scala.util.Random
import cats.data.State
object Deck {
sealed trait Rank
case object Two extends Rank
case object Three extends Rank
case object Four extends Rank
case object Five extends Rank
@joshuakfarrar
joshuakfarrar / form.php
Last active March 15, 2022 09:30
para vladimiro.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection

Ennis and Jack, the dogs, the horses and mules, a thousand ewes and their lambs flowed up the trail like dirty water through the timber and out above the tree line into the great flowery meadows and the coursing, endless wind.

@joshuakfarrar
joshuakfarrar / winkey-face.py
Last active March 15, 2022 09:30
para abril, on encoding
import functools
# our goal is to use the format feature of strings to render an array of integers, our encoded message, as a string
## remember character encodings?
# let's load up a fake one
# not a real one, not ascii; a fakeskii
encoding_file = open('zyx-fakeskii', 'r')
# nnfs.io
import numpy as np
# a single array of inputs, for a layer of 3 neurons
inputs = [1, 2, 3, 2.5]
weights = [[0.2, 0.8, -0.5, 1], [0.5, -0.91, 0.26, -0.5], [-0.26, -0.27, 0.17, 0.87]] # 3 neurons
biases = [2, 3, 0.5] # and their bias
const http = require('./http');
const API_URL = 'http://localhost:3001';
const API_CURRENT_VERSION = 'v0';
const CURRENT_API_URL = `${API_URL}/api/${API_CURRENT_VERSION}`;
const API = {
entities: {
all: () => http.get({
url: `${CURRENT_API_URL}/entities`
// three ways of expressing registration and sending confirmation e-mail in scala
(R.register _)
.andThen(_
.map(C.withConfirmable)
.map(_ >>= { confirmable => OptionT.liftF(C.sendConfirmationInstructions(confirmable)) })
)
.andThen(_ >>= { _ => Ok() })
.apply(form)
@joshuakfarrar
joshuakfarrar / getConfirmableFromToken.scala
Last active February 19, 2021 19:04
use monad transformers for great elegance!
import cats.data.OptionT
import cats.implicits._
/*
* in my domain, the confirmation algebra provides Confirmable,
* which is parameterized over the type of id of the thing, probably
* a user, to be confirmed, and the type of the confirmation token
*/
type C = Confirmable[UUID, SecureRandomId]
const _ = require('lodash');
const tags = (initialValue) => {
if ((typeof initialValue !== 'object' || Array.isArray(initialObject) === true)
|| (typeof initialValue !== 'undefined')) throw new Error('initial value must be undefined or an object');
const _tags = initialValue || {};
return {
addTag: (name, value) => {
if (typeof value === 'string' && value.length > 0) return tags(_.set(_tags, name, value));
function calc(users) {
return _.reduce(_.map(users, (user) => {
return _.reduce(_.map(user.puzzles, (puzzle) => {
return {
puzzleId: puzzle.puzzleId,
errors: _.reduce(puzzle.moves, (acc, move) => {
if (move.isValid) return acc;
else {
var fromState = _.get(move, 'fromState');
return _.merge({}, acc, { [fromState]: _.concat((_.get(acc, fromState) || []), move.toState) });