Skip to content

Instantly share code, notes, and snippets.

View pfcoperez's full-sized avatar

Pablo Francisco Pérez Hidalgo pfcoperez

View GitHub Profile
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)
#!/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;
val jsonString =
"""
|{
| "cluster_id": "47b0f5e332b5456a9f08ef7f9ca44f55",
| "cluster_name": "admin-console-elasticsearch",
| "healthy": true,
| "status": "initializing",
| "plan_info": {
| "healthy": true,
| "pending": {
# 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

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]] =
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)
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)
// 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 $ivy.`io.circe::circe-parser:0.11.1`
import io.circe._, io.circe.parser._, io.circe.syntax._
for {
input <- parse(repl.clipboard.read).right
toAdd <- parse("""{"new_field": "added programatically"}""").right
} repl.clipboard.write(input.deepMerge(toAdd).toString)

Keybase proof

I hereby claim:

  • I am pfcoperez on github.

  • I am pfcoperez (https://keybase.io/pfcoperez) on keybase.

  • I have a public key ASBBp5iz_1Z1-j5aIX7WnDsR7ODlkVK_JxoZTbaoOokdlwo