Skip to content

Instantly share code, notes, and snippets.

View regiskuckaertz's full-sized avatar

Regis Kuckaertz regiskuckaertz

View GitHub Profile
@regiskuckaertz
regiskuckaertz / PlayJsonDynamoFormat.scala
Created July 3, 2018 11:21
Scanamo format for streamlining JSON values to strings
object PlayJsonDynamoFormat {
implicit val format: DynamoFormat[JsValue] = DynamoFormat.xmap[JsValue, String](
x => Try(Json.parse(x)) match {
case Success(y) => Right(y)
case Failure(f) => Left(TypeCoercionError(t))
}
)(Json.stringify(_))
}
@regiskuckaertz
regiskuckaertz / foldr.md
Last active July 4, 2018 13:33
Derivations and partiality

We saw that every data type is inhabited by a value ⊥ pronounced "bottom". It is very important in a non-strict language such as Haskell to represent how much knowledge you have about a value. You can either know exactly what the value is, know only a part of it, or know nothing at all. Bottom represents the latter, but you could as well only know that a list starts with 1, i.e. 1 : ⊥.

In fact, all data types in Haskell respect an approximation function , where x ⊑ y means "x is an approximation of y". For example with booleans, x ⊑ y iff ⇔ x ≡ ⊥ or x ≡ y in other words either you don't know anything about a particular boolean, or you know exactly what it is. This approximation ordering is important when reasoning about recursive algorithms, which is why it is important to understand the notion of "nothingness" in Haskell.

@regiskuckaertz
regiskuckaertz / Pascal.md
Created June 22, 2018 14:14
Reverse-Then-Add sequence

Let's see what Wolfram Alpha has for us:

RTA is the sequence obtained from reversing the digits of a number n and adding the result to the original number. The 196-algorithm operates RTA iteratively until a palyndromic number is obtained.

Let's define alg196 then. We can take the definition above literally and get the following script:

alg196 :: Integer -> [Integer]
alg196 n = takeWhile palyndromic . rtaseq n

Today we did two derivations to flex our type muscles.

From type to expression

What is the definition of foo given its type:

foo :: Functor f => a -> f (x, b) -> f (x, a)

Well, we know that it takes two arguments, a value which we know nothing about, and another which has two qualities: (1)

> module Anagrams where
Here is our specification:
Describe how you would go about designing `anagrams :: Int -> [Word] -> String` so that anagrams n
takes a dictionary (a sorted list of English words), extracts the n-letter ones and prints a string
that gives a list of the anagram entries for the n-letter words. Assume `type Word = String`.
Because `Word` is a type in the Prelude, we need to hide it from scope. That's how it's done:
series :: Int -> [Int]
series n = go where go = n : (zipWith (+) go $ fmap mirror go)
mirror :: Int -> Int
mirror = undigits . digits
digits :: Int -> [Int]
digits = map (`mod` 10) . takeWhile (/= 0) . iterate (`div` 10)
undigits :: [Int] -> Int
package example
import cats._
import cats.free._
import cats.data._
import cats.syntax.flatMap._
import cats.effect.{LiftIO, IO}
object Example {
import functors._
import Control.Monad.State
-- first version
toh n = fst $ tohmoves n (n, 0, 0)
tohmoves :: Int -> (Int, Int, Int) -> (Int, (Int, Int, Int))
tohmoves 1 (a, b, c) = (1, (a - 1, b, c + 1))
tohmoves n (a, b, c) =
let (n', (a', c', b')) = tohmoves (n - 1) (a, c, b)
(n'', (a'', b'', c'')) = tohmoves 1 (a', b', c')
module Main where
--------------------------------------------------------------------------------
import Data.HeytingAlgebra
import Prelude
import Data.Array (filter, any, (:), uncons)
import Data.Either (Either(..))
import Data.Maybe (Maybe(..))
import Data.Profunctor (class Profunctor, arr)
import Data.Profunctor.Choice (class Choice, left)
@regiskuckaertz
regiskuckaertz / Main.purs
Created February 18, 2018 09:24
Instance function not in scope?
module Main where
import Prelude
import Data.Either (Either(..))
import Data.Profunctor (class Profunctor, arr)
import Data.Profunctor.Choice (class Choice)
import Data.Profunctor.Strong (second)
import Data.Tuple (Tuple(..))
-- State transformers