Skip to content

Instantly share code, notes, and snippets.

> var arr = ['a', 'b', 'c'];
undefined
> delete arr[1];
true
> (1 in arr)
false
@robmerrell
robmerrell / tictactoe.exs
Created November 29, 2014 23:31
Elixir Quiz TicTacToe solution
# solution for http://elixirquiz.github.io/2014-11-22-tic-tac-toe-part-1-the-game.html
defmodule Game do
defstruct board: [["_", "_", "_"], ["_", "_", "_"], ["_", "_", "_"]], # 3 x 3
current_player_pid: "",
previous_player_pid: ""
def draw_board(%Game{board: board}) do
Enum.each board, &IO.puts/1
end
defmodule ListSubset do
defp _contains_letters_count([], _, acc), do: acc
defp _contains_letters_count([h|t], compare, acc) do
new_list = List.delete(compare, h)
diff = length(compare) - length(new_list)
_contains_letters_count(t, new_list, acc+diff)
end
defmodule ListPairs do
defp _find([], _, acc), do: acc
defp _find([h|t], target_sum, acc) do
accepted_pairs = Enum.filter_map t, &(&1 + h == target_sum), &({h, &1})
_find(t, target_sum, acc ++ accepted_pairs)
end
def find(list, target_sum) do
_find(list, target_sum, [])
@robmerrell
robmerrell / gist:e79db0aa7578fc295f32
Created October 13, 2014 03:10
fizzbuzz using case
defmodule FizzBuzz do
def fizzbuzz(num) do
fizzbuzz = fn(x) ->
case {rem(x, 3) == 0, rem(x, 5) == 0} do
{true, false} -> IO.puts "fizz"
{false, true} -> IO.puts "buzz"
{true, true} -> IO.puts "fizzbuzz"
_ -> IO.puts x
end
for !chClosed {
select {
case rows, chanOk := <-ch:
chClosed = !chanOk
if chanOk {
rows.Scan(&showType)
fmt.Println(showType)
}
case err := <-errCh:
CCDictElement* pElement = NULL;
CCDICT_FOREACH(spawn_point, pElement) {
CCLog("%s", pElement->getStrKey());
}
// TODO can we get rid of these?
// Create a texture for non selected and selected points
unselected_point = [[SPTexture alloc] initWithWidth:200 height:200 draw:^(CGContextRef context) {
CGContextSetLineWidth(context, 2.0f);
CGContextSetRGBFillColor(context, 0.7f, 0.7f, 0.7f, 1.0f);
CGContextSetRGBStrokeColor(context, 0.7f, 0.7f, 0.7f, 1.0f);
CGRect circlePoint = (CGRectMake(0.0f, 0.0f, 8.0f, 8.0f));
CGContextFillEllipseInRect(context, circlePoint);
}];
@robmerrell
robmerrell / gist:985024
Created May 22, 2011 00:25
Plasma Cannon Lite memory usage
In play
--------
VmSize: 74408 kB
VmHWM: 27076 kB
Carded
--------
VmSize: 58020 kB
VmHWM: 27080 kB
begin
# group info for the commit
repo_info = {
:repo => repo_name,
:author => commit.author,
:committer_name => commit.committer,
:date => commit.commit_timestamp,
:message => commit.message,
:diff_patch => commit.diff,
:diff_files => commit.diff_files,