Skip to content

Instantly share code, notes, and snippets.

View sasa1977's full-sized avatar

Saša Jurić sasa1977

View GitHub Profile
defmodule Overrider do
defmacro __using__(_) do
quote do
import Kernel, except: [def: 2]
import Overrider
end
end
defmacro def(fun_def, opts) do
quote do
cmd = ~S"""
ruby -e '
STDOUT.sync = true
context = binding
while (cmd = gets) do
eval(cmd, context)
end
'
"""
cmd = ~S"""
ruby -e '
STDOUT.sync = true
def receive_input
encoded_length = STDIN.read(4)
return nil unless encoded_length
length = encoded_length.unpack("N").first
STDIN.read(length)
cmd = ~S"""
ruby -e '
require "bundler"
require "erlang/etf"
require "stringio"
STDOUT.sync = true
def receive_input
encoded_length = STDIN.read(4)
cmd = ~S"""
ruby -e '
require "bundler"
require "erlang/etf"
require "stringio"
@input = IO.new(3)
@output = IO.new(4)
@output.sync = true
defmodule RubyServer do
use GenServer
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, nil, opts)
end
def cast(server, cmd) do
GenServer.cast(server, {:cast, cmd})
end
defmodule Primes do
# Basic implementation of an infinite prime generator, as explained at
# http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
def new do
%{composites: HashDict.new, current: 2}
end
def next(%{composites: composites, current: current} = sieve) do
case HashDict.get(composites, current) do
defmodule Fun2MsTest do
@compile {:parse_transform, :ms_transform}
def test do
match_spec = :ets.fun2ms(fn({:foo, bar} = el) when bar == :baz -> el end)
IO.inspect match_spec
end
end
Fun2MsTest.test
@sasa1977
sasa1977 / simple_one_for_one.ex
Created May 26, 2017 07:53
Simple one for one supervising multiple children
defmodule Worker1 do
def start_link() do
Task.start_link(fn ->
Stream.repeatedly(fn -> :rand.uniform(1000) end)
|> Stream.each(&:timer.sleep/1)
|> Stream.each(fn _ -> IO.puts "worker 1" end)
|> Stream.run()
end)
end
end
# http://adventofcode.com/2017/day/1
defmodule Day1 do
def sum1(digits), do:
digits
|> Stream.concat(Enum.take(digits, 1))
|> Stream.chunk_every(2, 1, :discard)
|> Stream.filter(&match?([el, el], &1))
|> Stream.map(&hd(&1))
|> Enum.sum()