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 / peano's integer
Last active August 29, 2015 13:57
Denotate Peano's integer addition using Golang - Kind of pattern matching based on types only
package main
import "fmt"
type Peano interface { isPeano() }
// Zero
type Zero struct {}
func (z Zero) isPeano() {}
@d-plaindoux
d-plaindoux / signature.py
Last active August 29, 2015 14:04
Simple type specification in Python
#
# Simpla and Naive type checker ...
#
import inspect
class TypeCheckError(Exception):
pass
@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
@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 / 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()
}
module TennisKata
%default total
-- Data types
data Point = Love | Fiftheen | Thirteen | Fourteen
isFourteen : Point -> Bool
isFourteen Fourteen = True
@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)
//---------------------------------------------------------------------------------
@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 / 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 / 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),