Skip to content

Instantly share code, notes, and snippets.

box: trenpixster/elixir:1.2.5
services:
- id: postgres
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
build:
steps:
@vysakh0
vysakh0 / subarray_sum.ex
Last active June 29, 2016 14:51
Find if subarray with given sum exists
defmodule SubarraySum do
def run([], val), do: false
def run([ _ | rest ] = arr, val) do
run(arr, val, 0) || run(rest, val)
end
def run(_, val, val), do: true
def run([], _val, _res), do: false
def run(_, val, res) when(val < res), do: false
@vysakh0
vysakh0 / cars_controller.ex
Last active March 7, 2016 16:39
Problem: Get cars from different vendors for a pickup and destination. Given each vendor has set prices for distances/places.
# ...
def index(conn, params) do
model =
Enum.map(vendor_ids, fn(vendor_id) ->
Task.async(fn() ->
Cabs.find_with_fare(params, vendor_id)
# [ %Cab{price: 1, distance: 2},..]
# Does some calculation based on results from different tables.
end)
end)
@vysakh0
vysakh0 / hydra.ex
Last active February 19, 2016 14:04
The Process that retries itself without monitor, supervisors.
defmodule Hydra do
use GenServer
def run(data) do
{:ok, pid} = GenServer.start(__MODULE__, data, [])
GenServer.cast(pid, :process)
end
def init(data) do
{:ok, data}
@vysakh0
vysakh0 / foo.ex
Created February 13, 2016 17:33
if the stream is "aaaafoo" and the word to search for is "foo", the function should print "Found foo" on the 6th incoming char (the last 'o' in "foo").
defmodule Foo do
def find(word) do
find(IO.getn(""), word, word)
end
def find(_char, "", word) do
IO.puts "Found #{word}"
end
def find(char, << char :: binary-size(1), rest :: binary >>, word) do
@vysakh0
vysakh0 / prime.ex
Last active February 12, 2016 13:52
Find the list of prime numbers upto n
defmodule Prime do
def run(number) do
max = :math.sqrt(number) |> round
result = Enum.to_list(2..number)
run(max, result, {result, []})
end
def run(0, _, {res, sieve}) do
Enum.reverse(sieve) ++ res
end
def run(max, [ head | _ ], {result, sieve}) do
@vysakh0
vysakh0 / frequent.ex
Last active February 11, 2016 05:17
Find the most frequent(continuously repeated) integer in a list.
defmodule Frequent do
def number( [num | tail] ) do
number({num, 1}, tail, {num, 1})
end
def number({num, count}, [ num | tail ], max) do
number({num, count + 1}, tail, max)
end
def number({_num, count} = current, [ diff | tail ], {_, max_count} = max) do
@vysakh0
vysakh0 / any.ex
Created February 10, 2016 03:49
Remove any given character in a string just using recursion
defmodule Any do
def remove(str, char) do
remove("", str, char)
end
def remove(newstr, << char :: binary-size(1), rest :: binary >>, char) do
remove(newstr, rest, char)
end
def remove(newstr, << first :: binary-size(1), rest :: binary >>, char) do
remove(newstr <> first, rest, char)
end
@vysakh0
vysakh0 / palindrome.ex
Created February 9, 2016 04:04
A simple palindrome without using any inbuilt functions like `String.reverse` or `String.length`
defmodule Palindrome do
def check(<< first :: binary-size(1), rest :: binary >> = actual) do
check(first, rest, actual)
end
def check(newstr, << next_letter :: binary-size(1), rest :: binary >>, actual) do
check(next_letter <> newstr, rest, actual)
end
def check(reverse, <<>>, actual), do: reverse === actual