Skip to content

Instantly share code, notes, and snippets.

@nrinaudo
nrinaudo / Basic.v
Created August 16, 2023 16:02
Coq simplification "issue"
Inductive letter : Type := A | B| C | D | E | F.
Inductive modifier : Type := Plus | Natural | Minus.
Inductive grade : Type := Grade (l: letter) (m: modifier).
Inductive comparison: Set :=
| Eq: comparison
| Lt: comparison
| Gt: comparison.
@nrinaudo
nrinaudo / Trick.java
Created November 7, 2021 13:23
Surprising Java behaviour...
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
class Demo {
static class NotComparable {}
public static void main(String[] args) {
List<NotComparable> list = Arrays.asList(new NotComparable(), new NotComparable());
@nrinaudo
nrinaudo / inheritance-intro.org
Created August 13, 2020 19:58
Intro to a hypothetical talk on inheritance that I'll never have the courage to actually give

Hello, my name is Nicolas Rinaudo and I’m here today because I’d like to talk to you about one of the core features of our favourite language, one that we, as OOP programmers, should be intimately familiar with: inheritance.

Yes, I’m aware that there’s a fringe of the community that refuses to use inheritance on philosophical grounds, and I respect that. I just think it’s a bit of a shame because honestly, I couldn’t work without algebraic data types, and, well, Scala does them with inheritance. Or without type classes, for that matter - anybody that knows me knows I love my type classes - and they’re also done with inheritance in Haskell. I mean in Scala. Well, in both, really.

Don’t believe me? Here’s what the Haskell documentation has to say on the subject (https://www.haskell.org/tutorial/classes.html):

We say that Eq is a superclass of Ord (conversely, Ord is a subclass of Eq), and any type which is an instance of Ord must also be an instance of Eq.

Introduction

I was recently asked to explain why I felt disappointed by Haskell, as a language. And, well. Crucified for crucified, I might as well criticise Haskell publicly.

First though, I need to make it explicit that I claim no particular skill with the language - I will in fact vehemently (and convincingly!) argue that I'm a terrible Haskell programmer. And what I'm about to explain is not meant as The Truth, but my current understanding, potentially flawed, incomplete, or flat out incorrect. I welcome any attempt at proving me wrong, because when I dislike something that so many clever people worship, it's usually because I missed an important detail.

Another important point is that this is not meant to convey the idea that Haskell is a bad language. I do feel, however, that the vocal, and sometimes aggressive, reverence in which it's held might lead people to have unreasonable expectations. It certainly was my case, and the reason I'm writing this.

Type classes

I love the concept of type class

EXTENDS Integers, TLC, Sequences
CONSTANTS Books,
People,
NumCopies
ASSUME NumCopies \in Nat \ {0}
PT == INSTANCE PT
(*--algorithm library
------------------------------ MODULE imageaug ------------------------------
EXTENDS Integers, FiniteSets, TLC
CONSTANTS Documents,
Models,
MaxRetries,
NULL
ASSUME MaxRetries \in Nat \ {0}
ASSUME Cardinality(Documents) /= 0
@nrinaudo
nrinaudo / GenLenses.scala
Created February 7, 2019 20:37
Using optics to patch `Gen` instances
import monocle.macros._
import org.scalacheck._
import scalaz.scalacheck.ScalaCheckBinding._
// Deep hierarchy of product types.
case class Document(author: Author)
case class Author(firstName: String, lastName: String, city: City)
case class City(name: String, country: Country)
case class Country(name: String, continent: Continent)
case class Continent(name: String)
title layout
Start independent Futures outside of a for-comprehension
article

When working with independent [Futures][Future], make sure not to initialise them inside a for-comprehension.

Reason

For-comprehension will create a dependency between your [Futures][Future], turning your code synchronous behind your back.

object Test extends App {
val benchIterations = 10
val warmupIterations = 10
val repeat = 100000
val a = "foo"
val b = 123
def benchOne[U](a : => U): Long =
(0 to repeat).foldLeft(0L) { case (total, _) =>
@nrinaudo
nrinaudo / ReaderDemo.scala
Created August 13, 2018 21:17
Demo of how Reader works
import cats._
import cats.instances.all._
import cats.syntax.all._
final case class Configuration()
final case class Database()
final case class HttpServer()
object Test extends App {