Skip to content

Instantly share code, notes, and snippets.

View lasseebert's full-sized avatar

Lasse Skindstad Ebert lasseebert

View GitHub Profile
# This file contains the configuration for Credo and you are probably reading
# this after creating it with `mix credo.gen.config`.
#
# If you find anything wrong or unclear in this file, please report an
# issue on GitHub: https://github.com/rrrene/credo/issues
#
%{
#
# You can have as many configs as you like in the `configs:` field.
configs: [

Mini project specification

Task

Build a URL shortener API

Requirements

The following endpoints must be implemented:

@lasseebert
lasseebert / decrypt_payload.exs
Last active January 12, 2018 21:35
Decrypting AES CRT (Counter mode) in Elixir
defmodule DecryptPayload do
@moduledoc """
Decrypts payload
"""
def run(payload_hex, key_hex, iv_hex) do
payload_bytes = payload_hex |> Base.decode16!
key_bytes = key_hex |> Base.decode16!
iv_bytes = iv_hex |> Base.decode16!

Isolating code in contexts

Based on my blog post

Agenda

  • What?
  • How?
  • Why?
  • Elixir specific stuff
defmodule Legolas.Geolocation do
@moduledoc """
Public interface for all geolocation related functionality
"""
alias Legolas.Geolocation.Cache
alias Legolas.Geolocation.CityLocation
alias Legolas.Geolocation.GoogleMaps.Client
@type address :: {zip :: String.t, city :: String.t, country :: String.t}
children = [
# Many children here
# ...
MyProject.Geolocation
]
opts = [strategy: :one_for_one, name: MyProject.Supervisor]
Supervisor.start_link(children, opts)
alias MyProject.Geolocation.Cache
def child_spec([]) do
%{
id: __MODULE__,
start: {Cache, :start_link, []}
}
end
@type address :: {zip :: String.t, city :: String.t, country :: String.t}
@type geolocation :: {latitude :: number, longitude :: number}
@doc """
Finds the geolocation for the given address
"""
@spec lookup(address) :: {:ok, geolocation} | {:error, any}
def lookup(address) do
# Logic that calls the inner modules
end

Genstage - what is it?

Like a pipe operator (|>), but for processes and with back pressure and on coke.

So what is this back-pressure? In short it is "pull" and not "push".

Can I haz some codez?

Sample1 demo

Metaprogramming in Elixir - with a practical example

What is metaprogramming?

  • Writing code that writes code
  • In Elixir this could mean:
    • Write code outside of functions to define functions or modules
    • Implement a macro to manipulate AST at compile time
    • Manipulate and evaluate AST at runtime