Skip to content

Instantly share code, notes, and snippets.

@c4710n
c4710n / elixir-mix-format.sh
Created December 28, 2023 05:20
A simple wrapper to avoid mixing compile logs and formatting output.
#!/usr/bin/env bash
#
# A simple wrapper to avoid mixing compile logs and formatting output.
# 1. redirect output to /dev/null
# 2. avoid reading input from /dev/stdin, which will be used by `mix format`
mix compile > /dev/null 2>&1 </dev/null
exec mix format $@
@c4710n
c4710n / macos-network-flush-routing-table.sh
Created November 8, 2023 03:42
Fix macOS corrupted routing table
#!/usr/bin/env bash
#
# ## The Problem
#
# Can't assign requested address
#
# It occurs when macOS's TCP stack routing table has become corrupted.
#
# ## Reproducing
#
@c4710n
c4710n / elixir-format.el
Last active February 26, 2023 05:50
Alternative elixir-format in Emacs
(require 'reformatter)
(defcustom elixir-format--extra-args nil
"Extra arguments to pass to mix."
:group 'elixir-format
:type '(repeat string))
(defun elixir-format--mix-executable ()
(executable-find "mix"))
@c4710n
c4710n / mp3.exs
Last active July 8, 2022 09:12
A working example of calculating the duration of MP3 files in Elixir.
defmodule MP3 do
def get_duration(data) when is_binary(data) do
{_, rest} = parse_tag(data)
parse_frame(rest, 0, 0, 0)
end
defp parse_tag(<<
"ID3",
_major_version::integer,
_revision::integer,
@c4710n
c4710n / plug_overload_detector.ex
Created April 26, 2022 04:46
Check the rate of requests, and tag the corresponding connections with oveload tag.
defmodule PlugOverloadDetector do
@moduledoc """
Check the rate of requests, and tag the corresponding connections with
`conn.assigns.overload = true` when the rate is too high.
> The related package is ex_rated.
## Usage
### Use in routers
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
@c4710n
c4710n / benchmark-traverse-set-get.ex
Last active December 18, 2021 03:34
Benchmark - traverse map with `set` and `get` operation
Mix.install([
{:traverse, "~> 1.0"},
{:benchee, "~> 1.0"}
])
defmodule Data do
def create_sample(count) do
sample = %{
user_id: 1,
children: [
@c4710n
c4710n / focus-emacs-frame.applescript
Last active September 9, 2021 22:40
macOS: focus on emacs frame
#!/usr/bin/env osascript
#
# Focus on frame by the title of frame.
#
# Usage:
# $ focus-emacs-frame.applescript editor
# $ focus-emacs-frame.applescript terminal
on run argv
if argv is {} then
@c4710n
c4710n / position-window.applescript
Last active January 1, 2021 02:41
macOS: position window with builtin utilities
#!/usr/bin/env osascript
#
# Usage:
# $ ./position-window.applescript <position>
# Available postions:
# - left
# - right
# - full
# - center
#
@c4710n
c4710n / repo.ex
Last active November 22, 2022 09:13
Ecto: reverse a preload
defmodule Hello.Repo do
use Ecto.Repo,
otp_app: :hello,
adapter: Ecto.Adapters.Postgres
@doc """
Reverse a preload.
## Example