Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
sjoerdvisscher / HFunctorCombinators.hs
Created December 21, 2009 23:43
HFunctor combinators
{-# LANGUAGE RankNTypes, TypeOperators, KindSignatures, ScopedTypeVariables #-}
import Control.Functor.HigherOrder
import Control.Functor.Extras
newtype K k (r :: * -> *) a = K k
newtype I f (r :: * -> *) a = I (r (f a))
newtype E (r :: * -> *) a = E a
data (f :*: g) (r :: * -> *) a = f r a :*: g r a
data (f :+: g) (r :: * -> *) a = L (f r a) | R (g r a)
@jagbolanos
jagbolanos / dfsalgorithms.hs
Created March 5, 2011 23:37
Some DFS based graph algorithms in Haskell
--Proposed solutions to problems 87,88 and 89 of "99 Haskell Problems"
--Not optimal but they work
--If you know haskell and want to solve some problems there are some missing at:
--http://www.haskell.org/haskellwiki/99_questions/80_to_89
import Data.List
type Node = Int
type Edge = (Node,Node)
type Graph = ([Node],[Edge])
@sjoerdvisscher
sjoerdvisscher / RepresentableFix.hs
Created March 14, 2011 00:45
If a functor is representable and its key is a fixed point, then there's a much faster way to implement fmap, pure and ap.
{-# LANGUAGE TypeFamilies, DeriveFunctor #-}
import Data.Key (Key)
import Data.Functor.Foldable (Fix(..), fold, unfold)
import Control.Applicative
import Control.Arrow (left)
data RF f a r = RF { getRF :: Key f -> Either a (Key f, r) } deriving Functor
@Kedrigern
Kedrigern / Tree.hs
Last active March 27, 2024 00:43
Implementation of binary search tree in Haskell
{- Implementation of BST (binary search tree)
Script is absolutly free/libre, but with no guarantee.
Author: Ondrej Profant -}
import qualified Data.List
{- DEF data structure -}
data (Ord a, Eq a) => Tree a = Nil | Node (Tree a) a (Tree a)
deriving Show
@orclev
orclev / trie.hs
Created February 28, 2012 04:23
Haskell trie implementation
import Data.Maybe
import Control.Monad (liftM)
import Data.List (isPrefixOf)
import qualified Data.Map as M
import qualified Data.Foldable as F
-- | Trie container data type
data Trie a = Trie { value :: Maybe a
, children :: M.Map Char (Trie a) }
deriving (Show)
@folone
folone / hello-ski.hs
Created August 27, 2012 08:16 — forked from shangaslammi/hello-ski.hs
Hello World using Applicative instance for ((->) r) as a computational basis
$ ghci
GHCi, version 7.4.2: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l hello-ski.hs
[1 of 1] Compiling Main ( hello-ski.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
Hello world!
@hallettj
hallettj / state-example-game.js
Created November 9, 2012 04:41
Implementation of a State monad in JavaScript
/*
* Example of a state monad in use. This is adapted from an example on
* the Haskell Wiki:
* http://www.haskell.org/haskellwiki/State_Monad#Complete_and_Concrete_Example_1
*/
require(['state', 'qunit'], function(state, qunit) {
/*
* playGame() is a recursive function that given an array of moves
* defines an algorithm for constructing a final game score. Along
@asalant
asalant / connectWithRetry.js
Created November 17, 2012 01:24
Retry connecting to mongo if initial connect fails
var mongoose = require('mongoose')
var mongoUrl = "mongodb://localhost:27017/test"
var connectWithRetry = function() {
return mongoose.connect(mongoUrl, function(err) {
if (err) {
console.error('Failed to connect to mongo on startup - retrying in 5 sec', err);
setTimeout(connectWithRetry, 5000);
}
});
@akihiro4chawon
akihiro4chawon / Main.hs
Created December 3, 2012 08:12
BrainFuck Arrow
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
module Main where
import Control.Applicative
import Control.Arrow
import Control.Monad (void)
import Data.Attoparsec.ByteString.Char8
import qualified Data.ByteString.Char8 as B
@theangryangel
theangryangel / AuthController.js
Created February 28, 2013 21:54
sails (v0.8.82) + passport + passport-local Rough Example. For the love of all that is holy, don't use this in production.
// api/controllers/AuthController.js
var passport = require('passport');
var AuthController = {
login: function (req,res)
{
res.view();
},