Skip to content

Instantly share code, notes, and snippets.

View Daenyth's full-sized avatar

Alanna Stone Daenyth

View GitHub Profile
@Daenyth
Daenyth / ImmutableLRU.scala
Created March 12, 2019 17:36
LruRef / LRU for cats-effect
package teikametrics
import scala.collection.SortedMap
/**
* Immutable implementation of an LRU cache.
*
* @author Twitter
*
* Copy pasted from the version previously in twitter-util v19.1.0 at
@Daenyth
Daenyth / TestFailExample.scala
Created December 20, 2018 14:22
Save scalacheck generated data as json on failure in scalatest
class TestFailExample extends FlatSpec with GeneratorDriverPropertyChecks
it should "foo" in {
val genData: Gen[T]
IO(
forAll(genData) {
// test here
}
).onError {
case e: GeneratorDrivenPropertyCheckFailedException =>
val data = e.args.head
@Daenyth
Daenyth / StreamPureImpure.md
Last active December 18, 2018 14:11
fs2 Stream / pure stream implemented with impurity

Fold a String stream using StringBuilder instead of Monoid[String], because the monoid performs repeated concatenation.

@ def foldString[F[_]](src: Stream[F, String]): Stream[F, String] = src.fold(new StringBuilder)(_ append _).map(_.result)
defined function foldString

@ val s = foldString(Stream("Hello", " World!"))
s: Stream[Pure, String] = Stream(..)

@ s.toList
@Daenyth
Daenyth / StreamRetry.scala
Last active December 11, 2022 21:31
fs2 Stream 1.x / Simple example of retry with logging
import cats.effect.Sync
import io.chrisdavenport.log4cats.Logger
import cats._
import cats.implicits._
import scala.concurrent.duration._
class RetryExample[F[_]](implicit F: Sync[F], log: Logger[F]) {
case class ApiError(msg: String) extends Exception(msg)
private def getTempFromInternet: EitherT[F, ApiError, Float] = ???
This file has been truncated, but you can view the full file.
[12:40:05] [main/INFO] [LaunchWrapper]: Loading tweak class name net.minecraftforge.fml.common.launcher.FMLTweaker
[12:40:05] [main/INFO] [LaunchWrapper]: Using primary tweak class name net.minecraftforge.fml.common.launcher.FMLTweaker
[12:40:05] [main/INFO] [LaunchWrapper]: Calling tweak class net.minecraftforge.fml.common.launcher.FMLTweaker
[12:40:05] [main/INFO] [FML]: Forge Mod Loader version 14.23.5.2768 for Minecraft 1.12.2 loading
[12:40:05] [main/INFO] [FML]: Java is Java HotSpot(TM) 64-Bit Server VM, version 1.8.0_152, running on Mac OS X:x86_64:10.13, installed at /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home/jre
[12:40:05] [main/INFO] [FML]: Searching /Applications/MultiMC.app/Contents/MacOS/instances/Weirdpack-/.minecraft/mods for mods
[12:40:05] [main/WARN] [FML]: Found FMLCorePluginContainsFMLMod marker in [1.12]+VersionChecker-1.3.0.jar. This is not recommended, @Mods should be in a separate jar from the coremod.
[12:40:05] [main/WARN] [FML]: The coremod com.dynious.versionc
@Daenyth
Daenyth / Retriable.scala
Created November 2, 2018 13:17
RetriableT.scala
package teikametrics.sync
import scala.concurrent.{ExecutionContext, Future}
sealed trait Retriable[+T] {
def toOption: Option[T]
}
case class Ready[T](result: T) extends Retriable[T] {
override val toOption = Some(result)
@Daenyth
Daenyth / date_intervals.py
Last active October 11, 2018 17:19
Python date range interval storage, merging, compaction
# coding: utf-8
"""
Provides logic for tracking known and missing date ranges.
It can be used to track data acquisition from an external API.
It allows you to say: "I need data from D1-D2, and I have these chunks: [Dn..Dm]; what date ranges am I missing?"
Data can be tracked per item, for example:
ScheduleKey("GA", "credential_id", "http://google.com") or
ScheduleKey("Marketo", "instance_id", "activities")
@Daenyth
Daenyth / json_namedtuple.py
Last active February 3, 2020 19:33
Parse json via python NamedTuple
import hashlib
import json
from decimal import Decimal
from typing import Any, Dict, Type
from typing_inspect import get_args, get_generic_bases, is_generic_type, is_union_type # type: ignore
Json = Dict[str, Any]
@Daenyth
Daenyth / AwsSigner.scala
Last active April 18, 2019 05:11
Draft http4s middleware for AWS request signing
// Based on https://github.com/http4s/contrib/blob/master/aws/src/main/scala/org/http4s/contrib/aws/AwsSigner.scala
import java.util.Date
import cats.data.Kleisli
import cats.effect.{Effect, Sync}
import cats.implicits._
import fs2.Stream
import org.http4s.client.Client
import org.http4s.{Header, Request}
@Daenyth
Daenyth / Pull.md
Last active December 8, 2024 00:27
Designing an fs2 `Pull` from scratch

The problem

I have some data which has adjacent entries that I want to group together and perform actions on. I know roughly that fs2.Pull can be used to "step" through a stream and do more complicated logic than the built in combinators allow. I don't know how to write one though!

In the end we should have something like

def combineAdjacent[F[_], A](
 shouldCombine: (A, A) => Boolean,