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 / .ex
Last active November 11, 2015 04:38
do fib
defmodule SimpleFib do
def fib(0), do: 0
def fib(1), do: 1
def fib(n) do
do_fib(n, 2, 1, 0)
end
defp do_fib(x, x, prev, prevprev) do
prev + prevprev
end
@elbow-jason
elbow-jason / .ex
Created September 25, 2015 20:39
query example
defp planting_operations_only do
from operation in Operation,
where: operation.operation_type == "planting",
preload: :variety
end
def crop_plan_preloads(query) do
from q in query,
preload: :crop,
preload: :fields,
@elbow-jason
elbow-jason / gist:ee3461d71ef7442d39ff
Last active August 29, 2015 14:25
import example
defmodule ImportExample do
# on this line Enum is not imported, yet.
import Enum #imports are not Module-wide. they only apply below the import.
def all_funcs do
map([1,2,3], fn n -> n + 1 end) #map is from Enum
end
defmodule Trier do
require IEx
def thing do
x = 0
try do
1 / x
rescue
e in ArithmeticError ->
@elbow-jason
elbow-jason / gist:d2e01e137c46f9c495e5
Last active August 29, 2015 14:22
pattern_matching_warnings
def passwords_match?(_x, _x), do: true
def passwords_match?(_, _), do: false
# causes the following warning...
# registration.ex:61: warning: the underscored variable "_x" appears more
# than once in a match. This means the pattern will only match
# if all "_x" bind to the same value. If this is the intended behaviour,
# please remove the leading underscore from the variable name,
# otherwise give the variables different names
def f(x) when x in [:ok, :bien, :guay] do
:ok
end
def f(_) do
:mal
end
@elbow-jason
elbow-jason / Limited_possibilites.ex
Created April 26, 2015 20:11
Limited Possibilities
def f(:ok), do: :ok
def f(:bien), do: :ok
def f(:guay), do: :ok
def f(_), do: :mal
@elbow-jason
elbow-jason / install_erlang_and_elixir.sh
Last active August 29, 2015 14:19
Install Erlang 17.5 from source and Elixir from Precompiled.zip (for Mint 17.1 Rebecca)
#!/bin/bash
# This script installs Erlang 17.5 from source and Elixir 1.0.4
# from precomplized zip. Installation took approximately
# 40 minutes with a crappy computer and crappy internet connetion.
# This script should work (read: was only tested) on Mint
# Rebecca 17.1, but honestly should work on most Linux debian distros.
# INSTRUCTIONS:
# Pull this into a directory that will persist. (not tmp)
@elbow-jason
elbow-jason / gist:2bd76dcdead70312e083
Created April 14, 2015 03:08
erlang 17.5 and elixir 1.0.4 issues
elbow@elbow-K55N /usr/local/lib/erlang $ iex
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
*** ERROR: Shell process terminated! (^G to start new job) ***
=INFO REPORT==== 13-Apr-2015::20:02:11 ===
application: elixir
exited: {bad_return,
{{elixir,start,[normal,[]]},
{'EXIT',
@elbow-jason
elbow-jason / gist:0fdea26fa4aad95e1098
Created March 31, 2015 04:32
Understanding Rust ADTs Blog Example. Copy. Paste. Compile. Run.
/*
This is an enum. In Rust, an enum is a type which can only
be in one of a finite category of states; the states with which
the enum was defined.
Each of these states may or may not "wrap" an already existent type.
For instance, a variable that is of the type FizzedEnum can be in
the one of the following states: FizzBuzz, Fizz, Buzz, or Int(isize).
In the case of Int(isize), the FizzedEnum is in a state that wraps
an integer of type isize.