Skip to content

Instantly share code, notes, and snippets.

@ndwade
ndwade / Money.scala
Created June 23, 2022 21:47
type level domain model of Money
package contracts
import _root_.algebra.CommutativeGroup
import _root_.algebra.ring.Signed
import _root_.algebra.ring.Field
import spire.algebra.VectorSpace
import cats.syntax.group.*
import spire.syntax.vectorSpace.*
package wut
import scala.language.higherKinds
import cats.{ Functor }
object FreeShit {
/**
*/
case class Done[F[_]: Functor, A](a: A) extends Free[F, A]
@ndwade
ndwade / export_google_music.js
Created October 31, 2016 09:25 — forked from jmiserez/export_google_music.js
(fixed/updated 2016-05-10) Export your Google Music Library and Playlists (Google Play Music All Access) (see http://webapps.stackexchange.com/questions/50311/print-playlist-from-google-play-music for more)
// Jeremie Miserez <jeremie@miserez.org>, 2016
//
// A little bit of Javascript to let you export your Google Music library, playlists, and album track lists :)
//
// I posted this as an answer here: http://webapps.stackexchange.com/questions/50311/print-playlist-from-google-play-music
//
// 1. Go to: https://play.google.com/music/listen#/all (or your playlist)
//
// 2. Open a developer console (F12 for Chrome). Paste
// code below into the console.
@ndwade
ndwade / profit-scan.scala
Last active September 2, 2016 06:07
profit scan
import scala.language.postfixOps
def maxProfitScan(prices: List[Int]): Int = {
require(prices.size >= 2)
final case class State(hi: Int, lo: Int, pl: Option[Int] = None) {
def profit: Int = hi - lo
}
/** res ipsa loquitur */
trait Module {
trait Repo {
type T
final def save(t: T): String = s"saved $t"
}
abstract class ActiveRepo[R <: Repo](val repo: R) {
@ndwade
ndwade / XmlToJson.scala
Created January 25, 2016 02:08
xml to json converter
object XmlToJson {
import scala.language.postfixOps
import scala.xml._
import upickle.{ Js, json }
def apply(nodes: NodeSeq, excludes: NodeSeq = NodeSeq.Empty): String = {
def attrsToTaggedJsVals(attrs: MetaData): List[(String, Js.Str)] = attrs.asAttrMap.toList map {
case (tag, value) => tag -> Js.Str(value)
import java.time.temporal.ChronoUnit
object timescratch {
import java.time._
val myTz = ZoneId.systemDefault //> myTz : java.time.ZoneId = America/Los_Angeles
val zTz = ZoneId of "Z" //> zTz : java.time.ZoneId = Z
def zDaySpan(dayStart: ZonedDateTime) = {
/*
* Copyright 2014-2016 Panavista Technologies, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@ndwade
ndwade / WaiterSpec.scala
Last active December 23, 2015 10:19
Suspecting a bug in org.scalatest.concurrent.AsynchronousAssertions.Waiter. The code below fails pretty reliably. There seems to be a window of vulnerability in awaitImpl(): if the last (or last few) dismiss() calls happen after the dismissals count test at the top of the while() loop, but the associated notifyAll() invocations happen before the…
package scratch
// scalatest 2.0.M8
import org.scalatest.WordSpec
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.concurrent.AsyncAssertions
import org.scalatest.time.{ Span, Millis }
class WaiterSpec extends WordSpec with ShouldMatchers with AsyncAssertions {
@ndwade
ndwade / kind.scala
Created December 16, 2012 10:08 — forked from eed3si9n/kind.scala
// requires Scala 2.10.0-M7
// updated for Scala 2.10-RC5
def kind[A: scala.reflect.runtime.universe.TypeTag]: String = {
import scala.reflect.runtime.universe._
def typeKind(sig: Type): String = sig match {
case PolyType(params, resultType) =>
(params map { p =>
typeKind(p.typeSignature) match {
case "*" => "*"
case s => "(" + s + ")"