Skip to content

Instantly share code, notes, and snippets.

View jadlr's full-sized avatar

Jonas Adler jadlr

View GitHub Profile
@jadlr
jadlr / pushbutton.ex
Last active November 14, 2023 14:51
`gen_statem` example rewritten to elixir (http://erlang.org/doc/design_principles/statem.html)
defmodule PushButton do
@behaviour :gen_statem
@name :pushbutton_statem
# Client API
def start do
:gen_statem.start({:local,@name}, __MODULE__, [], [])
end
@jadlr
jadlr / FS2TaskInterop.scala
Created October 2, 2020 12:36
Unexpected behaviour when using zio with fs2
import fs2.Stream
import zio.interop.catz._
import zio.{ Runtime, Task }
object FS2TaskInterop extends App {
val runtime = Runtime.default
val nonStream = Task(123).map(_ => BigDecimal("16:19:25.242056065Z"))
val stream = Stream.eval(nonStream)
@jadlr
jadlr / CryptoApp.scala
Created June 26, 2020 13:51
Using AES256GCM to encrypt and decrypt a text
import cats.effect.{ ExitCode, IO, IOApp }
import tsec.cipher.symmetric.{ AAD, AADEncryptor, IvGen }
import tsec.cipher.symmetric.jca.{ AES256GCM, SecretKey }
import tsec.common._
import tsec.cipher.symmetric._
object CryptoApp extends IOApp {
private val aad: AAD = AAD("KnQCxy4rW4KuxMRcvkVvAs9KufF7XR4b".utf8Bytes)
@jadlr
jadlr / StateTest.scala
Created February 17, 2020 08:13
Experimenting with the state monad to have a context in https4
import cats.data._
import cats.effect.ExitCode
import cats.implicits._
import cats.~>
import org.http4s.dsl.Http4sDsl2
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.{ Http, Request, Response }
import zio._
import zio.clock.Clock
import zio.interop.catz._
@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
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 / 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 {
@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 / 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)
module LeftPad where
leftPad :: Int -> Char -> String -> String
leftPad n c x
| n > length x = leftPad n c (c : x)
| otherwise = x