Skip to content

Instantly share code, notes, and snippets.

View jkpl's full-sized avatar

Jaakko Pallari jkpl

View GitHub Profile
@jkpl
jkpl / check_rules.sh
Created February 25, 2017 23:26
Check window class and name (rules) in Xorg
#!/bin/sh
xprop |awk '
/^WM_CLASS/{sub(/.* =/, "instance:"); sub(/,/, "\nclass:"); print}
/^WM_NAME/{sub(/.* =/, "title:"); print}'
@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 / 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,

@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 / postdump.py
Created April 6, 2016 12:23
Dump incoming POST requests to STDOUT
#!/usr/bin/env python
import tornado.ioloop
import tornado.web
import pprint
class MyDumpHandler(tornado.web.RequestHandler):
def post(self):
pprint.pprint(self.request)
pprint.pprint(self.request.body)
@jkpl
jkpl / mailapp.sh
Created April 2, 2016 13:18
mu4e mail composer
#!/bin/sh
# usage: mailapp.sh [url]
EMACS="/usr/bin/emacs"
EMACSCLIENT="/usr/bin/emacsclient"
SOCKET_NAME="email"
SOCKET_PATH="/tmp/emacs$(id -u)/$SOCKET_NAME"
log() {
@jkpl
jkpl / ResponseCollector.scala
Last active October 20, 2020 10:13
Reusable response collector in Akka
package collector
import akka.actor._
import akka.util.Timeout
import scala.concurrent.duration._
import scala.concurrent.{Future, Promise}
trait ResponseTracker[T] {
def addResponse(response: T): ResponseTracker[T]
def isDone: Boolean
@jkpl
jkpl / ghwh.php
Created February 16, 2016 19:00
Basic Github webhook
<?php
// Conf
$secret = 'mysecret';
$command = '/path/to/script.sh';
function fail($msg) {
http_response_code(400);
echo $msg;
exit(1);
@jkpl
jkpl / sharescrot.sh
Last active January 9, 2016 19:36
Share screenhots using generic web host
#!/bin/sh
remote_path="myhost:path/to/screenshots/"
seconds=$(date +%s)
filename="${seconds}.png"
tmpdir="/tmp"
tmpfile="$tmpdir/$filename"
public_base_path="https://example.org/screenshots"
public_path="$public_base_path/$filename"