Skip to content

Instantly share code, notes, and snippets.

View djui's full-sized avatar

Uwe Dauernheim djui

View GitHub Profile
@djui
djui / 4clojure - 53
Last active December 15, 2015 04:50
#(second (reduce (fn [[t r] x]
(if (= (last t) (dec x))
[(conj t x) r]
(if (> (count t) (max 1 (count r)))
[[x] t]
[[x] r])))
[[] []] (conj % -1)))
@djui
djui / gapmap.clj
Last active December 15, 2015 14:49
gapmap function for Clojure
(defn gapmap
"Returns a lazy sequence consisting of the result of applying f to all
adjacent values of coll. f should be a function of 2 arguments. If coll
contains less than 2 items, it is returned and f is not called."
{:added "1.6"}
([f coll]
(if-let [r (next coll)] ; at least two items?
(lazy-seq (gapmap-internal f (first coll) r))
coll)))
@djui
djui / redcarpet_without_pygments.rb
Last active December 17, 2015 01:38
Jekyll plugin to allow prism.js for markdown code blocks. Markdown parsing is done using redcarpet.
module Jekyll
class RedcarpetWithoutPygmentsParser < Converter
safe true
priority :high
def initialize(config)
require 'redcarpet'
@config = config
@redcarpet_extensions = {}
(ns io.djui.misc)
;; Use case example for tree-seq
(defn- map-traverse
"Traverse a map with vectors in values as branches and return a list of key
x's values. Example:
{:a nil :x {:foo 1} :b [{:c nil :x 42} \"test\"]} => [{:foo 1} 42]"
[map-tree x]
(let [map-vals #(when (map? %) (vals %))
branch? (comp (partial some sequential?) map-vals)
;;
;; NS CHEATSHEET
;;
;; * :require makes functions available with a namespace prefix.
;;
;; * :use makes functions available without a namespace prefix
;; (i.e., refers functions to the current namespace).
;;
;; * :import refers Java classes to the current namespace.
;;
(let [xs (map #(Thread/sleep (* % 1000)) [1 9])]
(doseq [x xs]
(print x)))
@djui
djui / 30c3_downloader.sh
Last active January 1, 2016 23:49
Download all 30C3 presentations from Youtube and watch them offline. Dependencies: parallel, youtube-dl.
#!/bin/sh
youtube-dl -i -w -q https://www.youtube.com/playlist?list=PLBXmeocYXDfCFnkCoxkSpFChle3gmidEn
youtube-dl -i -w -q https://www.youtube.com/playlist?list=PLOcrXzpA0W82rsJJKrmeBlY3_MS0uQv3h
@djui
djui / saml.py
Last active November 30, 2023 00:26
Python class to fetch a website behind a SAML2 SSO service conforming to the Shibboleth protocol.
from __future__ import print_function
import cookielib
import getpass
import HTMLParser
import re
import sys
import urllib
import urllib2
(require '[com.keminglabs.zmq-async.core :refer [register-socket!]]
'[clojure.core.async :refer [>! <! <!! >!! go chan sliding-buffer close!]])
(let [addr "tcp://*:9999"
[s-in s-out] (repeatedly 2 #(chan (sliding-buffer 64)))]
(register-socket! {:in s-in :out s-out :socket-type :rep
:configurator (fn [socket] (.bind socket addr))})
(println "waiting messages...")
@djui
djui / cli_template.py
Last active October 7, 2015 13:27
Commandline Tool template for Python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import os
import sys
import traceback
__version__ = '1.0'