Skip to content

Instantly share code, notes, and snippets.

View henrik's full-sized avatar

Henrik Nyh henrik

View GitHub Profile
ExUnit.start
defmodule Golf.ChristmasTree do
use ExUnit.Case
import ExUnit.CaptureIO
# Put your code between the triple quotes.
# This is the 155 character baseline courtesy of @stoft.
@code """
IO.puts " *
defmodule MyMacro do
defmacro pick(list) do
after_block = Keyword.fetch(list, :after)
do_option = Keyword.fetch(list, :do)
else_options = Keyword.get_values(list, :else)
:random.seed(:os.timestamp)
option = Enum.random([do_option|else_options])
[option, after_block]
@henrik
henrik / caddy.exs
Last active December 15, 2015 17:31
A convenient script for Elixir golfing.
# A convenient script for Elixir golfing.
# Provide your code and some test cases. Then run this file, e.g. "elixir caddy.exs".
# Outputs your character length and test results.
# Text input, text output and return values are all handled.
#
# By Henrik Nyh (http://henrik.nyh.se) under the MIT license.
ExUnit.start
defmodule Caddy.Example do
defmodule Twelixir do
defmacro __using__(_) do
quote do
import Kernel, except: [@: 1]
import Twelixir
end
end
defmacro @({name, _, [message]}) do
var = Macro.var(name, __MODULE__)
# Shortest (91 chars):
for n<-1..?d,r=&(rem(n,&1)==0&&IO.write&2)do;f=r.(3,"Fizz");b=r.(5,"Buzz");f||b||r.(1,n)end
# If newlines are required (99 chars):
for n<-1..?d,r=&(rem(n,&1)==0&&IO.write&2),do: (f=r.(3,"Fizz");b=r.(5,"Buzz");f||b||r.(1,n);:io.nl)
# Other solutions:
for n<-1..?d,r=&(rem(n,&1)==0&&IO.puts&2),do: r.(15,"FizzBuzz")||r.(3,"Fizz")||r.(5,"Buzz")||r.(1,n)
for n<-1..?d,do: Enum.find [{15,"FizzBuzz"},{3,"Fizz"},{5,"Buzz"},{1,n}],fn({d,t})->rem(n,d)==0&&IO.puts(t)end
@henrik
henrik / test_helper.exs
Last active June 10, 2020 20:16
Improved `assert_compile_time_raise` based on this comment by Andrea Leopardi: http://andrealeopardi.com/posts/compile-time-work-with-elixir-macros/#comment-2347206739
ExUnit.start()
defmodule CompileTimeAssertions do
defmacro assert_compile_time_raise(expected_exception, expected_message, fun) do
# At compile-time, the fun is in AST form and thus cannot raise.
# At run-time, we will evaluate this AST, and it may raise.
fun_quoted_at_runtime = Macro.escape(fun)
quote do
assert_raise unquote(expected_exception), unquote(expected_message), fn ->
@henrik
henrik / example.coffee
Last active October 14, 2015 09:34
Simplistic CoffeeScript tweener for use e.g. with React. There's also https://github.com/chenglou/react-tween-state but that's more complex and seems not maintained at the time of writing.
tweener = new Tweener
# If the onChange callback changes the state of a React component, pretty things will happen.
tweener.tween
from: 1
to: 100
ms: 1000
steps: 25
onChange: (value) -> console.log("value is now #{value}")
defmodule ExMachina.With do
def create(model, attrs, {ExMachina.With, defaults}) do
full_attrs = Keyword.merge(defaults, attrs)
IO.puts "create #{model} with #{inspect full_attrs}"
end
end
defmodule ExMachina do
def with(defaults) do
{ExMachina.With, defaults}
@henrik
henrik / el_capitan.md
Last active May 1, 2023 18:24
El Capitan update: Ruby, Rails, homebrew, VirtualBox/Vagrant, Elixir etc.

My notes from updating to El Capitan, as a Ruby/Rails developer working in VirtualBox/Vagrant.

VirtualBox and Vagrant

Our dev environment currently requires us to use older versions of VirtualBox (4.3) and Vagrant (1.7.1).

It seems the new "SIP" protection messes with that version of VirtualBox.

I tried upgrading VirtualBox (to 5.0.6) and Vagrant (to 1.7.4). Those seemed to run fine on El Capitan, but we rely on Chef v. 10 and Vagrant doesn't seem to get along with it (error: "invalid option: --force-formatter").

IO.puts "My progress:"
1..100 |> Enum.each fn (i) ->
IO.write "\r[#{String.duplicate "=", i}] #{i} %"
:timer.sleep 25
end
IO.puts "\nDone!"