Skip to content

Instantly share code, notes, and snippets.

@dcastro
dcastro / gist:9093000
Created February 19, 2014 14:17
Build an Expression that instantiates an Anonymous Type
public void CreateExpression()
{
Type argType = typeof (MyClass);
string propertyName = "Name";
ParameterExpression paramExpression = Expression.Parameter(argType, "item");
//Create "item.Name" and "item.GetType()" expressions
var propertyAccessExpression = Expression.Property(paramExpression, propertyName);
var getTypeExpression = Expression.Call(paramExpression, "GetType", Type.EmptyTypes);
@dcastro
dcastro / io.md
Last active May 27, 2022 17:27
IO vs Future and Referential transparency

The problem

These programs are safe to refactor:

object p1 {
  val x = 1 + 123
  val y = 1 + 123
}
@dcastro
dcastro / slack_edit_message.json
Created May 15, 2022 17:28
slack_edit_message.json
{
"envelope_id": "f34c6197-a7ef-45ed-acad-4d5a3147ec86",
"payload": {
"token": "Pct6Sm6JynybrFXkjEiH7hhX",
"team_id": "T02NDBHSWSG",
"api_app_id": "A03BLN935DK",
"event": {
"type": "message",
"subtype": "message_changed",
"message": {
@dcastro
dcastro / slack_message.json
Created May 15, 2022 17:19
slack_message.json
{
"envelope_id": "012327ef-4c84-41e3-a952-09af25782c17",
"payload": {
"token": "Pct6Sm6JynybrFXkjEiH7hhX",
"team_id": "T02NDBHSWSG",
"api_app_id": "A03BLN935DK",
"event": {
"client_msg_id": "0837851c-2d34-4d4d-804c-7aed753c3421",
"type": "message",
"text": "Hello world",
@dcastro
dcastro / hangzhou.log
Created March 5, 2022 15:49
hangzhou vs ithaca
>>>>0: https://hangzhou.testnet.tezos.serokell.team/version
>>>>1: https://hangzhou.testnet.tezos.serokell.team/chains/main/blocks/head/protocols
>>>>2: https://hangzhou.testnet.tezos.serokell.team/chains/main/blocks/head/context/contracts/tz1ZSedNyGuqibEYCPbuMS3yEhhLHfZEuLtA/counter
>>>>3: https://hangzhou.testnet.tezos.serokell.team/chains/main/blocks/head/context/contracts/tz1ZSedNyGuqibEYCPbuMS3yEhhLHfZEuLtA/manager_key
>>>>4: https://hangzhou.testnet.tezos.serokell.team/monitor/bootstrapped
>>>>5: https://hangzhou.testnet.tezos.serokell.team/chains/main/blocks/head/context/constants
>>>>6: https://hangzhou.testnet.tezos.serokell.team/chains/main/blocks/head/hash
>>>>7: https://hangzhou.testnet.tezos.serokell.team/chains/main/chain_id
>>>>8: https://hangzhou.testnet.tezos.serokell.team/chains/main/chain_id
>>>>9: https://hangzhou.testnet.tezos.serokell.team/chains/main/blocks/head/helpers/scripts/simulate_operation
@dcastro
dcastro / universal_existential.md
Last active February 6, 2022 07:55
Universally vs existentially quantified type variables

A type variable can be either universally ("for all x, then ...") or existentially quantified ("there exists some x, ...").

(If you're a maths persons, this corresponds to ∀x. x and ∃x. x, respectively)

In haskell, universally quantified type vars are represented with a forall:

head :: forall a. [a] -> Maybe a
@dcastro
dcastro / log.md
Last active January 30, 2022 17:32
coffer example log
$ cabal run exe:coffer -- create diogo/google-work \
  --field user:dcastro@serokell.com \
  --field pw:123 \
  --field url:www.google.com

[SUCCESS] Entry created at '/diogo/google-work'

$ cabal run exe:coffer -- create diogo/google-home \
@dcastro
dcastro / shapeless-fieldname.scala
Last active July 13, 2021 20:01
Shapeless - get a field's name in a type-safe way
import shapeless._
import shapeless.ops.record.Keys
import shapeless.ops.hlist.Selector
import shapeless.tag._
/**
* Gets the name of a case class's field, in a type-safe way.
* If the class does not have a field with the given name, the program won't compile.
*
* Kinda similar to C#'s `nameof` operator, but not bolted onto the language
@dcastro
dcastro / circe_shapeless_record.scala
Created April 27, 2018 14:32
Circe + Shapeless: blacklist fields with type-safety
import shapeless._, record._
import io.circe._
import io.circe.syntax._
import io.circe.generic.encoding._
case class Person(name: String, age: Int)
object Person {
implicit val encodePerson: Encoder[Person] =
ReprObjectEncoder.deriveReprObjectEncoder.contramap { person =>
@dcastro
dcastro / singleton-types.md
Last active March 8, 2021 22:31
singleton-types.md

This tutorial assumes you're already familiar with Haskell's kind system and extensions like DataKinds. If not, please read my blog post first </shameless-plug>.

Say you want to model a TCP connection, and it can be in either an "open" or "closed" status.

data Connection = MkConnection ...