Skip to content

Instantly share code, notes, and snippets.

Avatar

fogfish

View GitHub Profile
@fogfish
fogfish / bomb.ts
Created Mar 9, 2021
AWS CDK Bomb 💣
View bomb.ts
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)
}
View hof_test.go
/*
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
View text-query.ts
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)
}
@fogfish
fogfish / aws-cdk-pure.ts
Last active Mar 8, 2020
defines pure functions to solve embarrassingly obvious composition problem in AWS CDK
View aws-cdk-pure.ts
//
// 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
@fogfish
fogfish / HListMap.scala
Created May 4, 2018
Use Poly1 to Map HList
View HListMap.scala
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 / HListCodec.scala
Created May 4, 2018
Scala shapeless HList to Json with io.circe
View HListCodec.scala
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": {...}}
*/