Skip to content

Instantly share code, notes, and snippets.

@martinsson
martinsson / moneychange.idr
Last active February 26, 2018 01:10
Guarantee (through the type-system) the right amount of change is given
module moneychange
import Data.String
--%default total
data Coin : (value: Nat) -> Type where
OneCent : Coin 1
FiveCent : Coin 5
TenCent : Coin 10
-- without this type that hides the value I can't put the coins in a list
@martinsson
martinsson / twitter.idr
Last active February 23, 2018 17:21
Dependent types to constrain the business domain in a twitter-like application
import Data.List
UserId: Type
UserId = String
data Message: (userId: UserId) -> Type where
MkMessage: (userId: UserId) -> String -> Message userId
data DeleteCommand: (userId: UserId) -> Type where
MkDeleteCommand: (userId: UserId) -> DeleteCommand userId
@martinsson
martinsson / fibonacci.ts
Last active March 12, 2018 22:05
Solving fibonacci in reactive streams. Why? Because it's possible :)
import {Observable} from "rxjs/Observable";
import {Subject} from "rxjs/Subject";
export function fibonacciStream() {
let fibN2: Subject<number> = new Subject();
// initiate the series, so that both this and fibN1 has atleast one
setImmediate(() => {
fibN2.next(0);
fibN2.next(1);
@martinsson
martinsson / Fibonacci.ts
Last active February 9, 2018 22:19
Algorithm for generating fibonacci numbers, javascript is not that slow after all
import * as _ from 'lodash'
function sumPreviousTwo(numbers: number[]) {
let [n2, n1] = _.takeRight(numbers, 2);
return n2 + n1;
}
export function fibonacci(length: number): number {
let firstTwoNumbers = [0, 1];
@martinsson
martinsson / Roman-spec.ts
Last active February 8, 2018 23:36
Example of a solution to the Roman Numerals Kata, done Craftsmanship Grenoble
import {assert} from "chai";
import {Roman} from "../src/Roman";
function toRoman(arabic: number) {
return new Roman().toRoman(arabic);
}
describe('RomanNumbers', () => {
describe('toRoman() is', () => {
@martinsson
martinsson / fizzbuzz-alternative.idr
Created January 20, 2018 13:38
A fizzbuzz with strong type-safety, but still extensible to allow Bang if multiple of 7 etc
import Data.So
%default total
fizz : Nat -> Bool
fizz n = (modNatNZ n 3 SIsNotZ) == 0
buzz : Nat -> Bool
buzz n = (modNatNZ n 5 SIsNotZ) == 0
data FizzT : (n: Nat) -> Type where
@martinsson
martinsson / fizzbuzz.idr
Last active June 21, 2019 09:58
Dependently typed fizzbuzz with Idris
%default total
-- Proofs that a value is divisible my 3 and 5 respectively
IsFizz : (k : Nat) -> Type
IsFizz k = (modNatNZ k 3 SIsNotZ = 0)
IsBuzz : Nat -> Type
IsBuzz k = (modNatNZ k 5 SIsNotZ = 0)
-- The type Fizzbuzz, one can only construct an instance of Fizz if one can provide
@martinsson
martinsson / CoinChange.idr
Created January 12, 2018 23:26
Strongly typed coin change kata in idris
data Change : (amount: Nat) -> Type where
NoChange : Change Z
SucChange : (prev: Change amount) -> Change (S amount)
Composite : (ch1: Change amount1) -> (ch2: Change amount2) ->
{auto prf: amount1 + amount2 = amount'}-> Change amount'
-- Simple : (value: Nat) -> (quantity: Nat) ->
-- {auto prf: value * quantity = amount'} -> Change amount'
extractAmount : (Change amount) -> Nat
@martinsson
martinsson / fizz buzz.idr
Created November 30, 2017 22:22
Proven Fizzbuzz pairing session
-- diversion, money change, fizzbuzz (divisible by)
-- Learnings:
-- Tom: I understand this, until(!) I implement it
-- functions that take a proof, ensure that they're never called without that proof and
-- the arguements to the proof
-- For instance constructors
@martinsson
martinsson / TripService.js
Last active November 13, 2017 23:04
Monadic version of the trip-service-kata by Sandro Mancuso, using Maybe and Either. Public interface still compatible
const { Maybe, Either } = require('monet');
class TripService {
// Monadic version of the public function, returns an Either
getTripsByUserEither(user, loggedUser) {
return Maybe.fromNull(loggedUser)
.toEither('User not logged in.')
.flatMap(connectedUser => this._doGetTripsByUser(user, connectedUser));