Skip to content

Instantly share code, notes, and snippets.


Julius Beckmann h4cc

  • ZENNER IoT Solutions
  • Hamburg, Germany
View GitHub Profile
View my_binary_parser.ex
defmodule MyBinaryParser do
# Parsing a 2 byte binary to float16
# according to:
# Float16 is build in OTP 24.
def parse_float16(<<sign::1, exponent::5, significand::10>>) do
case {sign, exponent, significand} do
{0b0, 0b00000, 0b0000000000} ->
h4cc / ecto_paginate_query.ex
Created Feb 6, 2019
Elixir Ecto paginate Query with callback
View ecto_paginate_query.ex
defmodule MyEcto do
# Helper to paginate a query
def paginate_query(%Ecto.Query{} = query, per_page, current_page, callback) when per_page > 0 and current_page >= 0 and is_function(callback) do
import Ecto.Query
offset = per_page * current_page
|> limit(^per_page)
|> offset(^offset)
|> LongRepo.all()
h4cc / helper.ex
Created Apr 11, 2018
Helper function for put_in for populating data in deep maps.
View helper.ex
defmodule Helper do
# Helper function for setting values deep in a map.
# Thanks @michalmuskala!
@doc """
Will set value at for keys deep inside data.
iex> Helper.put_in_deep(%{a: %{}}, [:a, :b, :c], 42)
h4cc / Makefile
Last active Jul 11, 2021
Ubuntu 18.04 Bionic Beaver - Basic packages i usually install
View Makefile
# Ubuntu 18.04 (Bionic Beaver)
# Basic packages i usually install.
# Author: Julius Beckmann <>
# Upgraded Script from 17.04:
# Upgraded Script from 16.04:
View inline_lookup.ex
defmodule InlineLookup do
# Examples how to handle a static lookup table in Elixir.
def example1(x) do
%{1 => "a", 2 => "b", 3 => "c"}[x]
# Could also be a attribute
# Should be the same as:
Map.get(%{1 => "a", 2 => "b", 3 => "c"}, x, nil)
View ambipi.txt
AmbiPI:/ # cat /storage/
if [ `service hyperion status | grep running | wc -l` = 1 ]
hyperion-remote -c black
sleep 3
sudo service hyperion stop
sudo service hyperion start
h4cc /
Created Nov 25, 2017
Updating a Ubuntu LTS version to the next available. Used for vServer with a broken locale setting, LOL.
# Updating a Ubuntu 14.04 LTS to 16.04 LTS
# by Julius Beckmann
# Prepare and Fix Locales.
apt-get update; apt-get install -y dialog language-pack-en-base language-pack-de-base; update-locale LANG=de_DE.UTF-8; dpkg-reconfigure locales; apt-get upgrade -y ; apt-get install -y update-manager-core
# Perform Dist Upgrade if presets ok
cat /etc/update-manager/release-upgrades | grep Prompt=lts && do-release-upgrade --check-dist-upgrade-only && do-release-upgrade -m server
# Answer questions, mostly with yes "y"
h4cc / struct_matching.exs
Created Jul 28, 2017
Example how matching on elixir structs will make code more robust and refactorable with less possible errors at runtime.
View struct_matching.exs
# This is our struct with some fields.
defmodule User do
defstruct name: "julius", role: :user
defmodule UserManager do
# Matching on %User{} will ensure we will get a user.
h4cc /
Created Jul 3, 2017
Installing a TP-Link TL-WN722N V2 on Ubuntu 16.04 with Kernel 4.4
# Source:
sudo apt-get install git dkms git make build-essential
cd /usr/src
sudo git clone
sudo dkms add ./rtl8188eu
sudo dkms build 8188eu/1.0
sudo dkms install 8188eu/1.0
sudo modprobe 8188eu
h4cc / with_example.exs
Created Jun 12, 2017
Elixir example for using "with" for a "unhappy path" lol.
View with_example.exs
# Checkout: WithExample.inverse_with
defmodule WithExample do
# Helper functions
def ok(result), do: {:ok, result}
def error(error), do: {:error, error}