Skip to content

Instantly share code, notes, and snippets.

View chrilves's full-sized avatar

Christophe Calvès chrilves

View GitHub Profile
@chrilves
chrilves / flist.scala
Created October 13, 2023 10:23
Lists defined using foldRight
/* A functional type of list
that is exactly the type of foldRight */
type FList[+A] = [R] => (_nil: R, _cons: (A,R) => R) => R
// The empty list defined as its foldRight
def nil[A]: FList[A] =
[R] => (_nil: R, _cons: (A,R) => R) => _nil
// The cons constructor defined also as its foldRight
def cons[A](head: A, tail: FList[A]): FList[A] =
use regex::Regex;
use std::cmp::{max, min, Ordering};
use std::fmt::{Display, Error, Formatter};
use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io::BufRead;
use std::time::Instant;
mod range {
use super::*;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Day21_Dirac_Dice2 {
public static void main(String[] args) {
int player1Starting = 4;
int player2Starting = 8;
enum Weird<A> {
Leaf(A),
Node(Box<Weird<(u8,A)>>)
}
fn traversal<A>(weird: &Weird<A>) {
match weird {
Weird::Leaf(_) =>
println!("Leaf"),
Weird::Node(b) => {
/***************************************************************
Just a small example of Algebraic Data Type (ADT) use
to show why they are called "algebraic".
This is a good exercice to check that you understand
the basics of ADT correctly.
Note: Run-it with Dotty (Scala 3) version >= 0.22
dotr -indent -new-syntax -strict -Yerased-terms -Yexplicit-nulls
/** Simulates a resource can be created and closed
* like a file handler, database connection handler, etc.
*
* Like the ones above, its methods are only available if
* the resource is opened, it must always be closed
* after use to avoid leaks and closing and already closed
* connection is a fault.
*
* @param name the name of the connection
* @param failureProba the probability opening and closing fail.
import $ivy.`io.github.jto:validation-xml_2.12:2.1.1`
import jto.validation._
import jto.validation.xml._
import jto.validation.xml.Rules._
import scala.xml._
import scala.reflect.runtime.universe._
final case class Test(x: Int, y: Int)
implicit val testR: Rule[Node, Test] =
/* Modified version of https://gist.github.com/non/51b83d0abc929cc4f0b153accf2bf02f
* to expose it's GADT's nature and provide the folding function.
*
* Should run out of the box with: amm <the_file>
*/
import $ivy.`org.typelevel:cats-core_2.12:1.6.0`
object demo {
import cats.{Applicative, Functor, Id, Semigroupal, Traverse}
object LensToZiper {
/*
This is a small sleight of hand to show how
[lenses](https://en.wikibooks.org/wiki/Haskell/Lenses_and_functional_references)
are actually related to to zippers (getters + setters).
This connection give some insight about why lenses
have the properties they have. So let's define all
we need for lenses:
import scala.language.higherKinds
trait Functor[F[_]] {
def map[A,B](fa: F[A])(f: A => B): F[B]
}
/* Any functor is by definition covariant because:
Let A and B be two types such that A <: B, then
(fa: F[A]).map((a:A) => (a:S)) : F[S]
but also