This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ListPermutations where | |
listPermutations :: [a] -> Integer -> [[a]] | |
listPermutations _ 0 = [[]] | |
listPermutations [] _ = [] | |
listPermutations (x:xs) n = fmap (x:) (listPermutations xs (n - 1)) ++ listPermutations xs n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module LeftPad where | |
leftPad :: Int -> Char -> String -> String | |
leftPad n c x | |
| n > length x = leftPad n c (c : x) | |
| otherwise = x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
OlderNewer