Skip to content

Instantly share code, notes, and snippets.

@nrinaudo
nrinaudo / Trick.java
Created Nov 7, 2021
Surprising Java behaviour...
View Trick.java
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 Aug 13, 2020
Intro to a hypothetical talk on inheritance that I'll never have the courage to actually give
View inheritance-intro.org

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.

View Haskell-Disappointment.md

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

View library-wip.tla
EXTENDS Integers, TLC, Sequences
CONSTANTS Books,
People,
NumCopies
ASSUME NumCopies \in Nat \ {0}
PT == INSTANCE PT
(*--algorithm library
View imageaug.tla
------------------------------ 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 Feb 7, 2019
Using optics to patch `Gen` instances
View GenLenses.scala
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)
View future.md
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.

View Test.scala
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 Aug 13, 2018
Demo of how Reader works
View ReaderDemo.scala
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 {
@nrinaudo
nrinaudo / Or.scala
Created Jan 24, 2018
Semi-automatic derivation
View Or.scala
import io.circe._
import io.circe.generic.semiauto._
import org.scalacheck._, derive._
sealed trait Or[+A, +B] extends Product with Serializable
final case class Left[A](a: A) extends Or[A, Nothing]
final case class Right[B](b: B) extends Or[Nothing, B]
object Or {