Skip to content

Instantly share code, notes, and snippets.

View maxjustus's full-sized avatar

Max Justus Spransy maxjustus

  • People With Jetpacks
  • Los Angeles, CA
View GitHub Profile
@maxjustus
maxjustus / parmap.go
Created September 13, 2023 00:20
golang parallelMap
func parallelMap[T any, V any](input []T, f func(T) (V, error)) ([]V, error) {
outChan := make(chan V, len(input))
errChan := make(chan error, len(input))
for i, val := range input {
go func(i int, val T) {
out, err := f(val)
if err != nil {
errChan <- err
} else {
@maxjustus
maxjustus / as-source.tsx
Created November 5, 2020 23:37
A stencil functional component for rendering the children element's markup in a <code> element along side the rendered jsx
import { h, ChildNode, FunctionalComponent, FunctionalUtilities } from "@stencil/core";
function vnodeToSource(elem: ChildNode, utils: FunctionalUtilities, output = "", level = 0): string {
if (elem.vchildren) {
let childOutput: string[] = [];
utils.forEach(elem.vchildren, (child) => {
childOutput.push(vnodeToSource(child, utils, output, level + 1));
});
@maxjustus
maxjustus / openssl_tls_1.2.patch
Last active March 15, 2017 20:58 — forked from kriansa/openssl_tls_1.2.patch
Source patch to add support to TLS 1.1 and 1.2 to Ruby 1.9.3
diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c
--- a/ext/openssl/ossl_ssl.c
+++ b/ext/openssl/ossl_ssl.c
@@ -107,6 +107,18 @@
OSSL_SSL_METHOD_ENTRY(TLSv1),
OSSL_SSL_METHOD_ENTRY(TLSv1_server),
OSSL_SSL_METHOD_ENTRY(TLSv1_client),
+#if defined(HAVE_TLSV1_2_METHOD) && defined(HAVE_TLSV1_2_SERVER_METHOD) && \
+ defined(HAVE_TLSV1_2_CLIENT_METHOD)
+ OSSL_SSL_METHOD_ENTRY(TLSv1_2),
@maxjustus
maxjustus / active_record_extensions.rb
Created January 6, 2016 20:08
Little active record extension to make zero downtime column removal a lil more convenient in Rails
# Put this in config/initializers
module ActiveRecord
class Base
def self.removed_columns(*names)
@@removed_columns = Set.new(names.map(&:to_s))
instance_eval do
def columns
cols = super
cols.reject { |col| @@removed_columns.include?(col.name) }
@maxjustus
maxjustus / locking_methods_bench.rb
Last active November 10, 2015 19:50
Benchmark for db locking approaches
require 'benchmark/ips'
carts = 10.times.map { Cart.create! account: Account.first }
carts.each { |cart|
items = Account.first.product_options.sample(5)
items.each { |item| cart.line_items.create product_option_id: item.id, quantity: 1 }
}
def all_at_once(carts, &blk)
@maxjustus
maxjustus / ranked_sample.rb
Last active September 25, 2015 18:38
Ranked array sample thingey
class Array
def ranked_sample(count)
slice_size = length / count
count.times.reduce([]) do |a, i|
i += 1
to_take = i == count ? length : slice_size * i
v = (take(to_take) - a).sample
a << v if v
a
end
@maxjustus
maxjustus / benchmark_output
Last active September 10, 2015 22:04
Quickly finding count of common elements in same length arrays in Ruby using RubyInline
Calculating -------------------------------------
ruby version 369 i/100ms
c version 3413 i/100ms
-------------------------------------------------
ruby version 3977.2 (±4.5%) i/s - 19926 in 5.020612s
c version 450675.5 (±9.4%) i/s - 2221863 in 4.986029s
~/dev/theclymb$ ./bin/rails runner bench.rb
Calculating -------------------------------------
ruby version 360 i/100ms
c version 3385 i/100ms
@maxjustus
maxjustus / rspec fire active record haxe.rb
Created September 5, 2015 17:01
hack to allow rspec fire to play nice with active record model column methods
# Rspec fire can only mock methods that are defined on a class.
# The way active record defines accessors is incompatible with rspec-fire's
# implementation so this works around that by defining an accessor for every
# attribute as soon as each model class is loaded for the first time
#
# TODO: See if this is needed with Rails 4
class ActiveRecord::Base
class << self
alias :__ar_original_inherited__ :inherited
@maxjustus
maxjustus / gist:0b87c0df16db64e365c2
Created June 29, 2015 21:15
Non-negative matrix factorization in Lua
-- Implements the algorithm described here http://www.quuxlabs.com/blog/2010/09/matrix-factorization-a-simple-tutorial-and-implementation-in-python/
-- by translating http://www.quuxlabs.com/wp-content/uploads/2010/09/mf.py_.txt into the equivalent lua code (I hope)
--
-- INPUT:
-- R : a matrix to be factorized, dimension N x M
-- P : an initial matrix of dimension N x K
-- Q : an initial matrix of dimension M x K
-- K : the number of latent features
-- steps : the maximum number of steps to perform the optimisation
-- alpha : the learning rate
@maxjustus
maxjustus / gist:9339313
Last active August 29, 2015 13:56 — forked from sdliv/gist:9339247
class CreateResumeDesigns < ActiveRecord::Migration
def up
change_table :resume_designs do |t|
t.string :purpose
end
end
def down
end
end