Skip to content

Instantly share code, notes, and snippets.

View nurugger07's full-sized avatar

Johnny Winn nurugger07

View GitHub Profile

First create a function to handle the soft delete

defmodule MyApp.Repo.Migrations.SoftDeletes do
  use Ecto.Migration

  def up do
    execute """
      CREATE FUNCTION soft_delete() RETURNS TRIGGER AS $$
        DECLARE
@nurugger07
nurugger07 / maybe_apply.ex
Created March 27, 2018 17:03
Maybe apply a function to a module
defmodule Maybe do
def apply(mod, fun, args) when is_list(args) do
with true <- function_exported?(mod, fun, Enum.count(args)) do
Kernel.apply(mod, fun, args)
else
false ->
"No function exported for #{mod}"
end
end
defmodule Connection do
defstruct pid: nil, locked: false
end
defmodule Broker do
use GenServer
def start do
GenServer.start_link(__MODULE__, fill_pool, name: Moby)
end

Problem: 02

Write a function num_to_list/1 that takes a number and returns a string from 1 up to the number joined with commas, e.g:

num_to_list(10) #=> "1,2,3,4,5,6,7,8,9,10"

Trivial Example: Compose a meet & greet in Ruby

class Greeter
  class << self
    def meet(person, &block)
      yield person
    end

 # Greet everyone except Johnny

Simple Elixir module:

defmodule Generic do
  def say(message) do
    IO.puts message
  end
end

The AST for the this looks like:

defmodule BinaryGarden do
  def handshake(<< a :: size(16), "00", c :: binary-size(2) >>) when c == "AB" do
    "You testing me?"
  end
  def handshake(<< a :: size(16), b :: size(16), c :: size(16) >>), do: "Hello there!"
  def handshake(_), do: "Don't talk to strangers"
end
defmodule Math do
def sum([]), do: 0
def sum([head | tail]), do: head + sum(tail)
def square([]), do: []
def square([head | tail]), do: [head * head | square(tail)]
end
defmodule Mapper do
def map([], _func), do: []
def map([head|tail], func) do
[func.(head) | map(tail, func)]
end
def mapsum([], _func), do: 0
def mapsum([head|tail], func) do
func.(head) + mapsum(tail, func)
# Ruby-2.0.0-preview1
module ProjectEuler
refine Enumerator::Lazy do
def method_missing(method, *args, &block)
if method.to_s =~ /multiple_of_(.*)$/
multiple_of $1.to_i, args[0].to_i
else
super