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 / 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 / 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 / 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 / onename.txt
Created February 27, 2016 00:17
onename verification
Verifying that +gvaughn is my blockchain ID. https://onename.com/gvaughn
@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
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 / 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
@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 / stack.exs
Created September 27, 2016 14:25
homework assignment to implement a stack and evaluate a postfix expression
defmodule Stack do
@moduledoc """
Implement a stack using a linked list to back it. The stack should support
peek, push, pop, count
do it in whatever language you want
"""
defstruct llist: []
def peek(%Stack{llist: [head | _]}), do: head
def peek(%Stack{}), do: nil
@gvaughn
gvaughn / ecto_postgres_fulltext_search_querying_example.ex
Created October 4, 2017 22:34 — forked from pmarreck/ecto_postgres_fulltext_search_querying_example.ex
How to set up postgres fulltext search triggers, index, and tsvector column on Elixir/Phoenix, with Ecto querying, including ranking and sorting by rank
defmodule YourAppName.Search do
# ...
@doc """
Queries listings.
"""
def query_listings(query, current_user) do
default_scope = from l in Listing, where: l.draft == false or l.user_id == ^current_user.id, order_by: [desc: l.updated_at], limit: 50
id = _try_integer(query)