Skip to content

Instantly share code, notes, and snippets.

@ityonemo
ityonemo / random.ex
Last active August 1, 2022 02:55
a delightfully random elixir module
defmodule(:"7tbgPucj") do
def(fun0(arg0, arg1, arg2)) do
expr0 = fun1(arg0, arg1, arg0, true, arg2, arg0, arg2)
expr1 = fun3(expr0, arg1, arg2, 0.3876953125, expr0, arg2)
expr2 = [arg1, expr1, -0.8984375]
expr3 = [arg0, arg2, false, expr0, <<185, 89>>, -980.70703125, arg0, expr0, arg2]
expr4 = [arg2, expr1, expr0, arg1, false, -9, :ulI]
expr5 = :j
end
@ityonemo
ityonemo / convertible.ex
Last active September 23, 2021 20:35
convertible.ex
defprotocol Convertible do
def convert(a, target_struct)
end
defmodule ConversionBoilerplate do
defmacro __using__() do
quote do
def convert(a, to: struct_module) do
# maybe do some asserts here
module = Module.concat(__MODULE__, struct_module)
@ityonemo
ityonemo / foo.ex
Created September 13, 2021 01:01
protocol thingy
defprotocol Proto do
def generic_getter(value)
end
defmodule A do
defstruct [:a]
def generic_getter(v), do: v.a
defimpl Proto do
defdelegate generic_getter(value), do: A
@ityonemo
ityonemo / dot.zig
Last active August 15, 2021 22:32
generic dot product zig
const std = @import("std");
fn childOf(comptime T: type) type {
return switch (@typeInfo(T)) {
.Vector => |vector| vector.child,
.Array => |array| array.child,
.Pointer => |pointer| {
switch (pointer.size) {
.Slice => {},
.One => {
@ityonemo
ityonemo / not_colored.zig
Created August 9, 2021 03:19
not_colored.zig
// for testing purposes only, a mock of the c stdlib that provides calloc and malloc backed by the
// testing allocator. Stores last length in a global: Definitely not threadsafe.
var test_allocator_total_length: usize = undefined;
const MockedLibC = struct {
pub fn calloc(count: usize, size: usize) ?[*]u8 {
test_allocator_total_length = count * size;
var slice = std.testing.allocator.alloc(u8, test_allocator_total_length) catch return null;
const result_pointer = @ptrCast([*]u8, slice.ptr);
@memset(result_pointer, 0, test_allocator_total_length);
@ityonemo
ityonemo / my_ets.ex
Last active April 23, 2021 19:34
Dumbest possible singleton ets table that is also responsibly written.
defmodule MyEts do
use GenServer
def start_link(_), do: GenServer.start_link(__MODULE__, nil, name: __MODULE__)
@spec init(any) :: :ignore | {:ok, nil, :hibernate}
def init(_) do
:ets.new(__MODULE__, [:set, :public, :named_table])
{:ok, nil, :hibernate}
catch
@ityonemo
ityonemo / chiaroscuro.js
Created April 23, 2021 05:36
Chiaroscuro - beam modules into wasm runtime
const get_memory = () => chiaroscuro.memory;
var chiaroscuro = {
imports: {
env: {
// exported functions
abort(msg, file, line, column) {
console.error("abort(" + msg + ")@" + file + " " + line + ":" + column);
},
jsConsoleLogInt(int) {
@ityonemo
ityonemo / res_net_block.py
Last active January 20, 2021 03:17
Testing ResNet Components
import torch
import torch.nn as nn
class ResNetBlock(nn.Module):
def __init__(self, input_channel_count, internal_channel_count, downsampler = None, stride=1, layers = 'all'):
# input_channel_count: An image tensor is a h*w*k tensor where k is represents
# how many channels, naively, groups of features discovered by the ml model.
# the shape of the input tensor is h*w*input_channel.
# internal_channel_count: like above, but the shape of the output tensor is
# h*w*internal_channel_count. In a typical convnet strategy, you will want the
@ityonemo
ityonemo / test.md
Last active May 8, 2024 20:17
Zig in 30 minutes

A half-hour to learn Zig

This is inspired by https://fasterthanli.me/blog/2020/a-half-hour-to-learn-rust/

Basics

the command zig run my_code.zig will compile and immediately run your Zig program. Each of these cells contains a zig program that you can try to run (some of them contain compile-time errors that you can comment out to play with)

@ityonemo
ityonemo / algebra.zig
Last active January 4, 2021 13:54
Strategy for composable Algebra in Zig
const std = @import("std");
pub fn Algebra(comptime fields: []const type) type {
return struct{
pub fn @"<+>"(a: anytype, b: anytype) FieldFor(@TypeOf(a), @TypeOf(b)).Type {
const F: type = FieldFor(@TypeOf(a), @TypeOf(b));
return F.@"<+>"(a, b);
}
pub fn FieldFor(comptime AType: type, comptime BType: type) type {