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
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) });
// RegistrationFormValidator.validateForm(form) returns Either[ValidationError, ValidRegistration]
// the expected return type of our HttpService[F] creator is F[Response[F]], so here we use .bimap
// and friends to finagle our validated form into that type.
val service: HttpService[F] = {
HttpService[F] {
case req @ POST -> Root / "users" =>
req.decode[RegistrationForm] { form =>
RegistrationFormValidator.validateForm(form).bimap(error => registrationFailed(error),
user => insertRegistration(User(UUID.randomUUID, user.email, user.password.bcrypt))
// given a List[Int], e.g. List(1, 50, 250, 250, 50, 6), find all the pairs of numbers which sum to 500
def pairsOfSumOfN(ns: List[Int], n: Int): List[(Int, Int)] =
ns.zipWithIndex.map(pair => ns.zipWithIndex.filter(_._2 != pair._2).map(_._1).zipWithIndex.map(second => (pair._1, second._1))).flatMap(a => a.filter(b => n == (b._1 + b._2)))
pairsOfSumOfN(List(1, 50, 250, 250, 50, 6), 500) // returns List((250, 250), (250, 250))! (can we dedupe this list by using indexes? you bet!)
import cats.effect.IO
import cats.Monad
import api.AirlinesService
import api.ProfilesService
import api.TripService
/*
* ok, let's say we have our services, but instead of Promises, this is Scala,
* so our services return IOs of responses as such:
@joshuakfarrar
joshuakfarrar / example.scala
Last active December 14, 2018 18:57
cats.effect._, HKTs, Kleisli, for comprehensions, and `implicit F: Monad[F]`; ADT promotion and more!
package me.omega
import cats.effect.{IOApp, IO, ExitCode}
import cats.Monad
import cats.data.Kleisli
import cats.syntax.all._
object Main extends IOApp {
override def run(args: List[String]): IO[ExitCode] = new App[IO].run.as(ExitCode.success) // `.as` is sugar for `.map[A](a: A)(_ => a)`
}
@joshuakfarrar
joshuakfarrar / build.sbt
Last active November 18, 2018 03:58
hola, kevin
name := "hola-kevin"
version := "0.1"
scalaVersion := "2.12.7"
scalacOptions += "-Ypartial-unification"
libraryDependencies += "org.typelevel" %% "cats-core" % "1.4.0"
libraryDependencies += "org.typelevel" %% "cats-effect" % "1.0.0"
@joshuakfarrar
joshuakfarrar / introduction.scala
Last active October 25, 2018 18:46
from the introduction of functional programming for mortals with scalaz
package LetThereBeEcho
trait Terminal[C[_]] {
def read: C[String]
def write(t: String): C[Unit]
}
trait Execution[C[_]] {
def doAndThen[A, B](c: C[A])(f: A => C[B]): C[B]
def create[B](b: B): C[B]
val bucketConfig = new BucketNotificationConfiguration()
bucketConfig.setConfigurations(existingConfig)
s3.setBucketNotificationConfiguration(
new SetBucketNotificationConfigurationRequest(config.bucket, bucketConfig)
)
@joshuakfarrar
joshuakfarrar / main.hs
Last active August 20, 2018 18:23
Using ExceptT to catch errors in Haskell.
{-# LANGUAGE NoImplicitPrelude #-}
module Main where
import Protolude
import qualified Data.Text as T
calculateThing :: ExceptT Text IO Text
calculateThing = do
print $ T.pack "log"