Skip to content

Instantly share code, notes, and snippets.

Avatar

Julius Beckmann h4cc

  • ZENNER IoT Solutions
  • Hamburg, Germany
View GitHub Profile
@h4cc
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
query
|> limit(^per_page)
|> offset(^offset)
|> LongRepo.all()
@h4cc
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!
# https://elixirforum.com/t/put-update-deep-inside-nested-maps-and-auto-create-intermediate-keys/7993/8
@doc """
Will set value at for keys deep inside data.
iex> Helper.put_in_deep(%{a: %{}}, [:a, :b, :c], 42)
@h4cc
h4cc / Makefile
Last active Sep 1, 2020
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 <github@h4cc.de>
#
# Upgraded Script from 17.04: https://gist.github.com/h4cc/09b7fe843bb737c8039ac62d831f244e
# Upgraded Script from 16.04: https://gist.github.com/h4cc/fe48ed9d85bfff3008704919062f5c9b
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/hyperion-black-screen.sh
#!/bin/bash
if [ `service hyperion status | grep running | wc -l` = 1 ]
then
hyperion-remote -c black
sleep 3
sudo service hyperion stop
else
sudo service hyperion start
fi
@h4cc
h4cc / update_ubuntu_lts.sh
Created Nov 25, 2017
Updating a Ubuntu LTS version to the next available. Used for Noez.de vServer with a broken locale setting, LOL.
View update_ubuntu_lts.sh
# 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
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
end
defmodule UserManager do
# Matching on %User{} will ensure we will get a user.
@h4cc
h4cc / install_tl_wn722n.sh
Created Jul 3, 2017
Installing a TP-Link TL-WN722N V2 on Ubuntu 16.04 with Kernel 4.4
View install_tl_wn722n.sh
# Source: https://askubuntu.com/questions/912498/tl-wn722n-is-not-recognized
sudo apt-get install git dkms git make build-essential
cd /usr/src
sudo git clone https://github.com/lwfinger/rtl8188eu.git
sudo dkms add ./rtl8188eu
sudo dkms build 8188eu/1.0
sudo dkms install 8188eu/1.0
sudo modprobe 8188eu
@h4cc
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}
@h4cc
h4cc / Makefile
Created Apr 26, 2017
Ubuntu 17.04 Zesty Zapus - Basic packages i usually install
View Makefile
#
# Ubuntu 17.04 (Zesty Zapus)
#
# Basic packages i usually install.
#
# Author: Julius Beckmann <github@h4cc.de>
#
# Upgraded Script from 16.04: https://gist.github.com/h4cc/fe48ed9d85bfff3008704919062f5c9b
# Upgraded Script from 14.04: https://gist.github.com/h4cc/7be7f940325614dc59fb
You can’t perform that action at this time.