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 / cmdargs.hs
Created February 5, 2014 23:30
Example use of cmdargs.
import System.Console.CmdArgs.Explicit
main :: IO ()
main = do
let flags = [Flag ["l","lj"] FlagReq (\s -> Right . (s:)) "type" "fooo"]
arguments :: Mode [String]
arguments = mode "foobar" [] "helpy" (Arg (const Right) "ff" False) flags
xs <- processArgs arguments
print $ helpText [] HelpFormatDefault arguments
mapM_ putStrLn xs
@danidiaz
danidiaz / optparse33.hs
Created February 6, 2014 01:02
optparse: some and many.
main :: IO ()
main = do
let files :: O.Parser [String]
files = O.helper <*> O.arguments1 (Just) (O.metavar "logfile" <> O.help "one or several log files")
foo = some $ O.strOption
( O.long "output"
<> O.short 'o'
<> O.metavar "FILENAME" )
parserInfo = O.info ((,) <$> files <*> foo) (O.fullDesc)
prefs = O.prefs O.showHelpOnError
@danidiaz
danidiaz / lensnprism.hs
Created February 7, 2014 21:04
Defining lenses and prisms without depending on lens.
streams :: forall f. Functor f => ((StdStream,StdStream,StdStream) -> f (StdStream,StdStream,StdStream)) -> CreateProcess -> f CreateProcess
streams f c = setStreams c <$> f (getStreams c)
where
getStreams c = (std_in c,std_out c, std_err c)
setStreams c (s1,s2,s3) = c { std_in = s1
, std_out = s2
, std_err = s3
}
pipe3 :: (StdStream,StdStream,StdStream)
:t view (_Unwrapping fromList) [('a','b')]
-- view
getConst . _env Const $ shell "foo.bat"
-- over
runIdentity . _1 (Identity . (+1)) $ (1,1)
-- a fold (?)
getConst . _Nothing (\x -> Const [x]) $ Just 'a'
-- a prism (?)
:m + System.Process System.Process.Streaming Control.Concurrent.Async
:set prompt #
let z = (async (execute (proc "gvim" []) show $ separate purge purge)) >>= \a -> threadDelay (10^6) >> cancel
a <- async $ execute (proc "gvim" []) show $ separate purge purge
cancel a
@danidiaz
danidiaz / depparse.hs
Last active August 29, 2015 13:56
Reading dependently-sized vectors.
-- http://www.reddit.com/r/haskell/comments/1z8wsv/prove_your_haskell_for_great_safety_part_i/cfrqwdf
-- http://hackage.haskell.org/package/monomorphic-0.0.3.0/docs/Data-Type-Monomorphic.html
fromList :: [a] -> Monomorphic (Vector a)
fromList [] = Monomorphic Nil
fromList (x : xs) =
case fromList xs of
Monomorphic vs -> Monomorphic $ x :- vs
readVector :: Read a => String -> Monomorphic (Vector a)
@danidiaz
danidiaz / sotime.hs
Created March 9, 2014 09:36
so response
{-# LANGUAGE OverloadedStrings #-}
import Data.Monoid
import Data.AdditiveGroup
import qualified Data.ByteString as B
import qualified Data.ByteString.Builder as BB
import qualified Data.ByteString.Lazy as BL
--import Data.Time.Clock
import Data.Thyme.Clock
import Data.Thyme.Clock.POSIX
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class Main {
public Main() {
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Npgsql;
using CommandLine;
using CommandLine.Text;
// http://commandline.codeplex.com/
namespace Project1