Skip to content

Instantly share code, notes, and snippets.

View d-plaindoux's full-sized avatar
🐪

Didier Plaindoux d-plaindoux

🐪
View GitHub Profile
@d-plaindoux
d-plaindoux / Applicative.scala
Last active December 9, 2020 07:20
Scala Functor, Applicative and Monad thanks to context bounds
package control
trait Applicative[M[_]] extends Functor[M] {
def pure[A](a: A): M[A]
def applicative[A, B](f: M[A => B])(a: M[A]): M[B]
override def map[A, B](f: A => B)(a: M[A]): M[B] = applicative(pure(f))(a)
@d-plaindoux
d-plaindoux / foo.rs
Created May 24, 2019 04:23
Implement a trait when another is already implemented
#![allow(dead_code)]
use std::io::{self, Read};
use std::marker::PhantomData;
pub trait Foo<T: Read> {
fn get_mut(&self) -> &mut T;
}
struct FooStruct<T,R> where T:Read, R:Foo<T> {
@d-plaindoux
d-plaindoux / parsec.rs
Last active February 28, 2019 04:31
Basic Parsers and incomplete string recogniser
#![allow(dead_code)]
use std::marker::PhantomData;
// -----------------------------------------------------------------------------
// Response definition
// -----------------------------------------------------------------------------
pub enum Response<A> {
Success(A, usize),
@d-plaindoux
d-plaindoux / for-comprehension.rs
Last active September 8, 2018 09:04
Scalas' for comprehension in Rust
#[macro_export]
macro_rules! foreach {
($a:ident <- ($e:expr) if ($cond:expr) yield $result:expr) => {{
$e.filter(|$a| $cond).map(|$a| $result)
}};
($a:ident <- ($e:expr) yield $result:expr) => {{
$e.map(|$a| $result)
}};
($a:ident <- ($e:expr) $($r:tt)+) => {{
$e.flat_map(|$a| foreach!($($r)+))
@d-plaindoux
d-plaindoux / comprehension.rs
Last active September 7, 2018 08:20
Simple comprehension macro in Rust
#[macro_export]
macro_rules! comprehension {
(($expr:expr) | $id:ident <- ($range:expr) if $cond:expr) => {{
let mut result = Vec::default();
for $id in $range {
if $cond {
result.push($expr);
}
}
@d-plaindoux
d-plaindoux / expression-problem.rs
Last active August 22, 2018 06:23
Rust example combining structures and trait implementation thanks to generic and type constraints
//---------------------------------------------------------------------------------
// Parser trait type defintion
//---------------------------------------------------------------------------------
trait Parser<E> {}
//---------------------------------------------------------------------------------
// Executable Parsers (Self is constraint)
//---------------------------------------------------------------------------------
module TennisKata
%default total
-- Data types
data Point = Love | Fiftheen | Thirteen | Fourteen
isFourteen : Point -> Bool
isFourteen Fourteen = True
@d-plaindoux
d-plaindoux / associated-type.swift
Created June 14, 2017 05:20
Swift deferred object creation using associated type
protocol A {
associatedtype E
func new() -> E?
}
protocol C {
init()
}
@d-plaindoux
d-plaindoux / Peano.swift
Created August 12, 2016 02:56
A taste of Swift 3.0 - Peano data type design
import Foundation
public indirect enum Peano: ExpressibleByIntegerLiteral {
case Succ(Peano),
Zero
public init(integerLiteral v: IntegerLiteralType) {
if v == 0 {
self = .Zero
} else {
@d-plaindoux
d-plaindoux / gist:6cf61856a137e3491d31
Last active August 29, 2015 14:19
Type Level programming
//
// From https://www.parleys.com/tutorial/type-level-programming-scala-101
//
import scala.language.higherKinds
// Int type programming level
sealed trait IntType {
type plus[that <: IntType] <: IntType