Skip to content

Instantly share code, notes, and snippets.

View rbishop's full-sized avatar

Richard Bishop rbishop

View GitHub Profile
@rbishop
rbishop / egpu.md
Last active July 25, 2020 16:41
2013 MacBook Pro eGPU build

Context

I bought a new Dell U2720Q monitor but the onboard graphics on my 13 inch 2013 MacBook Pro (Catalina) was not powerful enough to drive it at 3840x2160@60Hz. I had been using the Apple Thunderbolt Display prior to this and I really liked how through a single Thunderbolt cable it connected the display, ethernet, and USB devices. I didn't want to buy a new laptop as this one is still powerful enough for my needs of programming and not gaming.

As such, my parameters for an enclosure were that:

  • Small form factor. A lot of enclosures such as the Sonnet Breakaway Box and Razer Core X are very large.
  • This isn't for gaming so I didn't need a beefy video card or an enclosure with a really high wattage power supply.
  • Thunderbolt 3. While my current laptop is Thunderbolt 2 I recognize I won't have it for more than 2-3 more years and will eventually have a Thunderbolt 3 computer.
  • Ethernet and USB ports on the enclosure were a plus but not a hard requirement. The idea of having as many of my peripherals
@rbishop
rbishop / after.ex
Created August 18, 2015 15:06
Replacing imperative if/else with lazy, infinite Stream. I had to remove some of the specifics for security purposes but hopefully this shows the general idea.
def loop(model, models) do
task_sup = Process.whereis(:worker_supervisor)
Stream.resource(
fn -> {model, 0} end,
&func_that_gets_data/1,
fn(_) -> :ok end
)
|> Stream.chunk(20)
|> Stream.map(fn(ids) -> Task.Supervisor.async(task_sup, Worker, :sanitize, [model, ids]) end)
@rbishop
rbishop / flatten.ex
Created June 5, 2015 13:18
TCO and append-less List.flatten
defmodule ListOp do
def flatten([head | tail]), do: flatten(tail, [head])
def flatten([], acc), do: Enum.reverse(acc)
def flatten([ [head | []] | tail], acc), do: flatten(tail, [head | acc])
def flatten([ [head | rest] | tail], acc), do: flatten(flatten([rest | tail], [head | acc]))
def flatten([head | tail], acc), do: flatten(tail, [head | acc])
end
@rbishop
rbishop / README.md
Last active April 26, 2022 15:38
A super simple Elixir server for sending Server Sent Events to the browser.

Generate a new Elixir project using mix and add cowboy and plug as dependencies in mix.exs:

  defp deps do
    [
      {:cowboy, "~> 1.0.0"},
      {:plug, "~> 0.8.1"}
    ]
  end
@rbishop
rbishop / quicksort.ex
Created September 26, 2014 17:49
Quicksort in Elixir
# Not tail recursive and uses ++ :(
defmodule Quicksort do
def sort([]), do: []
def sort([head | rest]) do
{before, after} = Enum.partition(rest, &(&1 < head))
sort(before) ++ [head] ++ sort(after)
end
end
@rbishop
rbishop / ecto.ex
Last active August 29, 2015 14:06
Based on an unknown list size, I want to create an inner join and where clause for each item in the list. I don't know how to dynamically change the binding in the where, though.
def by_path(ancestry) do
Enum.reduce(ancestry, App.Node, fn (ancestor, query) ->
query
|> join(:inner, [n], n1 in App.Node, n1.id == n.parent_id)
|> where([n], n.key == ^ancestor) # This is where I'm stuck, all the where's are n0."key", see line 14
end)
end
# Here is the resulting SQL:
SELECT n0."id", n0."key", n0."project_id", n0."type", n0."parent_id"
@rbishop
rbishop / centinel.rb
Last active August 29, 2015 14:04
Cardinal Centinel Gateway Example
credit_card = ActiveMerchant::Billing::CreditCard.new(cc_number, cc_info_hash)
gateway = ActiveMerchant::Billing::CardinalCentinelGateway.new(
ProcessorId: '123',
MerchantId: '456',
TransactionPwd: 'p4ssw0rd'
)
lookup_response = gateway.authorize(amount, credit_card)
if lookup_response.enrolled?
@rbishop
rbishop / austin_beer
Last active August 29, 2015 14:04
Craft breweries and beers to order in Austin
- Jester King
- Black Metal Farmhouse Imperial Stout
- Whatever Farmhouse and Wild Ales they have out
- Anything else you see from them, apparently
- Austin Beerworks
- Fire Eagle IPA
- Heavy Machinery Double IPA
- Peacemaker Pale Ale
- Blackthunder Schwarzbier
@rbishop
rbishop / private.rb
Created July 15, 2014 18:57
Inheritance and private vs protected
class Parent
def initialize(name)
@name = name
end
private
attr_accessor :name
end
@rbishop
rbishop / unique_pairs.ex
Last active August 29, 2015 14:02
Find all the unique pairs of numbers that add to 100 in a list
defmodule UniquePairs do
def hundreds(numbers) do
numbers
|> combinations
|> is_hundred
|> sort_pairs
|> Enum.uniq
end