Skip to content

Instantly share code, notes, and snippets.

View elbow-jason's full-sized avatar
🏠
Working from home

Jason Goldberger elbow-jason

🏠
Working from home
View GitHub Profile
@elbow-jason
elbow-jason / matcher.exs
Created February 11, 2018 18:50
match a collection using pattern matching
defmodule Matcher do
defmacro match_any?(pattern, collection) do
quote do
Enum.any?(unquote(collection), fn item ->
case item do
unquote(pattern) -> true
_ -> false
end
end)
end
defmodule Breadpacket do
use GenServer
def start_link(%{packet_state: state, packet_name: name}) do
GenServer.start_link(__MODULE__, state, name: name)
end
def init(state) do
{:ok, state}
defmodule LookupExample do
def lookup(ip) do
ip
|> Geolix.lookup
|> parse_lookup
end
def parse_lookup(%{city: %{name: city_name}, country: %{iso_code: iso_code}, postal: %{code: postal_code}}) do
{city_name, iso_code, postal_code}
defmodule Bmgr.Transaction.SearchQuery do
import Ecto.Query
alias Bmgr.{Transaction}
@filters [:account, :status]
def build(filter_params) do
do_filter(Transaction, @filters, filter_params)
end
@elbow-jason
elbow-jason / redis_cheatsheet.bash
Created October 27, 2018 21:22 — forked from LeCoupa/redis_cheatsheet.bash
Redis Cheatsheet - Basic Commands You Must Know --> UPDATED VERSION --> https://github.com/LeCoupa/awesome-cheatsheets
# Redis Cheatsheet
# All the commands you need to know
redis-server /path/redis.conf # start redis with the related configuration file
redis-cli # opens a redis prompt
# Strings.
use std::thread;
use std::time as timing;
use time;
use std::fs;
use std::io;
use std::io::BufRead;
static DB_PATH: &str = "./file.db";
defmodule MultisortDecimalTest do
use ExUnit.Case
@data [
%{multiplier: Decimal.from_float(1.0), points: Decimal.from_float(400.0)},
%{multiplier: Decimal.from_float(1.0), points: Decimal.from_float(500.0)},
%{multiplier: Decimal.from_float(0.9), points: Decimal.from_float(600.0)},
%{multiplier: Decimal.from_float(0.9), points: Decimal.from_float(700.0)}
]
alias Ecto.Changeset
defmodule Poly1 do
use Ecto.Schema
embedded_schema do
field(:key1, :string)
end
@elbow-jason
elbow-jason / 000_what_is_nil.ex
Last active September 26, 2019 17:15
Bad, Better, Best - Loss of type information in Elixir
x = nil
# Q: What is x?
# Q: What type is it?
# Q: What does it represent?
@elbow-jason
elbow-jason / iex_session.ex
Created October 18, 2019 17:34
SparseCells
iex(1)> cells = SparseCells.build([cell_1: ["a", "b"], cell_2: [1, 2], cell_3: ["w", "x", "y", "z"]])
%SparseCells{
key_sizes: [cell_1: 2, cell_2: 2, cell_3: 4],
map: %{
{:cell_1, 0} => "a",
{:cell_1, 1} => "b",
{:cell_2, 0} => 1,
{:cell_2, 1} => 2,
{:cell_3, 0} => "w",
{:cell_3, 1} => "x",