Skip to content

Instantly share code, notes, and snippets.

View jkpl's full-sized avatar

Jaakko Pallari jkpl

View GitHub Profile
@jkpl
jkpl / writing_tech_articles.md
Last active April 16, 2024 10:45
Writing tech articles

Writing tech articles

This is a description of how I write tech articles for various blogs. Hopefully someone else will find this useful as well.

Create a Gist for the article

When I begin writing a new article, I create a new [GitHub Gist][gist] for the article files. The Gist contains a file for the article text and code examples related to the article.

@jkpl
jkpl / Main.scala
Last active February 5, 2024 08:29
Ways to pattern match generic types in Scala
object Main extends App {
AvoidLosingGenericType.run()
AvoidMatchingOnGenericTypeParams.run()
TypeableExample.run()
TypeTagExample.run()
}
class Funky[A, B](val foo: A, val bar: B) {
override def toString: String = s"Funky($foo, $bar)"
}
@jkpl
jkpl / cdk-stack-deps-to-mermaid.js
Created January 15, 2024 12:56
CDK stack dependencies to Mermaid
#!/usr/bin/env node
/*
Convert a CDK app manifest file to a stack dependency tree in Mermaid format.
*/
const fs = require('fs');
function dependencyTreeInMermaid(data) {
const names = {};
const links = [];
@jkpl
jkpl / ksql-app.yaml
Created May 7, 2020 14:10
Example KSQL app deployment for Kubernetes
---
apiVersion: v1
kind: Service
metadata:
name: demo-ksql-server
labels:
app: demo-ksql-server
spec:
ports:
- port: 8088
@jkpl
jkpl / article.md
Last active October 17, 2023 13:45
Error handling pitfalls in Scala

Error handling pitfalls in Scala

There are multiple strategies for error handling in Scala.

Errors can be represented as [exceptions][], which is a common way of dealing with errors in languages such as Java. However, exceptions are invisible to the type system, which can make them challenging to deal with. It's easy to leave out the necessary error handling, which can result in unfortunate runtime errors.

@jkpl
jkpl / article.md
Last active March 21, 2023 21:18
HTML search and replace in Clojure

HTML search and replace in Clojure

In Clojure, data structures are mostly built from a handful of core data structures such as lists, vectors, maps, and sets. This means that most data structures can leverage all of the generic data transformation and querying functions built for the core data structures instead of having to rebuild the same functionality for each data structure. This feature in combination with Clojure's rich standard library makes Clojure very attractive for solving data munching problems from other domains.

In this article, I'm going to demonstrate these capabilities for solving HTML transformations using Clojure. First, I'm going to describe how HTML can be represented in Clojure. With this representation in mind, I'll demonstrate how we can transform HTML documents in Clojure. Finally, I'll tie the transformations together with the HTML parsing and formatting to produce a complete solution.

@jkpl
jkpl / Dockerfile
Last active February 13, 2023 15:22
saml2aws Docker image
FROM debian:stable-slim
RUN apt-get update && \
apt-get install -y ca-certificates curl && \
rm -rf /var/lib/apt/lists/*
ENV SAML2AWS_VERSION=2.16.0
ENV SAML2AWS_DOWNLOAD_URL=https://github.com/Versent/saml2aws/releases/download/v${SAML2AWS_VERSION}/saml2aws_${SAML2AWS_VERSION}_linux_amd64.tar.gz
RUN curl -L "$SAML2AWS_DOWNLOAD_URL" -o saml2aws.tar.gz && \
@jkpl
jkpl / utpl_download.sh
Created October 22, 2011 19:58
Script that downloads Youtube playlist and extracts the audio from the downloaded clips.
#!/bin/bash
## URL
# The URL to the Youtube playlist
URL="http://www.youtube.com/playlist?list=PL60BA016CD6B147F8"
## DESTPATH
# The path where to to download the music to
DESTPATH="$HOME/Music/awesome_new_music"
@jkpl
jkpl / article.org
Last active November 9, 2022 18:46
Enforcing invariants in Scala datatypes

Enforcing invariants in Scala datatypes

Scala provides many tools to help us build programs with less runtime errors. Instead of relying on nulls, the recommended practice is to use the Option type. Instead of throwing exceptions, Try and Either types are used for representing potential error scenarios. What’s common with these features is that they’re used for capturing runtime features in the type system, thus lifting the runtime scenario handling to the compilation phase: your program doesn’t compile until you’ve explicitly handled nulls, exceptions, and other runtime features in your code.

In his “Strategic Scala Style” blog post series,

#!/bin/sh
#
# This script deploys the given manifest,
# tracks the deployment, and rolls back on failure.
#
# First execute this with "myapp.yaml" and then try it with "myapp.failing.yaml"
#
MANIFEST_PATH=$1
DEPLOYMENT_NAME=myapp