Skip to content

Instantly share code, notes, and snippets.

View MainShayne233's full-sized avatar
🎈
sleepy

shayne MainShayne233

🎈
sleepy
View GitHub Profile
@MainShayne233
MainShayne233 / cursed_client.ex
Last active July 19, 2020 21:47
A cursed Elixir macro that will retry code execution
defmodule CursedClient do
import CursedLib, only: [retry: 2]
def make_money do
retry times: 10, timeout: 1_000, on_error: &IO.inspect/1 do
[
fn -> {1_000_000, :dollars} end,
fn -> raise "You just got sued!" end,
fn -> raise "Your investors pulled out!" end,
fn -> raise "Your entire software team left all at once!" end,
@MainShayne233
MainShayne233 / input.txt
Created May 19, 2020 02:35
Example of using scanf to read file.
7
14
@MainShayne233
MainShayne233 / async_task_runner.ex
Created May 14, 2020 21:34
Inspectable task runner
defmodule MyApp.AsyncTaskRunner do
use Supervisor
defmodule Worker do
use GenServer
@ten_minutes 1000 * 60 * 10
def start_link(opts) do
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
@MainShayne233
MainShayne233 / meta.py
Last active April 10, 2020 01:00
Dynamically super to instance methods
class Foo:
def a(self):
return "a"
def add(self, x, y):
return x + y
class Bar:
def __init__(self):
@MainShayne233
MainShayne233 / intercept_def.ex
Last active April 3, 2020 18:19
Example of how to intercept an Elixir def call
defmodule InterceptDef do
@moduledoc """
An examble of how you can intercept a def call at
compile time to do something intermediate with the
definition.
"""
defmacro __using__([]) do
quote do
@MainShayne233
MainShayne233 / rc_list_parcer.rs
Created September 13, 2019 03:49
Simple lisp parser implemented in rust
#![feature(box_syntax, box_patterns)]
//////////////////////////////////////////////////////////////////////////////////////////////////
// Lisp parser //
// //
// Write code that takes some Lisp code and returns an abstract syntax tree. The AST should //
// represent the structure of the code and the meaning of each token. For example, if your code //
// is given "(first (list 1 (+ 2 3) 9))", it could return a nested array //
// like ["first", ["list", 1, ["+", 2, 3], 9]]. //
//////////////////////////////////////////////////////////////////////////////////////////////////
@MainShayne233
MainShayne233 / flipper.ex
Created September 3, 2019 15:36
Feature flipping macro in Elixir
defmodule Flipper do
defmacro deffeature(func, do: do_block, else: else_block) do
{function_name, _, _} = func
quote do
def unquote(func) do
if Flipper.is_enabled?(unquote(function_name)) do
unquote(do_block)
else
unquote(else_block)
end
@MainShayne233
MainShayne233 / crackle_pop
Created August 30, 2019 02:03
CracklePop solution in bash
#!/usr/bin/env bash
set -CEeuo pipefail
IFS=$'\n\t'
shopt -s extdebug
crackle_pop() {
local start="$1"
local end="$2"

Keybase proof

I hereby claim:

  • I am mainshayne233 on github.
  • I am mainshayne233 (https://keybase.io/mainshayne233) on keybase.
  • I have a public key ASCZgTyHKGvRKnqWxBTeLLfxbBhrvtaBey7O_E-6N5E-EAo

To claim this, I am signing this object:

@MainShayne233
MainShayne233 / concurrent_mapping.exs
Last active April 11, 2019 17:06
Concurrent Mapping in Elixir
# Normal map
data = [3, 6, 34, 7, 4, 23, 6]
expensive_function = fn x ->
:timer.sleep(500)
5 / x
end
Enum.map(data, expensive_function)