Skip to content

Instantly share code, notes, and snippets.

View jbpotonnier's full-sized avatar

Jean-Baptiste Potonnier jbpotonnier

View GitHub Profile
@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 =
<!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 / 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");
}
});
@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 / 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 / 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 / accessor.hs
Last active December 18, 2015 02:08
Access json data using path. Functional programming to the rescue :P
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text as Text
import Data.Text (Text)
import Data.Aeson (encode, decode, Value, Value(..))
import qualified Data.HashMap.Strict as HashMap
import Data.Vector ((!))
path :: Text -> Value -> Value
@jbpotonnier
jbpotonnier / override.js
Last active December 14, 2015 01:38
Override method in JavaScript
var create = function (proto) {
var F = function () {};
F.prototype = proto;
return new F();
};
var Point = {};
Point.initialize = function (x, y) {
this.x = x;
this.y = y;
@jbpotonnier
jbpotonnier / Main.hs
Last active December 10, 2015 19:18
EventSource streaming using Haskell and redis brpop. Use redis-cli to push some data in the "val" list and see it appear in your browser. It is not that hard... finally. There are some events not appearing at the beginning of the sequence, I don't know why, after that it works well.
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Applicative
import Snap.Core
import Snap.Util.FileServe
import Snap.Http.Server
import Data.Monoid (mconcat)
import Blaze.ByteString.Builder.Char8
import Blaze.ByteString.Builder
@jbpotonnier
jbpotonnier / posfix.ml
Last active October 12, 2015 05:18
Postfix evaluation
let push : float list -> string -> float list =
fun stack input -> match stack, input with
| (b::a::rest), "+" -> a +. b::rest
| (b::a::rest), "-" -> a -. b::rest
| (b::a::rest), "*" -> a *. b::rest
| (b::a::rest), "/" -> a /. b::rest
| rest, number -> (float_of_string number)::rest
let polish_eval : string -> float =
fun input ->