Skip to content

Instantly share code, notes, and snippets.

View regiskuckaertz's full-sized avatar

Regis Kuckaertz regiskuckaertz

View GitHub Profile
@regiskuckaertz
regiskuckaertz / adt.scala
Created July 14, 2021 09:52
Basic ADT stuff
/*
L :== integer
| L + L
| L * L
| (L)
| L / L
*/
val hosts: List[HttpHost] = ???
val client = new RestHighLevelClient(
RestClient.builder(hosts: _*)
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback {
def customizeHttpClient(httpClientBuilder: HttpAsyncClientBuilder): HttpAsyncClientBuilder =
httpClientBuilder.addInterceptorLast(new RequestAcceptEncoding())
})
)
@regiskuckaertz
regiskuckaertz / Perlin.hs
Last active July 11, 2024 13:14
Simple perlin noise
module GenArt.Perlin
( noise
) where
import Data.Array
import Data.Bits ((.&.))
permutation :: Array Int Int
permutation = listArray (0, 255)
[ 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225
@regiskuckaertz
regiskuckaertz / sudoku.md
Last active July 11, 2024 13:22
Sudoku: Twin exclusions

In this round of optimisations we will apply another common technique for making progress in a Sudoku. It is also a good opportunity to explore a couple of additional data structures.

Here are some stats from the first round of optimisations:

➜ head -n100 sudoku17.txt | time ./Sudoku
...
./Sudoku  71.92s user 0.55s system 99% cpu 1:12.78 total
@regiskuckaertz
regiskuckaertz / sudoku.md
Last active July 11, 2024 13:18
Sudoku: one step at a time

Prelude

In order to run your program and get some meaningful statistics:

  • download this list of Sudoku puzzles
  • set up a simple main that will run your sudoku against each line of the file:
main = do
  matrices <- lines <$> getContents
@regiskuckaertz
regiskuckaertz / sudoku.hs
Last active July 11, 2024 13:30 — forked from Ap0c/sudoku.hs
Implementation of sudoku in Haskell, from the FP reading group.
module Sudoku where
type Row a = [a]
type Matrix a = [Row a]
type Digit = Char
type Grid = Matrix Digit
-- sample3 is unsolvable
sample1, sample2, sample3 :: Grid
sample1 = ["2....1.38"
@regiskuckaertz
regiskuckaertz / MSS.md
Last active July 27, 2018 13:42
Practicing derivations: example #1

Thirty years ago, Jon Bentley wrote "Programming Pearls", a collection of great (imperative) algorithms. One of them was to find the maximum segment sum:

Given a list of numbers, the task is to compute the largest possible sum of a consecutive segment.

The "pearls" schtick caught on in the FP community, which came up with a research paper (of course) called "Functional pearls". In his book "Pearls of functional algorithm design", Richard Bird calculates an efficient version by derivation.

It starts with the specification:

@regiskuckaertz
regiskuckaertz / Derivations.md
Created July 27, 2018 12:42
Practicing derivations

Derivations are the bread and butter of functional programming. It is easy to fall into the trap of thinking it is a pointless academic exercise, which is the reason why a lot of people struggle with FP. Functional programs are not so much written, they're calculated. It is here that the true power of equational reasoning (i.e. being able to swap the left and right parts of an equation everywhere in the code) help us.

So, getting comfortable reading and doing derivations is a must for any FP practitioner.

Below are the ones we did in the previous sessions, plus a couple of exercises.

Fold fusion

@regiskuckaertz
regiskuckaertz / levenshtein.hs
Last active July 10, 2018 15:16
Edit distance with the Wagner-Fischer algorithm
module Levenshtein where
pairs :: [a] -> [(a,a)]
pairs [x,y] = [(x,y)]
pairs (x:y:xs) = (x,y) : pairs (y:xs)
vowels :: String
vowels = "aeiou"
cost :: Rational -> Rational -> Rational -> Char -> Char -> Rational
@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(_))
}