This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {...}} | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
OlderNewer