Skip to content

Instantly share code, notes, and snippets.

View rcdilorenzo's full-sized avatar
🙌
Working in His Kingdom (Col 3:17)

Christian Di Lorenzo rcdilorenzo

🙌
Working in His Kingdom (Col 3:17)
View GitHub Profile
@rcdilorenzo
rcdilorenzo / covariance.py
Last active April 18, 2022 15:40
How to calculate a covariance matrix
import numpy as np
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
def covariance(M: np.ndarray) -> np.ndarray:
"""
Compute sample covariance matrix from data M.
NOTE: M is assumed to be of shape (nrows, ncols).
@rcdilorenzo
rcdilorenzo / convertToJSONSchema.test.ts
Last active November 18, 2021 00:25
Helper to convert io-ts types to JSON Schema (to be extracted to open source NPM package)
import * as t from 'io-ts';
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
import { fromNullable } from 'io-ts-types/lib/fromNullable';
import convertToJSONSchema from './convertToJSONSchema';
import isoString from './isoString';
const SampleObject = t.strict(
{
aString: t.string,
import numpy as np
from typing import Callable, Optional, List
from pydantic import BaseModel
from tqdm.notebook import tqdm
from math import ceil, isnan, inf
class Activation(BaseModel):
forward: Callable[[np.ndarray], np.ndarray] # input -> output
backward: Callable[[np.ndarray, np.ndarray], np.ndarray] # (dA, X) -> gradients
@rcdilorenzo
rcdilorenzo / json_map_builder.ex
Last active October 25, 2021 08:19
Convert a elixir json-decoded object to a map no matter how deep
defmodule JSONMapBuilder do
def to_map(list) when is_list(list) and length(list) > 0 do
case list |> List.first do
{_, _} ->
Enum.reduce(list, %{}, fn(tuple, acc) ->
{key, value} = tuple
Map.put(acc, binary_to_atom(key), to_map(value))
end)
_ ->
list
@rcdilorenzo
rcdilorenzo / LoopExample.asm
Last active August 7, 2021 18:51
Creating a MASM loop with the condition either above or below the block to execute (e.g. a while vs do..while loop in C)
; Description:
; This program demonstrates how to
; create a while-style loop with the
; condition either above or below
; the block of code to execute.
; Author: Christian Di Lorenzo
INCLUDE Irvine32.inc
@rcdilorenzo
rcdilorenzo / change-harvest-format.md
Created November 13, 2017 16:54
Change format for newer (2017) Harvest installs from the App Store

To read your settings, run:

defaults read ~/Library/Containers/com.getharvest.harvestxapp/Data/Library/Preferences/group.com.getharvest.Harvest.Documents.plist

To change the time format to HH:MM, run

defaults write ~/Library/Containers/com.getharvest.harvestxapp/Data/Library/Preferences/group.com.getharvest.Harvest.Documents.plist TimeFormat hours_minutes

Restart the Harvest App, and it should now display HH:MM (I tested this)

@rcdilorenzo
rcdilorenzo / macro_fun.exs
Created January 30, 2015 03:27
Macro fun in Elixir mimicking Ruby's attr_accessor
defmodule MacroExp do
defmacro attr_accessor(atom) do
getter = String.to_atom("get_#{atom}")
setter = String.to_atom("set_#{atom}")
quote do
def unquote(getter)(data) do
data |> Map.from_struct |> Map.get(unquote(atom))
end
def unquote(setter)(data, value) do
data |> Map.put(unquote(atom), value)
@rcdilorenzo
rcdilorenzo / worldclass-save-discussions.js
Created March 16, 2019 18:34
A puppeteer script to download all discussions for a given WorldClass topic (assumes WORLDCLASS_DOMAIN environment is set)
const puppeteer = require('puppeteer');
const read = require('read');
const htmlToText = require('html-to-text').fromString;
const R = require('ramda');
const Promise = require('bluebird');
const fs = require('fs');
const download = require('@jinphen/download2');
const { CookieJar } = require('tough-cookie');
const mapSeries = R.flip(Promise.mapSeries);
@rcdilorenzo
rcdilorenzo / duration.ex
Created February 20, 2016 01:20
A simple construction of a duration time (e.g. "01:30" or "01:30:23") as a custom ecto type
defmodule Duration do
@moduledoc """
This duration module parses and formats strings
for a time duration in hours and minutes and
optionally seconds (e.g. 01:00 for an hour,
00:01:10 for one minute and ten seconds).
"""
@match ~r/^(?<hour>\d{1,2}):(?<min>\d{1,2}):?(?<sec>\d{0,2})$/
def parse(string) do
@rcdilorenzo
rcdilorenzo / utils.js
Created November 16, 2018 01:11
Transpose list of objects to an object with each property as a list of the values
// ========
// Usage
// ========
// > const { transposeObjects } = require('./utils');
// undefined
// > transposeObjects([
// ... { key1: 'object1key1', key2: 'object1key2' },
// ... { key1: 'object2key1', key2: 'object2key2' }
// ... ])
// { key1: [ 'object1key1', 'object2key1' ],