Skip to content

Instantly share code, notes, and snippets.

View mutablestate's full-sized avatar

Ben Sharman mutablestate

View GitHub Profile
@mutablestate
mutablestate / authorizable.rb
Last active August 29, 2015 13:58
Simple authorization of controller actions with Rails 4 Concern
# app/controllers/concern/authorizable.rb
module Authorizable
def authorize_admin!
unless signed_in? && current_user.admin?
flash_not_authorized
end
end
def authorize_editor!
@mutablestate
mutablestate / elixir_buzz.exs
Created July 29, 2014 02:59
FizzBuzz implementation in Elixir using cond
defmodule ElixirBuzz do
@moduledoc "It implements a fizzbuzz solution with cond"
@doc """
Returns a range from 1 to the given number in as list substituting a number for a pre-defined string when divisible by 3, 5, or 15 (3 and 5)
"""
def buzz(number) do
Enum.map(1..number, fn n ->
cond do
rem(n, 15) == 0 -> "ElixirBuzz"
@mutablestate
mutablestate / comeonin-otp-compile-error
Last active August 29, 2015 14:13
Comeonin compile error with OTP 17.1
==> comeonin
make: Nothing to be done for `priv/bcrypt_nif.so'.
Compiled lib/comeonin.ex
== Compilation error on file lib/comeonin/bcrypt.ex ==
23:08:33.076 [error] Error in process <0.271.0> with exit value: {{badmatch,{error,{bad_lib,"Library version (2.7) not compatible (with 2.6)."}}},[{'Elixir.Comeonin.Bcrypt',init,0,[{file,"lib/comeonin/bcrypt.ex"},{line,22}]},{code_server,'-handle_on_load/4-fun-0-',1,[{file,"code_serv...
** (MatchError) no match of right hand side value: {:error, :on_load_failure}
(stdlib) erl_eval.erl:657: :erl_eval.do_apply/6
@mutablestate
mutablestate / account.ex
Created February 19, 2015 21:45
Early exit example with multiple clause function (for Thailand Developers Elixir slack channel)
defmodule Account do
defstruct id: nil, balance: 0
def withdraw(account_id, amount) do
account_id
|> find
|> make_withdrawal(amount)
end
defp find(account_id) do
@mutablestate
mutablestate / never_use_pipe.ex
Created April 15, 2015 09:03
What if I never use pipe? Example 1 from blog post Pipe your way to readable and maintainable Elixir code
# setup
user = %{name: “Foo”}
new_name = %{name: “bar”}
# without pipe
updated_user = Map.put(user, :name, new_name)
validate_user = validate(updated_user)
# etc…
# with pipe
@mutablestate
mutablestate / pipe_multiple_transformation.ex
Created April 15, 2015 09:09
What if I only use pipe when my function expands to more than one transformation? Example 2 from blog post Pipe your way to readable and maintainable Elixir code
# setup
user = %{name: “Foo”}
new_name = %{name: “bar”}
# single transformation without pipe
Map.put(user, :name, new_name)
# single transformation with pipe
user |> Map.put(:name, new_name)
@mutablestate
mutablestate / roman.exs
Last active August 29, 2015 14:24
My current Roman numeral exercism.io iteration
defmodule Roman do
@arabic_roman %{
1 => "I",
5 => "V",
10 => "X",
50 => "L",
100 => "C",
500 => "D",
1000 => "M"
}
@mutablestate
mutablestate / vscode-extensions.md
Last active May 18, 2018 02:26
MBP Retina VSCode settings (Elixir / React dev)

EXTENSIONS LIST

code --list-extensions

LeCrunchic.mariana-nord
PeterJausovec.vscode-docker
Shan.code-settings-sync
Valentin.beamdasm
WallabyJs.quokka-vscode
@mutablestate
mutablestate / .iex.exs
Last active November 26, 2017 03:45
IEx configuration
imestamp = fn ->
{_date, {hour, minute, _second}} = :calendar.local_time()
[hour, minute]
|> Enum.map(
&String.pad_leading(Integer.to_string(&1), 2, "0")
)
|> Enum.join(":")
end

Keybase proof

I hereby claim:

  • I am mutablestate on github.
  • I am mutablestate (https://keybase.io/mutablestate) on keybase.
  • I have a public key ASB-gZNBAUHrojN0asgBv-y2-Nt_sgT_W2HLHxkLxvlEZAo

To claim this, I am signing this object: