Skip to content

Instantly share code, notes, and snippets.

@nicmart
nicmart / droste.md
Last active November 24, 2020 12:24

Droste for the really really impatient

For this tutorial we'll be using a simplified version of our expression tree. Our expressions accept only binary expressions that combine Integers and Strings using plus and minus operators. The rules are simple:

  • Adding or subtracting two integers is allowed. The result is the sum/subtraction of both numbers
  • Adding two strings or a string and an integer is allowed. The result will be the result of concatenating both operands as if they were strings
  • Subtracting a String from an int (or vice versa) is not allowed
Unique Predicates:
List(
Predicate("Num", List(Var("T0"))),
Predicate("Mult", List(Var("T0"), Point, Var("T1"))),
Predicate("Mult", List(Var("T0"), Var("T1"), Var("T2"))),
Predicate("Mult", List(Var("T0"), Var("T2"), Var("T3"))),
Predicate("Mult", List(Var("T0"), Var("T3"), Var("T4")))
)
Time to Compute Substitutions: 315ms
Time to Remove Empty Subst: 0ms
import scalaz.zio.IO
import scala.io.StdIn
object Example {
case class MyError(text: String)
def readOption(availableOptions: Set[String]): IO[InvalidOption, String] =
IO.effectTotal(StdIn.readLine()).flatMap { input =>
if (availableOptions.contains(input)) IO.succeed(input)
else IO.fail(InvalidOption(input, availableOptions))
@nicmart
nicmart / final.scala
Created July 12, 2018 11:01
Introduction to Finally Tagless Encodings
//2 + (3 + 4)
// 9 (evaluate)
// "(2 + (3 + 4))" (pretty printing)
trait AddLanguage[T] {
def literal(n: Int): T
def add(m: T, n: T): T
}
def expr[T](lang: AddLanguage[T]): T = {
@nicmart
nicmart / rectangle.scala
Created February 6, 2018 22:48
Rectangle search algo
package example
case class Rectangle(x1: Int, x2: Int, y1: Int, y2: Int) {
def xInterval: Interval = Interval(x1, x2)
def yInterval: Interval = Interval(y1, y2)
}
case class Interval(from: Int, to: Int) {
def contains(x: Int): Boolean = x >= from && x <= to
def intersect(other: Interval): Boolean = to > other.from && from < other.to
}
@nicmart
nicmart / symantics.scala
Created December 19, 2017 22:43
Experiments with a tagless final encoding for the simply typed lambda-calculus, following section 3 of the paper "Typed Tagless Final Interpreters" by Oleg Kiselyov
import scala.language.higherKinds
trait Symantics[F[_,_]] {
def int[E](n: Int): F[E, Int]
def add[E](n: F[E, Int], m: F[E, Int]): F[E, Int]
def z[E, T]: F[(T, E), T]
def s[E, T, A](f: F[E, T]): F[(A, E), T]
def lam[E, A, B](f: F[(A, E), B]): F[E, A => B]
def app[E, A, B](f: F[E, A => B], a: F[E, A]): F[E, B]
}
@nicmart
nicmart / partitions.scala
Created March 20, 2017 16:45
Partitions
def partitions(n: Int): List[List[Int]] = partitionsGreaterThan(n, 1)
def partitionsGreaterThan(n: Int, m: Int): List[List[Int]] = n match {
case _ if m > n => List()
case _ if m == n => List(List(n))
case _ if m < n =>
val ns1 = partitionsGreaterThan(n - m, m).map { m :: _ }
val ns2 = partitionsGreaterThan(n, m + 1)
ns1 ::: ns2
}
@nicmart
nicmart / SubsetEnumerator.php
Last active February 9, 2016 16:41
Enumerate all the subsets of a set of a given size
<?php
/**
* @author Nicolò Martini - <nicolo@martini.io>
*
* Created on 05/02/2016, 19:52
*/
namespace NicMart;
/**
<?php
/**
* This file is part of BehatIntro
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Nicolò Martini <nicmartnic@gmail.com>
*/
@nicmart
nicmart / README.md
Last active August 29, 2015 14:21
StatementBuilder

StatementBuilder

This library was born to improve the abstraction in the conversion of an Expression to a SQL statement, but it is completely Expression and SQL agnostic, so it can be used for simlar use-cases.

The problem

Let's say you have a type FieldConverter that converts a FieldExpression object to a prepared statement. That is basically a function type

FieldConverter: FieldExpression => PreparedStatement

For the sake of the argument, let's assume that PreparedStatement is just a string with placeholders, without the PDO values bindings.