Skip to content

Instantly share code, notes, and snippets.

View jadlr's full-sized avatar

Jonas Adler jadlr

View GitHub Profile
@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
@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.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 / 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
module LeftPad where
leftPad :: Int -> Char -> String -> String
leftPad n c x
| n > length x = leftPad n c (c : x)
| otherwise = x
@jadlr
jadlr / FizzBuzz.hs
Last active April 6, 2016 09:15
fizzbuzz in haskell
{-# LANGUAGE ParallelListComp #-}
module FizzBuzz where
-- https://en.wikipedia.org/wiki/Fizz_buzz
fizzBuzz :: [String]
fizzBuzz = take 100 $ go 1
where go n
| n `mod` 15 == 0 = "fizzbuzz" : go (n + 1)
@jadlr
jadlr / Repdigit.hs
Created July 19, 2016 20:24
Repdigit sequence calculation in haskell
module Repdigit where
-- https://oeis.org/A010785
repdigit :: Integer -> Integer
repdigit n = (n - 9 * floor ((fromInteger n - 1) / 9)) * (10 ^ floor ((fromInteger n + 8) / 9) - 1) `quot` 9
repdigits :: [Integer]
repdigits = go 0
where go n = repdigit n : go (n + 1)
@jadlr
jadlr / Max.scala
Created May 10, 2017 08:22
Slowly relearning Scala syntax
import java.util.NoSuchElementException
import cats._
import cats.implicits._
import scala.annotation.tailrec
object Max extends App {
def maximum[A : Order](list: List[A]): A = list match {
defmodule BatailleRoyale do
@cards 0..7 |> Enum.to_list |> List.duplicate(4) |> List.flatten
def times(n) do
results =
1..n
|> Enum.map(fn _ -> play() end)
|> Enum.map(fn {_, _, moves} -> moves end)
@jadlr
jadlr / pushbutton_handle_events.ex
Created December 9, 2016 13:19
`gen_statem` example rewritten in elixir using only handle_event functions
defmodule PushButtonHandleEvents do
@behaviour :gen_statem
@name :pushbutton_statem
# Client API
def start do
:gen_statem.start({:local,@name}, __MODULE__, [], [])
end