Skip to content

Instantly share code, notes, and snippets.

View pfcoperez's full-sized avatar

Pablo Francisco Pérez Hidalgo pfcoperez

View GitHub Profile
// Static type
case class A(x: Int) // This is a type we can't change nor extend using inheritance. It might come from a third party library.
// Contract (can be in a completely different module or library than where `A` has been defined.
type Id = String
trait Persistence[T] { // This is a type-class: A contract with operations that can be applied to any `T`
// In this example, it represents the sets operations to read from and write to a persisent storage.
import cats.data.Writer
import cats.syntax.writer._
import cats.instances.all._
type Metadata[Meta, T] = Writer[Meta, T]
object syntax {
implicit def metadataAs[Meta, T](m: Metadata[Meta, T]): T = m.run._2
implicit class MetadataExt[T](x: T) {
def withMetadata[Meta](m: Meta): Metadata[Meta, T] = Writer.apply(m, x)
case class Metadata[Meta, T](x: T, meta: Meta)
trait MetadataOps[T] {
def withMetadata[Meta](x: T, m: Meta): Metadata[Meta, T]
}
object syntax {
implicit def metadataAs[Meta, T](m: Metadata[Meta, T]): T = m.x
implicit class MetadataExt[T : MetadataOps](x: T) {
def withMetadata[Meta](m: Meta): Metadata[Meta, T] = implicitly[MetadataOps[T]].withMetadata(x, m)

I am curious about what's the approach you'd follow to allow run-time input/state change the way circe Encoders and Decoders serialize and de-serialize a type.

For example, imagine you want to redact values depending on certain run-time value:

    implicit def sensitiveEncoder[T, RedactedT](
      implicit redactedEncoderEvidence: Encoder[RedactedT],
      encoderEvidence: Encoder[T],
      context: SerdesContext
    ): Encoder[Sensitive[T, RedactedT]] =
# http://xmodulo.com/geographic-location-ip-address-command-line.html
for ip in $(traceroute -n 9.9.9.9 | awk '{print $2}' | tail -n +2); do echo "$ip -> $(geoiplookup $ip)"; done
for ip in $(traceroute -n www.google.es | awk '{print $2}' | tail -n +2); do curl ipinfo.io/$ip | jq .; echo ""; done
val jsonString =
"""
|{
| "cluster_id": "47b0f5e332b5456a9f08ef7f9ca44f55",
| "cluster_name": "admin-console-elasticsearch",
| "healthy": true,
| "status": "initializing",
| "plan_info": {
| "healthy": true,
| "pending": {
#!/bin/bash
BLIGHT_PATH=$(sudo find /sys -type d -name intel_backlight)
MAX_BRIGHTNESS=$(cat $BLIGHT_PATH/max_brightness)
while [ 1 ]; do
echo "$MAX_BRIGHTNESS*$(fswebcam --png -1 - | convert - -colorspace gray -format "%[fx:mean]" info:)" | \
bc -l | cut -d '.' -f 1 | \
sudo tee $BLIGHT_PATH/brightness;
class C(val x: Int, lazyNext: => C) { lazy val next = lazyNext }
lazy val head: C = new C(1, tail)
val tail: C = new C(2, head)
object Caesar extends App {
private lazy val pos2symbol: Vector[Char] = (('A' to 'Z') ++ ('a' to 'z') :+ ' ' ).toVector
private lazy val symbol2pos: Map[Char, Int] = pos2symbol.zipWithIndex.toMap
def encode(msg: String)(implicit key: Int): String =
msg collect { case c if symbol2pos contains c =>
pos2symbol((symbol2pos(c) + key) % symbol2pos.size)
}
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
/*! jQuery v2.0.0 | (c) 2005, 2013 jQuery Foundation, Inc. | jquery.org/license
//@ sourceMappingURL=jquery.min.map
*/
(function(e,undefined){var t,n,r=typeof undefined,i=e.location,o=e.document,s=o.documentElement,a=e.jQuery,u=e.$,l={},c=[],f="2.0.0",p=c.concat,h=c.push,d=c.slice,g=c.indexOf,m=l.toString,y=l.hasOwnProperty,v=f.trim,x=function(e,n){return new x.fn.init(e,n,t)},b=/[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source,w=/\S+/g,T=/^(?:(<[\w\W]+>)[^>]*|#([\w-]*))$/,C=/^<(\w+)\s*\/?>(?:<\/\1>|)$/,k=/^-ms-/,N=/-([\da-z])/gi,E=function(e,t){return t.toUpperCase()},S=function(){o.removeEventListener("DOMContentLoaded",S,!1),e.removeEventListener("load",S,!1),x.ready()};x.fn=x.prototype={jquery:f,constructor:x,init:function(e,t,n){var r,i;if(!e)return this;if("string"==typeof e){if(r="<"===e.charAt(0)&&">"===e.charAt(e.length-1)&&e.length>=3?[null,e,null]:T.exec(e),!r||!r[1]&&t)return!t||t.jquery?(t||n).find(e):this.constructor(t).find(e);if(r[1]){i