Skip to content

Instantly share code, notes, and snippets.

View ku1ik's full-sized avatar
👋

Marcin Kulik ku1ik

👋
View GitHub Profile
@ku1ik
ku1ik / poolboy_demo.ex
Created May 8, 2017 00:08 — forked from henrik/poolboy_demo.ex
Example of using Poolboy in Elixir to limit concurrency (e.g. of HTTP requests).
defmodule HttpRequester do
use GenServer
def start_link(_) do
GenServer.start_link(__MODULE__, nil, [])
end
def fetch(server, url) do
# Don't use cast: http://blog.elixirsips.com/2014/07/16/errata-dont-use-cast-in-a-poolboy-transaction/
timeout_ms = 10_000
@ku1ik
ku1ik / cleanup-input-stream.clj
Last active March 1, 2017 16:01
InputStream wrapper which calls given callback after the stream is closed.
(ns example
(:import java.io.FilterInputStream))
(defn cleanup-input-stream
"InputStream wrapper which calls given callback after
the stream is closed."
[is cleanup]
(proxy [FilterInputStream] [is]
(close []
(proxy-super close)
(ns cheshire.experimental
(:require [cheshire.core :refer :all]
[clojure.java.io :refer :all])
(:import (java.io ByteArrayInputStream FilterInputStream
SequenceInputStream)))
(defn escaping-input-stream
[is]
(let [new-is (proxy [FilterInputStream] [is]
(read
@ku1ik
ku1ik / benchmark.md
Last active February 8, 2017 15:07
Naive comparison of reduce/sum performance of Clojure 1.8, ClojureScipt (Planck 2.0, Lumo 1.1.0), Ruby 2.4, JRuby 9.1.7.0 and Elixir 1.3.2

Naive micro-benchmark of reduce/sum I made to get the idea of raw performance range of different dynamic languages / different runtimes.

For each of these I repeated the execution until the timing stopped changing in major way (JIT etc), and saved only the last run.

Don't take it too seriously ;)

/* Based on Monokai from base16 collection - https://github.com/chriskempson/base16 */
.asciinema-theme-monokai .asciinema-terminal {
color: #f8f8f2;
background-color: #272822;
border-color: #272822;
}
.asciinema-theme-monokai .fg-bg {
color: #272822;
}
.asciinema-theme-monokai .bg-fg {
@ku1ik
ku1ik / danny.datalog
Created January 26, 2017 11:49
"Find actors older than Danny Glover"
[:find ?actor
:where
[?d :person/name "Danny Glover"]
[?d :person/born ?danny-age]
[?a :person/name ?actor]
[?a :person/born ?actor-age]
[?m :movie/cast ?a]
[(< ?actor-age ?danny-age)]]
@ku1ik
ku1ik / web-controllers-doc_controller.ex
Last active June 24, 2017 16:19
Nested layouts in Phoenix
defmodule Asciinema.DocController do
use Asciinema.Web, :controller
def show(conn, _params) do
conn
|> put_layout(:docs)
|> render("show.html")
end
end
nREPL: Establishing direct connection to localhost:32323 ...
nREPL: Direct connection established
Connected. Clojure isn't a language, it's a building material.
Error in `cider-repl--state-handler': (void-function seq-find)
error in process filter: Symbol's function definition is void: seq-find [2 times]
(defn split-coll [elem coll]
(loop [coll coll
parts []
part []]
(if-let [e (first coll)]
(if (= e elem)
(recur (rest coll) (conj parts part) [])
(recur (rest coll) parts (conj part e)))
(if (seq part)
(conj parts part)
(defmacro events [one two & items] (prn one two) (set items))
(events 1 2 3 4 5 6) ; => #{3 4 5 6}
1 2 ; <- printed