Skip to content

Instantly share code, notes, and snippets.

@dripolles
dripolles / LastParameterLambda.kt
Created January 14, 2019 14:52
Kotlin's lambda as a last parameter + varargs + higher order function
typealias Operator = (Int, Int) -> Int
fun reduce(zero: Int, vararg xs: Int): ((Operator) -> Int) {
var acc = zero
fun reductor(op: Operator): Int {
xs.forEach { acc = op(acc, it)}
return acc
}
return { reductor(it) }
}
@dripolles
dripolles / EuroPythonPodcast
Last active October 31, 2018 16:35
Django girs, django gays
Django girls, Django gays
(E: entrevistador. A y B son los entrevistados, no consigo identificarlos por su
voz al 100% con los que se presentan al principio del podcast)
- E: Tenéis pensada alguna actividad para fomentar una sociedad “más mejor”? Es
una pregunta un poco rara.
- A: Je, ¿una sociedad más mejor? Hombre, pues mira, ahí va a haber como es la
<no se entiende bien> Django Girls, los Django Gays, los Django... (se oyen
risas) de todos los colores (más risas). Digo yo, por aquello de las cosas
import Data.Number.CReal
nilakantha :: Int -> CReal
nilakantha n = 3 + (sum $ take n series) where
series = zipWith (\sign d -> (-1) * sign * 4 / d) (cycle [-1,1]) denominators
denominators = map (\x -> x * (x+1) * (x+2)) [2,4..] :: [CReal]
main = print $ nilakantha 150
@dripolles
dripolles / lowestFreeInt.hs
Created April 15, 2014 16:29
1HaskellADay 2014/04/15
-- http://lpaste.net/revision/5805301582249590784
import Control.Arrow
import Data.Set as S (Set, empty, insert, member)
lowestFreeInt :: [Int] -> Int
lowestFreeInt = fst . foldl f (0, S.empty) where
f (n,s) x = (if (n == x) then nextFreeInt (n+1) s else n, S.insert x s)
lowestFreeInt' :: [Int] -> Int
lowestFreeInt' = fst . foldl f (0, S.empty) where
@dripolles
dripolles / stagedComputation.hs
Created April 14, 2014 13:16
1HaskellADay 2014/04/14
-- http://lpaste.net/revision/6908970897281908736
import Control.Category ((>>>))
stagedComputation :: [a->a] -> a -> [a]
stagedComputation fs x = tail $ scanl (flip id) x fs
stagedComputation' :: [a->a] -> a -> [a]
stagedComputation' = flip $ scanl (flip id) >>> (tail .)
main = do
@dripolles
dripolles / replicateF.hs
Last active August 29, 2015 13:58
1HaskellADay 2014/04/08
-- http://lpaste.net/revision/3166527577426755584
import Data.Monoid
replicateF :: Int -> (a -> a) -> a -> a
replicateF n = ((flip (!!) n . ) . iterate)
replicateF' :: Int -> (a -> a) -> a -> a
replicateF' n = appEndo . mconcat . replicate n . Endo
main = do
@dripolles
dripolles / braid.hs
Created April 7, 2014 09:36
1HaskelADay 2014/04/07
-- http://lpaste.net/revision/7041447080467890176
import Control.Applicative
braid :: [a] -> [a] -> [a]
braid xs ys = zip xs ys >>= (:) <$> fst <*> return . snd
main = do
print $ braid [0,2] [1,3 ..]
@dripolles
dripolles / foo.hs
Created April 3, 2014 08:42
1HaskellADay 2014/04/03
import Control.Applicative
import Control.Arrow
foo :: (a -> b) -> [a] -> [(a,b)]
foo f = map ((,) <$> id <*> f)
foo' :: (a -> b) -> [a] -> [(a,b)]
foo' = map . (id &&&)
main = do
@dripolles
dripolles / empytIndices.hs
Created March 31, 2014 16:10
1HaskellADay 2014/03/31
import Data.Maybe
emptyIndices :: [Maybe a] -> [Int]
emptyIndices = map fst . filter (isNothing . snd) . zip [0..]
main = do
print $ emptyIndices [Just 1,Nothing,Just 2,Nothing,Nothing,Just 3]
@dripolles
dripolles / squareList.hs
Last active August 29, 2015 13:57
1HaskellADay 24/03/2014
-- http://codepad.org/rwuXZKKQ
import Data.List.Split
squareList :: Int -> a -> [a] -> [[a]]
squareList n z xs = let
(x', xs') = splitAt n $ xs ++ repeat z
in take n (x' : squareList n z xs')
squareList' :: Int -> a -> [a] -> [[a]]
squareList' n z xs = take n $ chunksOf n (xs ++ repeat z)