Skip to content

Instantly share code, notes, and snippets.

@tehprofessor
tehprofessor / iex-autocomplete.md
Created January 23, 2020 01:27
iex autocomplete issue/question

Issue

When you try to autocomplete a module but have incorrectly capitalized it, it outputs an error to the console, e.g.

iex(2)> EFfDB.
17:09:28.708 [error] Loading of _build/dev/lib/eff_db/ebin/Elixir.EFfDB.beam failed: :badfile

When the module doesn't exist, no error is logged to the console. The issue isn't with IEx it's only manifesting there,

@tehprofessor
tehprofessor / mapify.ex
Last active January 27, 2020 13:27
Elixir example for HN post
defmodule Mapify
# This function uses a little trick matching on `%_{}` which matches only if data is a struct
def to_map(%_{} = data) do
container = Map.from_struct(data)
# Elixir will destructure a map into a two-item tuple {key, value}.
for {k, value} <- container, into: container, do: {k, to_map(value)}
end
# This matches `anything` it's the "catch all" for our `to_map` function
@tehprofessor
tehprofessor / karabiner-fix.sh
Created July 4, 2019 18:03
Karabiner Elements "Fix-It" for Catalina Beta
#!/bin/sh
KARABINER_ELEMENTS_PATH="/Applications/Karabiner-Elements.app"
echo "Killing Karabiner & associated processes"
sudo pkill -f Karabiner -9 # Kills the main Karabiner UI
sudo pkill -f karabiner -9 # Kills the background processes
echo "Starting Karabiner-Elements... It may be up to a minute after opening Karabiner-Elements before your custom keys activate."
open $KARABINER_ELEMENTS_PATH # Open it back up!
defmodule Thinger do
end
defmodule Thinger.Fubar do
defstruct :queue, :some_module, :some_other_thing
use GenServer
def init(:ok) do
@tehprofessor
tehprofessor / .brew-fix.sh
Created May 24, 2018 20:15
Fixes Homebrew's annoying upgrade/update behaviour, will break `update --merge` functionality but quite honestly, IDGAF.
brew() {
local cmd=$1; shift
if test "$cmd" = 'upgrade' || test "$cmd" = 'update'; then
"__brew_update_or_upgrade" "$@"
else
command brew "$cmd" "$@"
fi
}
__brew_update_or_upgrade() {
@tehprofessor
tehprofessor / nil_or_else_example.rb
Created February 8, 2016 06:07
Ruby 2.3's safe navigation operator allows for a pretty nice "or else" construct.
module OrElse
module ObjectImplementation
def or_else(_)
self
end
end
module NilClassImplementation
def or_else(val)
val
end
@tehprofessor
tehprofessor / GetOrElse.swift
Last active August 29, 2015 14:23
GetOrElse -- Swift
//
// GetOrElse.swift
//
// Requires Swift 2.0 for protocol extensions
//
import Foundation
protocol GetOrElse {
func getOrElse<T>(block block: () -> T) -> T
import scala.util._
import java.security.SecureRandom
import java.security.MessageDigest
/*
* Generates a Bearer Token with a length of
* 32 characters (MD5) or 64 characters (SHA-256) according to the
* specification RFC6750 (http://tools.ietf.org/html/rfc6750)
*
* Uniqueness obtained by by hashing system time combined with a
@tehprofessor
tehprofessor / nsgradient_example.rb
Last active August 29, 2015 13:56
NSGradient.alloc.initWithColorsAndLocations -- RubyMotion (OS X)
def make_stepped_gradient
@grad = NSGradient.alloc.initWithColorsAndLocations(start_color, 0.0, middle_color, 0.015, end_color, 1.0, nil)
# Explodes with:
# 2014-02-23 10:52:01.323 my_app[86365:303] -[__NSCFNumber colorUsingColorSpace:]: unrecognized selector sent to instance 0x7f8764169b20
# 2014-02-23 10:52:01.416 my_app[86365:303] -[__NSCFNumber colorUsingColorSpace:]: unrecognized selector sent to instance 0x7f8764169b20
end
def start_color
@start_color ||= BW.rgba_color(100,100,100,1.0)
end
def middle_color
@tehprofessor
tehprofessor / string.rb
Created September 23, 2013 00:44
Handy little shortcut for OpalRB, adds #to_element method (and #to_e via an alias) to String.
class String
def to_element
Element.find("#{self}")
end
alias :to_e :to_element
end
# Now you can do:
el = "#cool-stuff".to_e
# vs