Skip to content

Instantly share code, notes, and snippets.

View fogfish's full-sized avatar

fogfish

View GitHub Profile
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 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
@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 / 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": {...}}
*/