Skip to content

Instantly share code, notes, and snippets.

View Dierk's full-sized avatar

Dierk König Dierk

View GitHub Profile
@Dierk
Dierk / Perceptron.groovy
Created November 14, 2021 20:23
Standard learning algorithm for a Perceptron that servers as a linear classifier
// Perceptron
// see https://towardsdatascience.com/perceptron-learning-algorithm-d5db0deab975
def weights = [0, 0, 0] // b, xCoeff, yCoeff such that classifier will become b + xCoeff * x + yCoeff * y == 0
def data = [
[1, 0, 0, false], // bias (start with 1), x, y, classification value
[1, 1, 0, true], // here: training the "or" function. Expected: [-1, 1, 1]
[1, 1, 1, true],
[1, 0, 1, true]
]
@Dierk
Dierk / Advent9.groovy
Created December 9, 2017 14:30
Advent of Groovy code, day 9
// see challenge here: http://adventofcode.com/2017/day/9
String input = /your input here/
String collapseEscape = input.replaceAll(/!./,'')
String collapseGarbage = collapseEscape.replaceAll(/<.*?>/,'')
String listString = collapseGarbage.tr('{}','[]')
String normCode = listString.replaceAll(/\[,/,'[')
@Dierk
Dierk / Advent8.groovy
Created December 8, 2017 13:34
Advent of Groovy code, day 8
// http://adventofcode.com/2017/day/8
def input = '''b inc 5 if a > 1
a inc 1 if b < 5
c dec -10 if a >= 1
c inc -20 if c == 10'''
regs =[:].withDefault{0}
inc = { String key, Integer x -> regs[key] += x }
dec = { String key, Integer x -> inc(key, -x) }
@Dierk
Dierk / Advent6.fr
Last active December 8, 2017 09:18
Advent of Frege code, day 6
module Advent6 where
{-
http://adventofcode.com/2017/day/6
-}
import Data.List
mapAtIndex :: (a->a) -> Int -> [a] -> [a]
mapAtIndex f n xs = take n xs ++ rest (drop n xs) where
@Dierk
Dierk / Advent3.fr
Created December 4, 2017 11:17
Advent of Frege code, day 3
module Advent3 where
{-
http://adventofcode.com/2017/day/3
-}
import Data.List
data Direction = Right | Up | Down | Left
derive Show Direction
@Dierk
Dierk / Advent2.fr
Created December 3, 2017 17:03
Advent of Frege code, day 2
module Advent2 where
{-
http://adventofcode.com/2017/day/2
-}
-- the core of the checksum logic
checkSum xss = sum (map lineCS xss) where
lineCS xs = maximum xs - minimum xs
@Dierk
Dierk / Advent1.groovy
Created December 2, 2017 22:45
Advent of Groovy code, 2017, day 1
// http://adventofcode.com/2017/day/1
def captcha(list) {
def last = list[-1]
def clean = list.findAll { e ->
if (e == last) {
last = e; true
} else {
last = e ; false
}
@Dierk
Dierk / Leak.fr
Created September 16, 2017 12:43
example of a possible space leak in lazy languages like Frege when running on a platform like JVM
-- Leak example from http://homepages.inf.ed.ac.uk/wadler/papers/leak/leak.ps
-- convenience
parseStr :: String -> (String, String)
parseStr = packedPair . parse . unpacked where
packedPair (x,y) = (packed x, packed y)
parse :: [Char] -> ([Char], [Char])
parse [] = ([], [])
parse (' ': cs) = ([], cs)
@Dierk
Dierk / Balanced.purs
Created September 14, 2017 11:30
The balanced parentheses kata with two solutions in purescript
module Balanced where
import Prelude (Unit, discard, ($), (+), (-), (>=), (<=), (&&), (==), negate)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)
import Data.Foldable (traverse_, foldMap)
import Control.Monad.State (State, execState)
import Control.Monad.State.Class (modify)
import Data.String (toCharArray)
module FizzBuzz
import Data.Vect
Rule : Type
Rule = (n: Int) -> (old: List String) -> (List String)
everyNthRule : (every: Int) -> (replacement: String) -> Rule
everyNthRule every replacement n old =
if (mod n every == 0) then old ++ [replacement] else old