Skip to content

Instantly share code, notes, and snippets.

View gabro's full-sized avatar

Gabriele Petronella gabro

View GitHub Profile
@gabro
gabro / crypto.scala
Created July 22, 2020 05:49
Read string into keyring
import org.bouncycastle.openpgp.{
PGPPublicKeyRing,
PGPPublicKeyRingCollection,
PGPUtil,
}
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator
def readPGPPublicKeys(pubKeys: List[String]): PGPPublicKeyRingCollection = {
val fingerprintCalculator = new JcaKeyFingerprintCalculator()
val publicKeyRings = pubKeys.map { pubKey =>
@gabro
gabro / extension.ts
Created September 28, 2018 08:37
VSCode decoration API
'use strict';
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import { window, commands, ExtensionContext, Range, Position, TextEditor, workspace, DecorationOptions } from 'vscode';
const decorationType = window.createTextEditorDecorationType({ })
function addDecorations(e: TextEditor) {
try {
package docs
import mdoc.Reporter
import mdoc.StringModifier
import scala.meta.inputs.Input
import java.util.UUID
/** Transforms scala code blocks into Scastie snippets
*
* ==Usage==
export default function (babel) {
const { types: t } = babel;
return {
visitor: {
NumericLiteral(path) {
path.node.value += 1
}
}
};
export default function(context) {
return {
CallExpression(node) {
const { callee } = node;
if (callee.type === "MemberExpression" && callee.object.name === "console" && callee.property.name === "log") {
context.report(node, "Do not use console.log");
}
}
};
}
parser | AST | ES2017 | ES next
-----------|:---------------:|:------:|:--------------:
acorn | ESTree | ✅ | only stage-4
babylon | Babylon AST | ✅ | ✅
espree | ESTree | ✅ | same as acorn
esprima | ESTree | ✅ | ❌
flow | ESTree + custom | ✅ | ✅
TypeScript | TypeScript AST | ✅ | ✅
@gabro
gabro / cats.scala
Created February 25, 2017 10:52
Monad Transformers for the working programmer
import cats.data.OptionT, cats.std.future._
def findAddressByUserId(id: Long): Future[Option[Address]] =
(for {
user <- OptionT(findUserById(id))
address <- OptionT(findAddressByUser(user))
} yield address).value
@gabro
gabro / compose-applicative.js
Last active November 21, 2016 13:04
Applicative instances composition in vanilla js
function composeFunctor(f, g) {
function map(h, fga) {
return f.map(ga => g.map(h, ga), fga)
}
return { map }
}
function composeApplicative(f, g) {
@gabro
gabro / applicative.js
Created November 21, 2016 10:22
Applicative abstraction in vanilla.js
const arr = {
ap(fab, fa) {
return fab.reduce((acc, f) => acc.concat(fa.map(f)), [])
}
}
const f = (n) => n * 2
const g = (n) => n + 1
const a1 = [f, g]
@gabro
gabro / validation.flow.js
Last active February 24, 2020 16:38
Dynamic object validation using $ObjMap in Flow
/* @flow */
// A simplified representation of types using phantom types (so that we store the Type information both at value and type level)
class Type<T> {};
class StringT extends Type<string> {}
class NumberT extends Type<number> {}
// A schema for a User
const User = {
name: new StringT(),