Skip to content

Instantly share code, notes, and snippets.

View joladev's full-sized avatar

Johanna Larsson joladev

View GitHub Profile
@joladev
joladev / manager.ex
Created February 7, 2020 10:10
Example of keeping ETS tables alive through process failures
defmodule Manager do
use GenServer
def start_link(_) do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
def init(_) do
Process.flag(:trap_exit, true)
worker = Process.whereis(Worker)
@joladev
joladev / iolist_benchmark.exs
Last active February 5, 2020 13:24
A benchmark comparing building a string through repeated concatenation vs using an iolist and `Enum.reverse/1` + `:erlang.iolist_to_binary/1`
defmodule IOList do
@base62_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" |> String.graphemes()
def random_chars(n) do
Enum.take_random(@base62_alphabet, n) |> Enum.join()
end
def with_iolist(input), do: with_iolist(input, [])
def with_iolist("", acc), do: acc |> Enum.reverse() |> :erlang.iolist_to_binary()
def with_iolist(<<first::utf8, rest::binary>>, acc) do
@joladev
joladev / phx.diff
Created October 26, 2019 19:26
Git diff of phoenix 1.4.9 and 1.4.10
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e515143..36a412c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,6 +24,19 @@ Note the websocket/longpoll configuration given to socket/3 will only apply afte
The old APIs for building transports are also deprecated. The good news is: adapting an existing transport to the new API is a less error prone process where you should mostly remove code.
+## 1.4.10 (2019-09-05)
+
@joladev
joladev / rot.ex
Last active August 15, 2019 05:40
@alphabet_lower ?a..?z
@alphabet_upper ?A..?Z
@spec rotate(text :: String.t(), shift :: integer) :: String.t()
def rotate(text, shift) do
text
|> String.to_charlist()
|> Enum.map(&(rot(&1, shift)))
|> List.to_string()
end
export const setIn = (obj: Object, key: string, value: string): Object => {
const levels = key.split('.');
const innerMostKey = levels.pop();
const innerMost = { [innerMostKey]: value };
const m = levels.reverse().reduce(
(acc, curr) => ({
[curr]: acc,
}),
@joladev
joladev / jsintro.md
Last active July 4, 2016 20:32
JavaScript Intro

Discipline

Java and C# are great at forcing you to do the right thing. JavaScript is not. It requires discipline.

This means choosing good and informative variable names, creating a clear and understandable architecture, and a flow of code that makes sense.

Global scope

Why this is a terrible, terrible design choice.

(def combinations
(for [x (range 1 98) y (range 1 98)
:when (>= 98 (+ x y))]
(let [z (- 100 x y)]
{:x x :y y :z z})))
(defn winner? [{:keys [x y z]}]
(== 100 (+ (* 15 x) (* 1 y) (* 0.25 z))))
(defn find-winner []
// Throttle from underscore.js, simplified and deattached
var throttle = function(func, wait) {
var context, args, result;
var timeout = null;
var previous = 0;
var later = function() {
previous = Date.now();
timeout = null;
result = func.apply(context, args);
@joladev
joladev / placeholder.js
Last active January 4, 2016 02:59
Faking placeholder in IE8
/**
* Set the "data-placeholder" attribute on your input fields.
* The script is only run if placeholders are not supported.
* Depends on jQuery >= 1.8
**/
(function (window, $) {
var e = document.createElement('input');
if (!('placeholder' in e)) {
$('input[data-placeholder]').each(function (idx, element) {
$(element).val($(element).attr('data-placeholder'));