Skip to content

Instantly share code, notes, and snippets.

View jbpotonnier's full-sized avatar

Jean-Baptiste Potonnier jbpotonnier

View GitHub Profile
@jbpotonnier
jbpotonnier / Friends.hs
Created July 5, 2013 12:37
Atelier Haskell
module Friends where
data Relation = Friend | Cousin | Professor deriving (Show, Eq)
data Person = Person String deriving (Show, Eq)
data Fact = Fact Relation (Person, Person) deriving (Show, Eq)
data Graph = Graph [Fact] deriving Show
areFriends :: Graph -> Person -> Person -> Bool
areFriends (Graph facts) person1 person2 =
(not . null) [(p1, p2) | Fact Friend (p1, p2) <- facts, p1 == person1 && p2 == person2]
@jbpotonnier
jbpotonnier / Reco.hs
Last active December 19, 2015 09:59
Recommend products based on sales. There is an approximation when sorting by sales, considering the wholes sales, when only the sales of people having bought the same product should be used. The data structure could be easily implemented in Redis ;)
module Reco where
import Prelude hiding (product)
import qualified Data.Set as Set
import Data.Set(Set)
import qualified Data.Map as Map
import Data.Map(Map)
import Data.List(sortBy)
import Data.Ord (comparing)
@jbpotonnier
jbpotonnier / Main.hs
Created September 16, 2013 21:08
The code I wrote as an exercise to answer Zalora job offer.
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.List (intercalate)
import Data.Maybe (fromMaybe)
import Network.HTTP.Conduit (simpleHttp)
import Control.Concurrent.Async (mapConcurrently)
@jbpotonnier
jbpotonnier / events.js
Last active December 26, 2015 08:59
Fun with Knockout and ajax calls. Display and update the last events on Github.
var events = ko.mapping.fromJS([]);
ko.applyBindings({
events: events,
onRowClicked: function (row) {
// modify model to update UI. Will be re-updated by next ajax call :P
row.actor.login("FOO");
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Gists</title>
<link type="text/css" rel="stylesheet" href="style.css">
<script type='text/javascript' src='lib/zepto.min.js'></script>
<script type='text/javascript' src='lib/knockout-2.3.0.js'></script>
<script type='text/javascript' src='lib/knockout.mapping-latest.js'></script>
</head>
@jbpotonnier
jbpotonnier / FilterAndRemoveEmpty.hs
Last active December 27, 2015 17:39
Filter tree using a predicate, like the widgets to help finding file in directories.
module FilterTree where
import Data.Maybe (mapMaybe)
data Tree a b = Leaf a
| Node a [Tree a b]
deriving Show
filterTree :: (a -> Bool) -> Tree a b -> Maybe (Tree a b)
filterTree p tree =
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
{-# LANGUAGE DeriveGeneric #-}
module Json where
import Test.SmallCheck.Series
import GHC.Generics
import Data.List (intercalate)
import qualified Text.ParserCombinators.Parsec.Token as Token
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
{-# LANGUAGE DeriveGeneric #-}
module Polynomial where
import Test.SmallCheck.Series (Serial)
import GHC.Generics (Generic)
import qualified Text.ParserCombinators.Parsec as Parsec
import Text.ParserCombinators.Parsec (ParseError, Parser, sepBy1)
@jbpotonnier
jbpotonnier / ghci
Last active January 3, 2016 12:29
Some tools to use Haskell in a shell pipe. ex: cat stats.dat | hask 'maplines (nth 1 |> to_float) |> maximum'
import Control.Category ((>>>))
let io h = interact $ (++"\n") . show . h
let maplines h = map (h . words) . lines
let to_float = read :: String -> Float
let nth = flip (!!)
let (|>) = (>>>) :: (a -> b) -> (b -> c) -> (a -> c)
@jbpotonnier
jbpotonnier / Connector.hs
Last active August 29, 2015 13:56
Using semaphore to allocate resources.
import Network (withSocketsDo, accept, listenOn, PortID(PortNumber))
import Control.Concurrent (forkIO)
import System.IO (Handle, hClose, hGetLine, hSetBuffering, BufferMode (..))
import Control.Applicative ((<$>), (<*>))
import Control.Monad (forever)
import Control.Monad.STM (atomically)
import Control.Concurrent.STM (STM)
import Control.Concurrent.STM.TChan (TChan, newTChan, writeTChan, readTChan)
import Control.Concurrent.STM.TVar (TVar, newTVar, writeTVar, readTVar)
import Control.Concurrent.STM.SSem (SSem)