Skip to content

Instantly share code, notes, and snippets.

View psilospore's full-sized avatar
🍄

psilospore psilospore

🍄
View GitHub Profile
@psilospore
psilospore / readert_convo.txt
Created June 9, 2022 18:45
Conversation on the ReaderT design pattern
Alright I was trying to find that article but it has way more details then I remember. Here’s what we basically do:
We have a App Monad or “Context”
newtype App a = App {unApp :: ReaderT Environment IO a}
So you can see the IO thing in there but you’ll notice there’s something called ReaderT and Environment. I’ll get to those.
Now when you mess around with Haskell you might use functions in IO e.g.
foo :: IO ()
@psilospore
psilospore / getProperties.hs
Created January 19, 2022 03:36
Get list of property names from a Haskell Record
{-# LANGUAGE DeriveDataTypeable #-}
import qualified Data.Data as D
import Data.Data (Data)
data Gurren = Gurren {
owner :: String
, spiralPower :: Natural
} deriving (Data)
let properties = D.constrFields . head . D.dataTypeConstrs $ D.dataTypeOf (undefined :: Gurren)
@psilospore
psilospore / typed-mock.ts
Created February 4, 2021 15:55
Generate semi-realistic mock data from an io-ts codec
import { default as faker } from 'faker'
import { fold, none, Option, some } from 'fp-ts/lib/Option'
import { pipe } from 'fp-ts/lib/pipeable'
import { keys } from 'fp-ts/lib/Record'
import { evalState, State } from 'fp-ts/lib/State'
import * as t from 'io-ts'
import { NumberFromString } from 'io-ts-types/lib/NumberFromString'
import { Moment } from 'moment'
// TODO don't use lodash
@psilospore
psilospore / firestoreIoTs.ts
Last active August 29, 2020 23:41
Using io-ts with Firestore's timestamp data type
import {Firestore} from "@google-cloud/firestore"
import { firestore as firestoreAdmin } from 'firebase-admin'
import {foldMap} from 'fp-ts/lib/Array'
import {pipe} from 'fp-ts/lib/pipeable'
import {monoidSum} from 'fp-ts/lib/Monoid'
import {flatMap, some as exists} from 'lodash'
import * as io from 'io-ts'
import { Timestamp } from '@google-cloud/firestore'
object ParEitherNonEmpty {
import cats.data.NonEmptyList.catsDataSemigroupForNonEmptyList
import cats.data.{NonEmptyList, Validated}
new Parallel[Either[NonEmptyList[_], ?]] {
override type F[v] = Validated[NonEmptyList[_], v]
override def applicative: Applicative[Validated[NonEmptyList[_], *]] = {
Validated.catsDataApplicativeErrorForValidated[NonEmptyList[_]](catsDataSemigroupForNonEmptyList)
}
@psilospore
psilospore / trie.scala
Last active March 9, 2019 04:59
Bad Scala Trie
import scala.collection.mutable
class TrieNode(var children: mutable.Map[Char, TrieNode] = mutable.Map()) {
// Inserts into current Trie
def insert(c: Char): TrieNode = {
children.get(c).getOrElse({
children.put(c, new TrieNode())
this
}
)
@psilospore
psilospore / total.sc
Created February 9, 2019 17:33
Find how much I've given someone on venmo Ammonite
val s = %%pbpaste
s.out.string.split("\n").filter(_.contains("$")).map(_.split("\\$")(1)).map(_.toFloat).sum
@psilospore
psilospore / lookerUpper.scala
Created December 21, 2018 16:23
Look up usages of scala method using macros
import scala.language.experimental.macros
//WIP does not function
final class lookerUpper extends scala.annotation.StaticAnnotation {
import lookerUpper._
/**
* @param annottees list of untyped annottees
* @return
*/
@psilospore
psilospore / user.sbt
Created December 4, 2018 03:10
user.sbt as a scala contributor
addCommandAlias("partestDebugOn", """set javaOptions in test in IntegrationTest += "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5008" """)
addCommandAlias("partestDebugOff", """set javaOptions in test in IntegrationTest -= "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5008" """)
addCommandAlias("replDebugOn_212", """set javaOptions in run in replJlineEmbedded += "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5008" """)
addCommandAlias("replDebugOff_212", """set javaOptions in run in replJlineEmbedded -= "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5008" """)
addCommandAlias("replDebugOn_213", """set javaOptions in run in replFrontend += "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5008" """)
addCommandAlias("replDebugOff_213", """set javaOptions in run in replFrontend -= "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5008" """)
@psilospore
psilospore / bagotricks-scala.md
Last active September 25, 2018 19:40
Scala and IntelliJ Bag'o'Tricks

Bag O Tricks

Scala

Set up build.sbt with the following:

//TODO add cats
scalaVersion := "2.12.4"

libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.2.18"