Skip to content

Instantly share code, notes, and snippets.

View gvaughn's full-sized avatar
:atom:
Never trust an atom. They make up everything.

Greg Vaughn gvaughn

:atom:
Never trust an atom. They make up everything.
View GitHub Profile
@gvaughn
gvaughn / deeply_nested.exs
Last active May 28, 2019 03:54
Elixir get_in with nested maps and lists
defmodule DeeplyNested do
def get_nat_ip(input) do
steps = [first_map_with_key("accessConfigs"),
first_map_with_key("natIP")
]
get_in(input, steps)
end
defp first_map_with_key(key) do
@gvaughn
gvaughn / slackpost.sh
Created May 11, 2016 21:43 — forked from mattak/slackpost.sh
post message to slack
#!bash -e
TOKEN= # slack token is generate from: https://api.slack.com/web
CHANNEL= # name of channels or group
MESSAGE= # message
NICK= # bot name
IS_PRIVATE= # 1 or 0
if [ $IS_PRIVATE -eq 1 ]; then
API_TYPE=groups
p=fn 0,_->;i,p->spawn fn->w=" on the wall.";b=&" #{&1} bottle#{&1==1&&""||"s"} of Elixir"
IO.puts [inspect(self),b.(i),w,b.(i),".\n",i==1&&"Go get some more,"<>b.(99)||"Take one down pass it around,"<>b.(i-1),w,10]
p.(i-1,p)end end;p.(99,p);:timer.sleep 999
@gvaughn
gvaughn / Mapper.ex
Created March 1, 2016 00:58
representation mapping
defmodule Mapper do
@x_mapping [bar: [:baar], constant: [:constant], foo: [:fooo], stuffiness: [:stuff, :stuffiness], thing_other: [:thing, :other], thing_sub: [:thing, :sub]]
def for_x(source_data) do
mapper_for(@x_mapping).(source_data)
end
defp mapper_for(mapping) do
&Enum.map(&1, fn input -> Map.new(mapping, fn {dest, src} -> {dest, get_in(input, src)} end) end)
end
@gvaughn
gvaughn / onename.txt
Created February 27, 2016 00:17
onename verification
Verifying that +gvaughn is my blockchain ID. https://onename.com/gvaughn
@gvaughn
gvaughn / golf.exs
Created November 24, 2015 18:27 — forked from henrik/caddy.exs
A convenient script for Elixir golfing.
# A convenient script for Elixir golfing.
# Provide your code and some test cases. Then run this file, e.g. "elixir golf.exs".
# Outputs your character length and test results.
# Text input, text output and return values are all handled.
#
# By Henrik Nyh (http://henrik.nyh.se) under the MIT license.
ExUnit.start
defmodule Golf.Example do
@gvaughn
gvaughn / caesar.exs
Last active November 27, 2015 19:20
Caesar Cipher ElixirGolf
# for an offset range of -26 to + 26
# 114 characters
[n,p]=IO.gets("")|>String.split",";IO.puts for c<-to_char_list(p),do: c<97&&c||97+rem c-71-String.to_integer(n),26
# for any offset range
# 122 characters
[n,p]=IO.gets("")|>String.split",";IO.puts for c<-to_char_list(p),do: c<97&&c||97+rem c-71-rem(String.to_integer(n),26),26
# with @MPAherns use of a binary generator in the list comprehension
# 103 characters
@gvaughn
gvaughn / clock.ex
Created September 30, 2015 15:27 — forked from CrowdHailer/clock.ex
Creating boundary modules for elixir applications. These have their implementation set during the configuration step. In this example we switch clock between system clock and a dummy clock
# This module represents a behaviour and when used picks from the Application configuration which implementation will be used
defmodule Clock do
@callback now() :: Integer.t
defmacro __using__([]) do
module = Application.get_env(:my_app, :Clock)
quote do
alias unquote(module), as: Clock
end
@gvaughn
gvaughn / description.md
Last active September 28, 2015 23:44
Address changes

What's up with Address and UserAddressBook?

It's a domain level refactoring. Address is now readonly with copy on write semantics, so once it's in the db, it doesn't change. If you use the typical nested attributes for addresses in Orders (and now Users and CreditCards) it'll just work. If you need to create them manually Address.factory and Address.immutable_merge are your new friends. UserAddressBook is foundational work to ultimately allow users a richer set of addresses to use for pre-fill during checkout.

Background

I was asked to add what seemed like a fairly simple new feature to our custom frontend code, but upon digging I found that it would require some fairly large refactoring to do it well. That new feature was to allow the user to manage a list of addresses in their account section of our frontend. Those will be used as options to pre-fill during checkout. The things that I found that I wanted to refactor were some missing and misplaced address associations, plus a lot of unn

@gvaughn
gvaughn / copr.md
Last active January 30, 2024 16:11
git copr alias

I'd like to share some git aliases that you might find useful if you handle pull requests from others.

Add these to your ~/.gitconfig in the [alias] section:

copr = "!f() { git fetch -fu origin refs/pull/$1/head:pr-$1; git checkout pr-$1; } ; f"
prunepr = "!git for-each-ref refs/heads/pr-* --format='%(refname:short)' | while read ref ; do git branch -D $ref ; done"

Now you can "git copr #{pr_number}" (check out pull request is the mnemonic) and it will pull down the PR in a local branch of pr-#{pr_number} and check it out for you. To do it right, you must pronounce it "Copper" with a James Cagney gangster accent.