Skip to content

Instantly share code, notes, and snippets.

View fogfish's full-sized avatar

fogfish

View GitHub Profile
@fogfish
fogfish / HListCodec.scala
Created May 4, 2018 06:09
Scala shapeless HList to Json with io.circe
import io.circe.{Encoder, JsonObject, ObjectEncoder}
import shapeless.{::, HList, HNil}
import scala.reflect.ClassTag
/**
* val product = A(...) :: B(...) :: C(...) :: HNil
* product.asJson
*
* {"a": {...}, "b": {...}, "c": {...}}
*/
@fogfish
fogfish / HListMap.scala
Created May 4, 2018 10:44
Use Poly1 to Map HList
import shapeless._
object size extends Poly1 {
implicit def default[T] = at[T](t => (t, 1))
implicit def caseInt = at[Int](x => (x, x))
implicit def caseString = at[String](s => (s, s.length))
}
@fogfish
fogfish / aws-cdk-pure.ts
Last active March 8, 2020 06:31
defines pure functions to solve embarrassingly obvious composition problem in AWS CDK
//
// Copyright (C) 2019 Dmitry Kolesnikov
//
// This pure.ts file may be modified and distributed under the terms
// of the MIT license.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
class Text {
static f(str: TemplateStringsArray, ...args: Array<any>) {
return <A>(...props: Array<A>): string => {
const parsed = args.map((x, index) => (typeof x === 'function' ? x(props[index]) : x))
if (parsed.some(x => x === undefined)) {
return ''
}
return str.reduce((acc, substr, index) => acc + this.text(parsed[index - 1]) + substr)
}
/*
go test -bench=. -test.benchmem=true -test.benchtime=10s
goos: darwin
goarch: amd64
BenchmarkDirectCall-12 1000000000 1.31 ns/op 0 B/op 0 allocs/op
BenchmarkInterface-12 1000000000 1.69 ns/op 0 B/op 0 allocs/op
BenchmarkHoF-12 1000000000 1.61 ns/op 0 B/op 0 allocs/op
@fogfish
fogfish / bomb.ts
Created March 9, 2021 17:40
AWS CDK Bomb 💣
import * as cdk from '@aws-cdk/core'
class MyResource extends cdk.Construct {
constructor(scope: cdk.Construct, id: string, l: number) {
super(scope, id)
for (const x of Array(10).keys()) {
(l === 0)
? new cdk.CfnJson(this, `${id}${x}`, {value: '0123456789abcdef'})
: new MyResource(this, `${id}${x}`, l - 1)
}
/*
Eq : T ⟼ T ⟼ bool
Each trait implements mapping pair of value to bool category using own
equality rules
*/
type Eq [T any] interface {
Equal(T, T) bool
}
package eq
/*
eqInt declares a new instance of Eq trait, which is a real type.
The real type "knows" everything about equality in its own domain.
The instance of Eq is created as type over string, it is an intentional
technique to create a namespace using Golang constants.
The instance of trait is referenced as eq.Int in the code.
*/
type Haystack[T any] struct{ Eq[T] }
func (h Haystack[T]) Lookup(a T, b []T) bool {
for _, x := range b {
if h.Eq.Equal(a, x) {
return true
}
}
return false
}
package ord
/*
Ord : T ⟼ T ⟼ Ordering
Each type implements compare rules, mapping pair of value to enum{ LT, EQ, GT }
*/
type Ord [T any] interface {
Eq[T]