Skip to content

Instantly share code, notes, and snippets.

View blouerat's full-sized avatar

Bastien Louërat blouerat

View GitHub Profile
@blouerat
blouerat / collectReverse.hs
Created June 17, 2014 07:42
1HaskellADay: collectReverse
import Control.Applicative
{- We still have yesterday's room
-}
data Room a = Room {doors :: (Bool, Bool), content :: a}
deriving (Eq)
{- | Given a list of rooms, we want to go as far as we can and collect the elements
in he room, **starting from the last room**.
@blouerat
blouerat / collect.hs
Last active August 29, 2015 14:02
1HaskellADay: collect (a room traversal)
import Control.Applicative
{- You have a list of rooms, each rooms has two doors and contain one element.
To enter a room, the first door must be opened. To leave a room, the second door
must be open. It means that if you want to move from one room to the next one,
both the second door of the first room and the first door of the second room.
-}
data Room a = Room (Bool, Bool) a
{- | Given a list of rooms, we want to go as far as we can and collect the elements
@blouerat
blouerat / countOccurrences.hs
Created June 2, 2014 09:51
1HaskellADay: countOccurrences
import Data.Map hiding (foldr)
{-| Count character occurrences in a string
Examples:
>>> lookup 'l' $ countOccurrences "hello"
Just 2
>>> lookup 'n' $ countOccurrences "hello"
Nothing
-}
@blouerat
blouerat / interleaveSwapped.hs
Last active August 29, 2015 14:01
1HaskellADay: interleaveSwapped
import Control.Applicative
import Data.Tuple
{- Yesterday, we appended swapped values:
Example
> appendSwapped [(1,2),(2,3)]
[(1,2),(2,3),(2,1),(3,2)]
-}
appendSwapped :: [(a,a)] -> [(a,a)]
@blouerat
blouerat / appendSwapped.hs
Last active August 29, 2015 14:01
1HaskellADay: appendSwapped
import Control.Applicative
import Data.Tuple (swap)
{- Given a list of tuples, return a list containing the initial elements and the swapped elements
It's an easy one to start the week.
Example
> appendSwapped [(1,2),(2,3)]
[(1,2),(2,3),(2,1),(3,2)]
-}
@blouerat
blouerat / FreeIdZero.scala
Created May 2, 2014 16:18
Scala, Free Monad and TAPL
import scalaz.Functor
import scalaz.Free
sealed trait Term[+A]
object Term {
case object Zero extends Term[Nothing]
case class Id[A](term: A) extends Term[A]
implicit val termFunctor: Functor[Term] = new Functor[Term] {
def map[A, B](fa: Term[A])(f: A => B): Term[B] = fa match {
@blouerat
blouerat / Foobar.java
Last active September 29, 2016 00:50
Why Java 8 matters
package java8fun;
public class Foobar {
private String foo;
private Integer bar;
public Foobar(String foo, Integer bar) {
this.foo = foo;
this.bar = bar;
@blouerat
blouerat / had.hs
Last active August 29, 2015 13:59
HAD.Y2014.M04.D16.Exercise
reverseMap :: [a -> b] -> a -> [b]
reverseMap = sequence
with a new array: 10
with a new array and new value: 10
with a new value: 42
@blouerat
blouerat / OOP.java
Last active December 16, 2015 17:59
OOP example
package oop;
public class OOP {
public static void main(String[] args) {
Pants pants = new Pants("dark blue");
System.out.println("I have a pair of " + pants.getColour() + " pants!");
pants.setColour("vermilion");
System.out.println("Now my pants are " + pants.getColour() + ", and it's much better!");
}