Skip to content

Instantly share code, notes, and snippets.

View jadlr's full-sized avatar

Jonas Adler jadlr

View GitHub Profile
@jadlr
jadlr / RateLimiter.scala
Created February 29, 2016 17:06
Reusable rate limiter for akka streams
import akka.stream.FlowShape
import akka.stream.scaladsl.{Flow, GraphDSL, Source, Zip}
import scala.concurrent.duration.FiniteDuration
object RateLimiter {
private def rateLimiter[A](rate: FiniteDuration) = {
case object Tick
@jadlr
jadlr / ListPermutations.ex
Last active February 22, 2016 11:19
Permutations of a certain length of a list in elixir (v2)
defmodule ListPermutations do
def list_permutations(_, 0), do: [[]]
def list_permutations([], _), do: []
def list_permutations([h|t], n), do: Enum.map(list_permutations(t, n-1), fn(l) -> [h|l] end) ++ list_permutations(t, n)
end
@jadlr
jadlr / ListPermutations.hs
Last active February 22, 2016 11:06
Permutations of a certain length of a list in haskell (v2)
module ListPermutations where
listPermutations :: [a] -> Integer -> [[a]]
listPermutations _ 0 = [[]]
listPermutations [] _ = []
listPermutations (x:xs) n = fmap (x:) (listPermutations xs (n - 1)) ++ listPermutations xs n
@jadlr
jadlr / ListPermutations.hs
Last active February 20, 2016 23:09
Permutations of a certain length of a list
module ListPermutations where
listPermutations :: Integer => [a] -> b -> [[a]]
listPermutations l n = snd $ permutate l n
where permutate :: Integer => [a] -> b -> ([[a]], [[a]])
permutate [] _ = ([[]], [])
permutate (x:xs) n = let (pt, acc) = permutate xs n in permutateInner x pt pt acc n
where permutateInner :: Integer => a -> [[a]] -> [[a]] -> [[a]] -> b -> ([[a]], [[a]])
permutateInner _ [] pt acc _ = (pt, acc)
permutateInner h (x:xs) pt acc n