Skip to content

Instantly share code, notes, and snippets.

@rlb3
rlb3 / code_lock.ex
Created February 11, 2019 21:45 — forked from peerreynders/code_lock.ex
Erlang gen_statem OTP design principles "state_functions" callback mode revised example "translated" to Elixir
# file: code_lock.ex
# Translated from: http://erlang.org/doc/design_principles/statem.html#example-revisited
# callback mode: :state_functions
#
defmodule CodeLockCommon do
# Admittedly gratuitous use of a macro
#
def handle_event(:cast, {:down, button}, data) do
{:keep_state, Map.put(data, :button, button)}
end
@rlb3
rlb3 / pushbutton.ex
Created February 11, 2019 21:35 — forked from peerreynders/pushbutton.ex
Erlang gen_statem module documentation "handle_event_function" callback mode example "translated" to Elixir
# file: pushbutton.ex
# Translated from: http://erlang.org/doc/man/gen_statem.html#example
# callback mode: :handle_event_function
# i.e. one callback function handle_event/4 for all states
#
defmodule Pushbutton do
@behaviour :gen_statem
# The registered server name
defp name(),
@rlb3
rlb3 / pushbutton.ex
Created February 11, 2019 21:35 — forked from peerreynders/pushbutton.ex
Erlang gen_statem module documentation "state_functions" callback mode example "translated" to Elixir
# file: pushbutton.ex
# Translated from: http://erlang.org/doc/man/gen_statem.html#example
# callback mode: :state_functions
# i.e. one callback function per state, so the each possible
# state has to be represented as a bare atom which directly
# identifies the module function to be called (:gen_fsm like).
#
# This example has two states :on and :off.
# Therefore there is an on/3 and an off/3 callback function.
#
@rlb3
rlb3 / code_lock.ex
Last active February 9, 2019 21:27 — forked from RaimoNiskanen/code_lock.ex
gen_statem Code Lock Example in Elixir for SF CodeBeam 2018
defmodule CodeLock do
@name :code_lock
## Start and stop
def start_link(code) do
:gen_statem.start_link({:local,@name}, __MODULE__, code, [])
end
def stop, do: :gen_statem.stop @name
def callback_mode, do: [:state_functions,:state_enter]
@rlb3
rlb3 / elixir.tex
Created June 24, 2018 17:58 — forked from m1dnight/elixir.tex
Elixir Listings Latex
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings,xcolor}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage[scaled=0.9]{DejaVuSansMono}
\definecolor{commentgreen}{RGB}{2,112,10}
\definecolor{eminence}{RGB}{108,48,130}
\definecolor{weborange}{RGB}{255,165,0}
\definecolor{frenchplum}{RGB}{129,20,83}

I learned a little bit about building queries (even with Rails/ARel) of postgres JSONB columns in my side project that I wanted to share.

I have a model that represents a Japanese word:

{"id"=>4111,
 "type"=>"vocabulary",
 "characters"=>"洗う",
 "meanings"=>[{
    "meaning"=>"To Wash", 
@rlb3
rlb3 / why.ex
Created April 15, 2018 20:45 — forked from ConnorRigby/why.ex
#2
defmodule :'Elixir.Why' do
@blah __ENV__.file
@file File.read!(@blah)
@num String.at(@file, 1) |> String.to_integer()
@doc false
def unquote(:"$handle_undefined_function")(function, _args) do
filename = __MODULE__.module_info()[:compile][:source] |> to_string()
File.read!(filename)
@rlb3
rlb3 / camel_case_argument_middleware.rb
Created February 22, 2018 18:21 — forked from coleturner/camel_case_argument_middleware.rb
Parses camelCase arguments into snake_case for GraphQL Ruby
class CamelCaseMiddleware
def call(parent_type, parent_object, field_definition, field_args, query_context, next_middleware)
next_middleware.call([parent_type, parent_object, field_definition, transform_arguments(field_args), query_context])
end
def transform_arguments(field_args)
transformed_args = {}
types = {}
field_args.each_value do |arg_value|
@rlb3
rlb3 / 2017-10-21-how-to-connect-and-debug-elixir-or-phoenix-app-at-heroku.md
Created October 21, 2017 23:21
How to connect and debug Elixir or Phoenix App at Heroku?

How to connect and debug Elixir or Phoenix App at Heroku?

Heroku has a great feature called heroku ps:exec which allows you to connect to running nodes. You can use this command to connect your elixir nodes easily. And debug your nodes like in your network.

Step 1

Let's start with Procfile To run a named app at heroku your procfile should specify the app name like:

web: MIX_ENV=prod elixir --sname coolelixirapp -S mix run --no-halt
@rlb3
rlb3 / frp.md
Created October 14, 2017 21:49 — forked from ohanhi/frp.md
Learning FP the hard way: Experiences on the Elm language

Learning FP the hard way: Experiences on the Elm language

by Ossi Hanhinen, @ohanhi

with the support of Futurice 💚.

Licensed under CC BY 4.0.

Editorial note