Skip to content

Instantly share code, notes, and snippets.

View tmartin8080's full-sized avatar
๐Ÿ 
๐Ÿก

Troy Martin tmartin8080

๐Ÿ 
๐Ÿก
View GitHub Profile
@tmartin8080
tmartin8080 / leetcode-two-sum-solution.ex
Last active February 9, 2023 12:46
leetcode-two-sum-solution
defmodule Solution do
def two_sum(nums, target) do
nums
|> Enum.with_index()
|> Enum.reduce_while(%{}, fn {num, index}, acc ->
diff = target - num
if Map.has_key?(acc, diff) do
{:halt, [Map.get(acc, diff), index]}
else
@tmartin8080
tmartin8080 / big-o.md
Created November 16, 2021 16:47 — forked from PJUllrich/big-o.md
Big-O Time Complexities for Elixir Data Structures
@tmartin8080
tmartin8080 / Map.Helpers
Created February 24, 2020 13:44 — forked from kipcole9/Map.Helpers
Helpers for Elixir Maps: underscore, atomise and stringify map keys
defmodule Map.Helpers do
@moduledoc """
Functions to transform maps
"""
@doc """
Convert map string camelCase keys to underscore_keys
"""
def underscore_keys(nil), do: nil
@tmartin8080
tmartin8080 / with_example.ex
Created February 8, 2020 23:27 — forked from devonestes/with_example.ex
Further refactoring of a with statement
# Step 1
def create_subscription(email, plan_id, payment_method_id) do
with %User{customer_id: nil, name: name} = user <-
Repo.get_by(User, email: email),
{:ok, %Stripe.Customer{id: customer_id}} <-
Stripe.Customer.create(%{
name: name,
email: email,
payment_method: payment_method_id,
@tmartin8080
tmartin8080 / sidekiq_cheat_sheet.md
Last active November 12, 2019 18:42 — forked from wakproductions/sidekiq_cheat_sheet.md
Sidekiq Commands Cheat Sheet
@tmartin8080
tmartin8080 / clear-sidekiq-jobs.md
Created November 11, 2019 21:03
clear-sidekiq-jobs
require 'sidekiq/api' # for the case of rails console

Sidekiq::Queue.new("infinity").clear
Sidekiq::RetrySet.new.clear
Sidekiq::ScheduledSet.new.clear
@tmartin8080
tmartin8080 / z.md
Created November 8, 2019 12:26 — forked from mischah/z.md
Installing und initializing z (https://github.com/rupa/z) with help of Homebrew.

#The power of z

Do you spend lots of time doing things like this?

cd this/is/the/path/that/i/want/so/i/type/it/all/out/to/get/whereiwant

With z, you could just do this:

@tmartin8080
tmartin8080 / error-handling-with-fetch.md
Created September 19, 2019 18:59 — forked from odewahn/error-handling-with-fetch.md
Processing errors with Fetch API

I really liked @tjvantoll article Handling Failed HTTP Responses With fetch(). The one thing I found annoying with it, though, is that response.statusText always returns the generic error message associated with the error code. Most APIs, however, will generally return some kind of useful, more human friendly message in the body.

Here's a modification that will capture this message. The key is that rather than throwing an error, you just throw the response and then process it in the catch block to extract the message in the body:

fetch("/api/foo")
  .then( response => {
    if (!response.ok) { throw response }
    return response.json()  //we only get here if there is no error
 })
@tmartin8080
tmartin8080 / install_elixir.md
Created November 30, 2018 16:11 — forked from rubencaro/install_elixir.md
Elixir installation guide

Elixir installation guide

Version numbers should be the ones you want. Here I do it with the last ones available at the moment of writing.

The simplest way to install elixir is using your package manager. Sadly, at the time of writing only Fedora shows the intention to keep its packages up to date. There you can simply sudo dnf install erlang elixir and you are good to go.

Anyway, if you intend to work with several versions of erlang or elixir at the same time, or you are tied to a specific version, you will need to compile it yourself. Then asdf is your best friend.

@tmartin8080
tmartin8080 / gist:5aceab04cf3fabd6fff952939fa7b8de
Last active November 30, 2018 16:22
deploy-phoenix-asdf-install-deps
sudo apt-get install -y build-essential git wget libssl-dev libreadline-dev \
libncurses5-dev zlib1g-dev m4 curl wx-common libwxgtk3.0-dev autoconf \
libxml2-utils xsltproc fop unixodbc unixodbc-bin unixodbc-dev