Skip to content

Instantly share code, notes, and snippets.

View Dania02525's full-sized avatar

Dania Es Dania02525

  • Aureo Accounts LLC, Parcelmagic, Asymptote Communications, Homeminder.ai
  • Key Largo, FL, Taos, NM
View GitHub Profile
@Dania02525
Dania02525 / fizzbuzz.exs
Last active August 29, 2015 14:24
Elixir fizzbuzz pattern matching
defmodule Fizzbuzz do
def fizzbuzzer(n) do
case {rem(n, 3), rem(n, 5), n} do
{0, 0, _} ->
"FizzBuzz"
{0, _, _} ->
"Fizz"
{_, 0, _} ->
@Dania02525
Dania02525 / task_race.exs
Last active January 4, 2017 19:39
Task race, return the winner, ignore the slower tasks (if any)
defmodule Async do
def run do
task1 = Task.async(fn -> :timer.sleep(10000); :slow end)
task2 = Task.async(fn -> :timer.sleep(2000); :fail end)
task3 = Task.async(fn -> :timer.sleep(1000); :fail end)
await([task1, task2, task3])
end
@Dania02525
Dania02525 / sublist.exs
Last active August 29, 2015 14:25
Brute force solution to try different possibilities simultaneously for the sublist challenge on excercism.io
defmodule Sublist do
@doc """
Returns whether the first list is a sublist or a superlist of the second list
and if not whether it is equal or unequal to the second list.
"""
def compare(a, b) do
task1 = Task.async(fn -> is_equal(a, b) end)
task2 = Task.async(fn ->
case is_equal(a, b) do
:equal ->
@Dania02525
Dania02525 / parse.exs
Created July 30, 2015 04:03
Parse multilayer map containing list to bracket style array as a string
defmodule Parse do
def brackets(map) do
map
|> Enum.map(fn({k,v})-> process(Atom.to_string(k), v) end)
|> List.flatten
|> URI.encode_query
end
def process(acc, v) when is_map(v) do
@Dania02525
Dania02525 / create_postgres_procedures.exs
Last active June 24, 2016 17:40
Ecto migration to allow postrges to generate local ids in a SaaS app
defmodule Repo.Migrations.CreateProcedures do @doc """
Creates procedure to get and update a local_id number on SaaS apps where
next local id is stored in the tenant table.
Example trigger is given for the user table
"""
use Ecto.Migration
def up do
execute "CREATE OR REPLACE FUNCTION get_local_id(tenant_id integer) RETURNS integer AS $$
defmodule PlugJwt do
@moduledoc """
A JWT Plug
Usage:
#When reading from joken config block
plug PlugJwt
#or the module that implements `Joken.Config` can be set explicitly
@Dania02525
Dania02525 / elixir-postgres-9.4-Vagrantfile.sh
Last active September 14, 2015 18:17
Vagrant file for trusty with erlang, elixir, postgres 9.4 and git
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network "forwarded_port", guest: 4000, host: 4000
config.vm.provision :shell, :inline => PROVISION
end
@Dania02525
Dania02525 / hs_tariffs.exs
Created December 2, 2015 19:49
Hs tariff seed for Ecto
This file has been truncated, but you can view the full file.
tariffs = [%{category: "Live horses, asses, mules and hinnies", hs_number: "010121", subcategory: "Horses", type: "Purebred breeding animals"}, %{category: "Live horses, asses, mules and hinnies", hs_number: "010129", subcategory: "Horses", type: "Other"}, %{category: "Live horses, asses, mules and hinnies", hs_number: "010190", subcategory: "Horses", type: "Imported for immediate slaughter"}, %{category: "Live bovine animals", hs_number: "010221", subcategory: "Cattle", type: "Purebred breeding animals"}, %{category: "Live bovine animals", hs_number: "010229", subcategory: "Cattle", type: "Other"}, %{category: "Live bovine animals", hs_number: "010231", subcategory: "Cattle", type: "Purebred breeding animals"}, %{category: "Live bovine animals", hs_number: "010239", subcategory: "Cattle", type: "Other"}, %{category: "Live swine", hs_number: "010391", subcategory: "Other", type: "Weighing less than 50 kg each"}, %{category: "Live swine", hs_number: "010392", subcategory: "Other", type: "Weighing 50 kg or more
@Dania02525
Dania02525 / terminal here.workflow
Last active June 27, 2016 15:10
Apple script to open teminal and cd to directory- for iterm2 3.0.2 beta
on run {input, parameters}
tell application "Finder"
set dir_path to "\"" & (POSIX path of (input as string)) & "\""
-- display dialog dir_path
end tell
log dir_path
CD_to(dir_path)
end run
on CD_to(theDir)
@Dania02525
Dania02525 / cumulative_merge.rb
Created November 30, 2016 17:17
Cumulative merge- a non destructive ruby merge operation
# rather than blindly overwriting key=>value pairs in the target
# as #merge method, for each matching key found in the target, the
# values are merged, meaning you can add to any level of the hash by
# passing the full key=>value path to the value you wish to add to
def cumulative_merge(target, hash)
if target.keys.include? hash.keys.first
Hash[hash.keys.first, cumulative_merge(target[hash.keys.first], hash[hash.keys.first])]
else
target.merge(hash)
end