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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Simple Elixir module:
defmodule Generic do
def say(message) do
IO.puts message
end
endThe AST for the this looks like:
Trivial Example: Compose a meet & greet in Ruby
class Greeter
class << self
def meet(person, &block)
yield person
end
# Greet everyone except JohnnyProblem: 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"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
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