Skip to content

Instantly share code, notes, and snippets.

View pedrofurla's full-sized avatar

Pedro Furlanetto pedrofurla

View GitHub Profile
@pedrofurla
pedrofurla / download-so.hs
Created December 10, 2021 08:13
Extract Stackoverflow code blocks
{-# LANGUAGE LambdaCase #-}
{-
cabal v2-install --lib hxt
cabal v2-install --lib hxt-xpath
cabal v2-install --lib http-client
cabal v2-install --lib http-client-tls
-}
import Text.XML.HXT.Core (runX, readString, withParseHTML, withWarnings)
import GHC
import GHC.Paths
main =
do
putStrLn $ "libdir = " ++ libdir
(runGhc (Just libdir) $ execStmt "print $ 1+1" execOptions) >> pure ()
putStrLn "We are done"
λ> :{
> second (x:y:xs) = y : second xs -- returns every second element of a list
> second _ = []
>
> xs = [1,2,3,4] ++ second xs
> :}
λ> second [1,2,3,4]
[2,4]
λ> second $ second [1,2,3,4]
[4]
@pedrofurla
pedrofurla / vector.hs
Created April 15, 2021 00:52
Vectors Haskell and Scala
module Vector where
data Vector = Vector Int Int
add (Vector x0 y0) (Vector x1 y1) = Vector (x0+x1) (y0+y1)
inverse (Vector x0 y0) = Vector (-x0) (-y0)
subtract v0 = add v0 . inverse
-- the above is equivalent to `subtract v0 v1 = add v0 (inverse v1)
{-# LANGUAGE Rank2Types, ScopedTypeVariables #-}
newtype StateT s m a = StateT { runStateT :: s -> m (a, s) }
newtype ReaderT r m a = ReaderT { runReaderT :: r -> m a }
newtype Identity a = Identity { runIdentity :: a }
{- class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b -}
data MonadI m = MonadI {
λ> :t +v join @((->) Int)
join @((->) Int)
:: forall a. Monad ((->) Int) => (Int -> Int -> a) -> Int -> a
foldr @[] (
\elm -> \case
(k, i):rest -> if (elm == k) then (k,i+1):rest else (elm, 1):(k, i):rest
[] -> [(elm,1)]
) [] "aaaabbbcca"
@pedrofurla
pedrofurla / something.hs
Created January 20, 2021 03:48
reddit l0z4cz
{-# OPTIONS_GHC -fforce-recomp #-}
module V where
data LExpr = Var String -- Variable
| App LExpr LExpr -- Funktionsapplikation
| Lam String LExpr -- Lambda-Abstraktion
newVar :: [String] -> String
@pedrofurla
pedrofurla / file.scala
Created December 8, 2020 14:26
Stackoverflow 65178794 (AUX Pattern, path dependency)
sealed trait Kind
case object B extends Kind
case object C extends Kind
sealed trait Test {
type K <: Kind
val kind: K
}
@pedrofurla
pedrofurla / postgres_queries_and_commands.sql
Created April 9, 2020 02:15 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(clock_timestamp(), query_start), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
-- show running queries (9.2)
SELECT pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'