Skip to content

Instantly share code, notes, and snippets.

View hollance's full-sized avatar

Matthijs Hollemans hollance

View GitHub Profile
@JadenGeller
JadenGeller / Fraction.swift
Last active January 5, 2023 06:28
Fractions in Swift
// Now availbile as a Swift package on GitHub!
// https://github.com/jadengeller/fractional
public typealias Fraction = Fractional<Int>
private func gcd<Number: IntegerType>(var lhs: Number, var _ rhs: Number) -> Number {
while rhs != 0 { (lhs, rhs) = (rhs, lhs % rhs) }
return lhs
}
@calebd
calebd / ArrayHelpers.swift
Last active November 4, 2022 15:17
Swift Helpers
extension Array {
func first() -> Element? {
if isEmpty {
return nil
}
return self[0]
}
func last() -> Element? {
@pcuenca
pcuenca / MakeContiguousKernel.metal
Created July 29, 2022 12:29
Metal Kernel to make a contiguous copy of MLMultiArray storage
//
// MakeContiguousKernel.metal
//
// Created by Pedro Cuenca on 20220307.
// Copyright © 2022 LateNiteSoft S.L. All rights reserved.
//
#include <metal_stdlib>
using namespace metal;
@kendricktan
kendricktan / capsule_networks.py
Last active August 17, 2021 17:12
Clean Code for Capsule Networks
"""
Dynamic Routing Between Capsules
https://arxiv.org/abs/1710.09829
"""
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision.transforms as transforms
@petewarden
petewarden / convert_cc_to_tflite.py
Created February 27, 2020 23:55
Example of converting a .cc TensorFlow Lite C data array file back into a binary flatbuffer on disk
import re
output_data = bytearray()
with open('tensorflow/tensorflow/lite/micro/examples/magic_wand/magic_wand_model_data.cc', 'r') as file:
for line in file:
values_match = re.match(r"\W*(0x[0-9a-fA-F,x ]+).*", line)
if values_match:
list_text = values_match.group(1)
values_text = filter(None, list_text.split(","))
values = [int(x, base=16) for x in values_text]
output_data.extend(values)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mikeash
mikeash / blockforward.m
Created October 21, 2011 02:38
NSInvocation works for blocks too!
#import <dlfcn.h>
#import <Foundation/Foundation.h>
struct BlockDescriptor
{
unsigned long reserved;
unsigned long size;
void *rest[1];
dispatch_block_t RecursiveBlock(void (^block)(dispatch_block_t recurse))
{
// assuming ARC, so no explicit copy
return ^{ block(RecursiveBlock(block)); };
}
typedef void (^OneParameterBlock)(id parameter);
OneParameterBlock RecursiveBlock1(void (^block)(OneParameterBlock recurse, id parameter))
{
@ingramj
ingramj / brainfuck.rb
Created February 25, 2009 00:37
A Brainfuck interpreter written in Ruby.
#!/usr/bin/env ruby
class BrainFuck
def initialize
@ops = create_ops
@tape = Array.new(1024,0)
@tp = 0
@code = []
@cp = 0
end
//
// NudgeModel.h
// NudgeFoundation
//
// Created by Jeremy Tregunna on 10/7/2013.
// Copyright (c) 2013 Jeremy Tregunna. All rights reserved.
//
#import <Foundation/Foundation.h>