Skip to content

Instantly share code, notes, and snippets.

View mpenet's full-sized avatar
🪲
breeding bugs

Max Penet mpenet

🪲
breeding bugs
View GitHub Profile
<!doctype html>
<html>
<head>
<style>
@-webkit-keyframes relocate {
0% { top:16px; }
50% { top: 64px; }
100% { top: 16px; }
}
video{
@danneu
danneu / 1-codec.clj
Created November 10, 2013 11:58
a rough implementation/dump of the bitcoin wire protocol (codec.clj) and some usage examples (chan.clj). https://github.com/ztellman/gloss/issues/27
(ns blockdude.codec
(:require [clojure.string :as str]
[gloss.core :as gloss-core :refer :all
:exclude [byte-count]]
[blockdude.hash :as hash]
[blockdude.util :refer :all]
[gloss.core.codecs :refer [identity-codec]]
[gloss.io :refer :all
:exclude [contiguous decode encode]])
(:import [java.lang Character]
@maxlapshin
maxlapshin / setup_lager_logging.erl
Created November 26, 2013 12:22
How we configure lager for logging
setup_lager_logging() ->
application:load(lager),
ConsoleFormat = [time, " ", pid, {pid, [" "], ""},
{module, [module, ":", line, " "], ""},
message, "\n"
],
FileFormat = [date, " "] ++ ConsoleFormat,
LogDir = os:getenv("LOGDIR"),
@pyrtsa
pyrtsa / http-kit-vs-reason-phrase.clj
Created March 10, 2014 09:43
HTTP Kit still fails to handle empty Reason-Phrase in HTTP responses
(import 'org.httpkit.client.Decoder)
(import 'java.nio.ByteBuffer)
(.decode (Decoder. nil nil) (ByteBuffer/wrap (.getBytes "HTTP/1.1 200 \r\n" "UTF-8")))
;;=> ProtocolException not http protocol? HTTP/1.1 200 org.httpkit.client.Decoder.parseInitialLine (Decoder.java:77)
(pst)
;;; ProtocolException not http protocol? HTTP/1.1 200
;;; org.httpkit.client.Decoder.parseInitialLine (Decoder.java:77)
;;; org.httpkit.client.Decoder.decode (Decoder.java:88)
@renatoathaydes
renatoathaydes / HttpAsyncClientCreator.java
Created April 10, 2014 09:08
Creating and configuring a HttpAsyncClient
// more config options at http://hc.apache.org/httpcomponents-asyncclient-4.0.x/httpasyncclient/examples/org/apache/http/examples/nio/client/AsyncClientConfiguration.java
public RequestRunner provideRequestRunner( ComponentContext context, URI pageUri, Iterable<URI> assetUris )
throws IOException
{
try
{
IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
.setIoThreadCount( Runtime.getRuntime().availableProcessors() )
.setConnectTimeout( 30_000 )
@josiahcarlson
josiahcarlson / rate_limit.py
Last active February 25, 2022 14:33
Rate limiting with Redis primarily using Lua scripting
'''
rate_limit.py
Written May 7-8, 2014 by Josiah Carlson
Released under the MIT license.
Offers a simple interface for offering rate limiting on a per second, minute,
hour, and day basis. Useful for offering varying rate limits depending on user
behavior. Sliding window based limits offer per minute, hour, and day limits
RATE_LIMIT_SCRIPT = r'''
local now = tonumber(ARGV[1])
local required = tonumber(ARGV[2])
local rate = tonumber(ARGV[3])
local per_secs = tonumber(ARGV[4])
local do_subtract = tonumber(ARGV[5]) == 1
local full_at = tonumber(redis.call('GET', KEYS[1])) or 0
local score, result
if full_at < now then
score = rate
(require '[clojure.core.async :as a])
(def xform (comp (map inc)
(filter even?)
(dedupe)
(flatmap range)
(partition-all 3)
(partition-by #(< (apply + %) 7))
(flatmap flatten)
(random-sample 1.0)
@ghadishayban
ghadishayban / weighted_rand.clj
Last active September 23, 2022 08:15
Vose's alias method for weighted randoms
(ns weighted-rand
(:import clojure.lang.PersistentQueue))
(defprotocol Rand
(nextr [_ rng]))
;; Vose's alias method
;; http://www.keithschwarz.com/darts-dice-coins/
(deftype Vose [n ^ints alias ^doubles prob]
@ptaoussanis
ptaoussanis / transducers.clj
Last active December 8, 2024 03:24
Quick recap/commentary: Clojure transducers
(comment ; Fun with transducers, v2
;; Still haven't found a brief + approachable overview of Clojure 1.7's new
;; transducers in the particular way I would have preferred myself - so here goes:
;;;; Definitions
;; Looking at the `reduce` docstring, we can define a 'reducing-fn' as:
(fn reducing-fn ([]) ([accumulation next-input])) -> new-accumulation
;; (The `[]` arity is actually optional; it's only used when calling
;; `reduce` w/o an init-accumulator).