Skip to content

Instantly share code, notes, and snippets.

@gausby
gausby / twitter.txt
Last active September 15, 2021 18:53
Begin the morning by saying to thyself, I shall meet with the
busy-body, the ungrateful, arrogant, deceitful, envious, unsocial. All
these things happen to them by reason of their ignorance of what is
good and evil. But I who have seen the nature of the good that it is
beautiful, and of the bad that it is ugly, and the nature of him who
does wrong, that it is akin to me, not only of the same blood or seed,
but that it participates in the same intelligence and the same portion
of the divinity, I can neither be injured by any of them, for no one
can fix on me what is ugly, nor can I be angry with my kinsman, nor
hate him, For we are made for co-operation, like feet, like hands,
(use-package eshell
:after projectile
:preface
(defun universal-argument-eshell ()
"wrap the `eshell'-command, with a check for whether or not
the universal argument has been applied, and how many times.
Zero times: normal behavior (eshell); Once: open a shell in the
current project root"
(interactive)
(cond ((equal current-prefix-arg nil)
@gausby
gausby / shell.nix
Created January 7, 2020 17:03
Start a nix shell with Elixir 1.10.0-rc.0
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
let
inherit (lib) optional optionals;
erlang_wx = erlangR21.override {
wxSupport = true;
};
@gausby
gausby / README.org
Last active July 17, 2022 14:19
Short presentation on the benefits of using structs for process state management

Structs Our Friends

Introduction

Just to get everyone up to speed; Structs are a great because they provide compile time checks. Compile-time checks are awesome as we can catch errors before we run our application in the run-time. Structs also allow us to make custom data types, and we can implement protocols that amongst other things allow us to tell Elixir how it should enumerate over our data type, or insert items into our data type using Protocols.

test "take and free" do
mutex_pid = Mutex.start()
parent = self()
assert :ok = Mutex.wait(mutex_pid)
spawn_link(fn() ->
send parent, :ready
:ok = Mutex.wait(mutex_pid)
send parent, :done
defmodule MqttTest.Handler do
@moduledoc false
require Logger
defstruct [:name]
alias __MODULE__, as: State
@behaviour Tortoise.Handler
@gausby
gausby / advice-flyspell-auto-correct-word-to-read-out-loud.el
Last active November 14, 2017 15:41
Custom flyspell-insert-function that will read the word out loud
;; Read the inserted word out loud using the macOS speech synthesizer
;; when a misspelled word is corrected using the flyspell
;; `flyspell-auto-correct-word'-command.
;;
;; Note this only work on macOS. It should be fairly easy to change to
;; work with other command line interface speech synthesize systems.
;;
;; In a previous version this used to be a defadvice, but I've changed
;; it to implementing a custom flyspell insert function. This seems to
;; work a bit more reliably.
@gausby
gausby / dice.ex
Last active January 19, 2017 20:56
Probably not the best we can come up with!
defmodule Dice do
@moduledoc """
The best I could come up with...
iex> import Dice
...> ~d'1d6'+3
"""
def sigil_d(input, []) do
[n, eyes] =
@gausby
gausby / counting_on_bits_in_a_bitfield.ex
Created September 15, 2016 09:21
Counting the on bits in a bitfield using really bit integers as the data representation
# ...
def has(bitfield) do
count_elements(bitfield.pieces, 0)
end
defp count_elements(0, acc), do: acc
defp count_elements(pieces, acc) do
count_elements(pieces >>> 1, acc + (pieces &&& 1))
end
[1,0,1,1,1] |> Enum.reduce(0, fn
1, acc -> acc * 2 + 1
0, acc -> acc * 2
end)