Skip to content

Instantly share code, notes, and snippets.

View danidiaz's full-sized avatar

Daniel Díaz Carrete danidiaz

View GitHub Profile
@danidiaz
danidiaz / gist:6821903
Created October 4, 2013 06:46
Ix from control.lens.
(ix 13 .~ 'z') ['a','b','c']
-- "abc"
#_ (ix 2 .~ 'z') ['a','b','c']
-- "abz"
@danidiaz
danidiaz / gist:7166886
Created October 26, 2013 08:40
choosing from Control.Lens.Lens
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
import Data.Tree
ps :: Show a => a -> IO ()
ps = putStrLn . show
data Person = Person {
_name :: String,
@danidiaz
danidiaz / gist:7167189
Created October 26, 2013 09:15
inside, from lens
{-# LANGUAGE TemplateHaskell #-}
import Control.Lens
import Data.Tree
data Person = Person {
_name :: String,
_years :: Int
} deriving Show
@danidiaz
danidiaz / gist:7168119
Created October 26, 2013 11:00
Isos and au
import Control.Lens
import Data.Foldable
import Data.Monoid
import Data.Complex
import Data.Complex.Lens
r0 = au (from _polar.wrapping Sum) foldMap [(1.0,pi),(1.0,0.0)]
r1 = view _polar $ getSum $ mconcat $ (map $ view $ from _polar.wrapping Sum) [(1.0,pi),(1.0,0.0)]
r2 = view _polar $ getSum $ foldMap (Sum . (view $ from _polar)) [(1.0,pi),(1.0,0.0)]
@danidiaz
danidiaz / gist:7383997
Created November 9, 2013 10:16
Drop dead easy way to serve a directory through HTTP using Ruby.
require 'rack'
foo = Rack::Directory.new("C:/Users/ZZZZ/.m2")
Rack::Server.start :app => foo, :Port => 9292
@danidiaz
danidiaz / gist:7518431
Last active December 28, 2015 14:59
My solution to the Twitter waterflow problem. Other solutions here: http://chrisdone.com/posts/twitter-problem-loeb
puddles :: [Int] -> Int
puddles l = go 0 [] (zip l (repeat 1)) where
go acc [] (a:b:xs)
| fst a <= fst b = go acc [] (b:xs)
| otherwise = go acc [a] (b:xs)
go acc (p@(pp,cp):ps) (a@(aa,ca):b@(bb,cb):xs)
| aa == bb = go acc (p:ps) ((aa,ca+cb):xs)
| aa < bb = let acc' = acc + ca * min (pp-aa) (bb-aa) in
if pp > bb
then go acc' (p:ps) ((bb,cb+ca):xs)
@danidiaz
danidiaz / gist:7589046
Last active December 29, 2015 00:49
Playing with the _Cons prism and beside from Control.Lens.Traversal.
import Control.Lens
import Data.Char
over (_Cons.beside id each) toUpper "aa"
over (_Cons.beside id each) toUpper "" -- works fine, too.
-- the "aside" prism is pretty sweet, too!
@danidiaz
danidiaz / gist:7612820
Last active December 29, 2015 04:09
Concatenative helpers for Haskell
module Util (
dot, (.:),
dot3, (..:),
cleave,
cleave3,
spread,
spread3,
apply,
apply3,
ifte
@danidiaz
danidiaz / gist:7614715
Last active December 29, 2015 04:29
Composing with applicatives
import Data.Functor.Compose
import Control.Lens
foo = getCompose $ (*3) <$> Compose (pure succ) :: IO (Int -> Int)
-- return 5 <**> foo
-- 18
fim = (. (*3)) <$> pure succ :: IO (Int -> Int)
--return 5 <**> fim
@danidiaz
danidiaz / gist:7629418
Created November 24, 2013 16:58
concatenative obscurity
import System.Directory
import System.FilePath
import Control.Monad.List
import Control.Arrow
import Control.Applicative
import Control.Lens
import Data.Function
import Data.Monoid
cleave :: (a -> b) -> (a -> c) -> (b -> c -> d) -> a -> d