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:5579007
Last active December 17, 2015 08:18
bash stuff
cat | grep foo
{ cat aabsdf.xx 2>&1 ; } 2>/dev/null
# http://stackoverflow.com/questions/2342826/how-to-pipe-stderr-and-not-stdout?rq=1
# http://www.gnu.org/software/bash/manual/bashref.html#Redirections
# http://mywiki.wooledge.org/Redirection
@danidiaz
danidiaz / gist:5628188
Created May 22, 2013 14:56
Scala first steps
object Foo {
def main(args: Array[String]):Unit = {
println("jaaaaaar")
args.foreach(println)
args.foreach(println(_))
args.foreach((s:String)=>println(s))
val listy = List(2,3,4,5)
val x = listy match {
case _ => "whatever"
scala> val s = Set(3,5,6)
s: scala.collection.immutable.Set[Int] = Set(3, 5, 6)
scala> List(34,234,5,6,3).map(s)
res3: List[Boolean] = List(false, false, true, true, true)
scala> List(34,234,5,6,3)(2)
res4: Int = 5
@danidiaz
danidiaz / gist:6321659
Created August 23, 2013 17:05
State monad: Lazy vs. Strict
-- Fails with Control.Monad.State.Strict
-- Works with Control.Monad.State.Lazy
evalState (undefined >> put 'a' >> get ) 'c'
-- See on SO: http://stackoverflow.com/questions/13186512/difference-between-haskells-lazy-and-strict-monads-or-transformers
@danidiaz
danidiaz / gist:6405694
Created September 1, 2013 16:54
generating a random vector
-- | Main entry point to the application.
module Main where
import qualified Data.Vector.Unboxed as V
import Control.Monad
import Control.Monad.Random
import System.Random
foovec :: V.Vector Int
@danidiaz
danidiaz / gist:6480065
Created September 7, 2013 22:43
intersperse skipSpace in attoparsec parsers
spcs ps = skipSpace *> foldr (*>) skipSpace (intersperse skipSpace ps)
parseKeywords = manyTill' (takeTill isEndOfLine *> endOfLine)
(_string "wt.contentGroup" *> _char '=' *> _char '{')
*> sepBy' (_skipMany1 digit *> _char ':' *> _parseQuot)
(_char ',')
where _string x = skipSpace *> string x
_char x = skipSpace *> char x
_skipMany1 x = skipSpace *> skipMany1 x
_parseQuot = _char '"' *> A.takeWhile isAlpha <* char '"'
@danidiaz
danidiaz / gist:6614635
Created September 18, 2013 19:52
Composing applicative functors.
import Control.Monad.Trans.List
import Control.Monad.Trans.Writer
import Control.Applicative
import Data.Functor.Compose
import Data.Monoid
tell' :: Monoid a => a -> Compose [] (Writer a) ()
tell' = Compose . pure . tell
list :: Monoid a => [x] -> Compose [] (Writer a) x
@danidiaz
danidiaz / gist:6619766
Created September 19, 2013 06:34
Composing the erros of different stages.
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE RankNTypes #-}
module Main where
import System.IO
import Control.Applicative
import Control.Monad
@danidiaz
danidiaz / gist:6734938
Last active December 24, 2015 03:08
Prism adventures
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Lens
import System.IO
-- Note that data constructors can't begin with underscore _
data Foo = A Int Int | B Char Char
makePrisms ''Foo