Skip to content

Instantly share code, notes, and snippets.

View mandubian's full-sized avatar

Pascal Voitot mandubian

View GitHub Profile
import play.api.libs.json._
import play.api.libs.functional.syntax._
implicit val cReads = Json.reads[Circle]
implicit val pReads = Json.reads[Polygon]
// you can replace __ (root node) by JsPath if you prefer
// | = orElse
// needs to convert to Form explicitly because Reads is invariant
implicit val shapeReads = __.read[Rect].map(x => x:Form) | __.read[Circle].map(x => x:Form)
@mandubian
mandubian / CODE.scala
Last active September 30, 2021 20:03 — forked from playxamplez-admin/CODE
#Json #Reads/#Writes for a sealed #trait & inheriting caseclasses without type indication in Json #Play2.1
import play.api.libs.json._
import play.api.libs.functional.syntax._
sealed trait Shape
case class Circle(c: (Float, Float), r: Float) extends Shape
object Circle {
// reader is covariant and can be implicit in inheriting caseclasses
implicit val reader = Json.reads[Circle]
// writer is contravariant and can't be implicit in inheriting caseclasses
txt="""private bool isEven(int number) {
if (number == 1) return false;
else if (number == 2) return true;
else if (number == 3) return false;
else if (number == 4) return false;
"""
openai.Completion.create(
engine="davinci",
prompt=txt,
>>> import torch
# a Float tensor
>>> a = torch.tensor([[0, 0], [1, 1], [2, 2]], dtype=torch.float)
>>> a
tensor([[0., 0.],
[1., 1.],
[2., 2.]])
# a Long tensor
>>> b = torch.tensor([[3], [4], [5]], dtype=torch.long)
@mandubian
mandubian / kp-monoid.scala
Last active September 30, 2019 21:39
"Monad is a monoid in the category of endofunctors" evidence using trivial sample of Scala Kind-Polymorphism PoC
/** Showing with Kind-Polymorphism the evidence that Monad is a monoid in the category of endofunctors */
object Test extends App {
/** Monoid (https://en.wikipedia.org/wiki/Monoid_(category_theory))
* In category theory, a monoid (or monoid object) (M, μ, η) in a monoidal category (C, ⊗, I)
* is an object M together with two morphisms
*
* μ: M ⊗ M → M called multiplication,
* η: I → M called unit
*
@mandubian
mandubian / CODE.scala
Last active February 27, 2018 15:43 — forked from playxamplez-admin/CODE
#Recursive class #Json #Reads using #lazyRead feature #Play21 #scala A class inheriting a parent trait with a member of the parent type
sealed abstract trait Tree
case class Leaf(a: String) extends Tree
case class Node(l: Tree, r: Tree) extends Tree
implicit val treeR: Reads[Tree] =
__.read[Leaf].map(x => x:Tree) orElse
(
(__ \ "l").lazyRead(treeR) and (__ \ "r").lazyRead(treeR)
)(Node.apply _).map(x => x:Tree)
@mandubian
mandubian / keybase.md
Created July 24, 2015 10:06
keybase.md

Keybase proof

I hereby claim:

  • I am mandubian on github.
  • I am mandubian (https://keybase.io/mandubian) on keybase.
  • I have a public key whose fingerprint is D5C4 503A 757C CB4C 507C 7219 4BA3 91C1 1159 42CE

To claim this, I am signing this object:

// Fake Scalafix rule to rewrite Scala to CCC
// Just so that you catch the idea
final case class CCCRule(index: SemanticdbIndex) extends SemanticRule(index, "CCCRule") {
def convertSubTree(ctx: RuleCtx, v: Tree, tree: Tree): Tree = {
tree match {
// the identity morphism
case t@q"$x => ${y: Term.Name}" if (x.name.isEqual(y)) =>
q"""K.id[${x.decltpe.get}]"""
// our CCC with Graph as morphism
implicit val GraphCCC: ClosedCartesianCat[Graph] = new ClosedCartesianCat[Graph] {
// long boring useless code
}
// we can also implement CCCNumExt for Graph
implicit def GraphCCCNumExt[A](implicit N: Numeric[A], gp: GenPorts[A]): CCCNumExt[Graph, A] = new CCCNumExt[Graph, A] {
// genComp just generate a component with 2 inputs, 1 output and a nice name
def negateC: Graph[A, A] = genComp("-")
def addC: Graph[(A, A), A] = genComp("+")
// We can write this type class parameterized by the morphism and a type A
trait CCCNumExt[->[_, _], A] {
def negateC: A -> A
def addC: (A, A) -> A
def mulC: (A, A) -> A
}
// and implement it for Function1 and any Numeric A
implicit def Function1CCCNumExt[A](implicit N: Numeric[A]): CCCNumExt[Function1, A] = new CCCNumExt[Function1, A] {
def negateC: A => A = N.negate _