Skip to content

Instantly share code, notes, and snippets.

@blrobin2
blrobin2 / autosave_associated_many_to_many.rb
Created January 6, 2021 15:16
A Rails model mix-in for handling many-to-many updates and nested attributes
module AutosaveAssociatedManyToMany
extend ActiveSupport::Concern
# When using a combination of autosave and accepts nested_attributes_for for a many-to-many association
# By default, Rails always creates new records
# If you have unique indexes on the association keys on your bridge table, you'll just get errors
# By defining a matcher, you provide a way to determine whether a "new" record is actually an existing record
# By defining an updater, you provide a way to update values on a many-to-many (changing an association to a new field
# or updating any additional fields on the bridge table)
#
@blrobin2
blrobin2 / complex_sorting.rb
Created August 11, 2020 14:33
How to do combination ASC, DESC sorting in Ruby
collection = [
{
'contract_number' => '123456',
'contract_level' => 1,
'contact_name' => 'craig'
}, {
'contract_number' => '654321',
'contract_level' => 2,
'contact_name' => 'david'
}, {
@blrobin2
blrobin2 / end_of_time.js
Created May 30, 2020 13:15
A way of representing the concept of a MAX_DATE or END_OF_TIME constant
const END_OF_TIME = new Date(parseInt('9'.repeat(15), 10))
// Thu Sep 26 33658 21:46:39 GMT-0400 (Eastern Daylight Time)
// Example usage: default for date comparisons where you want to ensure the default is greater than
// any of the provided dates
const dateFromMMDDYYYY = mmddyyyy => new Date(Date.parse(mmddyyyy))
const dates = ['02/29/1984', '03/01/2055', '02/29/2244']
const earliest_date = dates.map(dateFromMMDDYYYY)
.reduce(
@blrobin2
blrobin2 / pythagorean-triples.ts
Created April 9, 2019 18:50
Pythagorean Triples with MATH
/**
* Function: pythagorean Triples
*
* How to derive the side_b formula:
* - + =
* - = -
* - 2bc + + = - + 2bc +
* - 2bc + + = + 2bc + - // Line above reorganized to see simplification
* - 2bc + + = (b + c)² -
* - 2 + 2bc = (b + c)² -
@blrobin2
blrobin2 / ord-sort.ts
Created December 7, 2018 13:44
Ord-based Bubble sort with TYPES
interface Setoid<T> {
equals(that: T): Boolean;
}
interface Ord<T> extends Setoid<T> {
lte(that: T): Boolean;
}
class Coord implements Ord<Coord> {
@blrobin2
blrobin2 / ord-sorts.js
Last active June 12, 2022 09:26
Ord-based Bubble, Merge, and Quick Sort
// A demonstration of sorting objects with Ord instances
// equals :: Setoid a => a ~> a -> Boolean
// lte :: Ord a => a ~> a -> Boolean
/*
* Data Constructors, and their Setoid and Ord instances
*/
// I'm using the daggy library to make constructors https://github.com/fantasyland/daggy
const daggy = require('daggy')
@blrobin2
blrobin2 / composing-monads.hs
Last active August 29, 2018 19:58
Implement join :: M (N (M (N a))) -> M (N a), given distrib :: N (M a) -> M (N a) and assuming M and N are instances of Monad
import qualified Control.Monad as M (join)
-- Not necessarily possible function, but if it is, then joining on Monad stack is possible
distrib :: (Monad m, Monad n) => n (m a) -> m (n a)
distrib = undefined
join :: (Monad m, Monad n) => m (n (m (n a))) -> m (n a)
join mnmna = do
nmna <- mnmna
nna <- distrib nmna
@blrobin2
blrobin2 / url.js
Created July 9, 2018 18:46
URL string building helpers
function createUrl(base, paramObj) {
return [base, createUrlParams(paramObj)].join(base.includes('?') ? '&' : '?')
}
function createUrlParams(paraObj) {
return Object.entries(paramObj).map(([key, val]) => `${key}=${val}`).join('&')
}
// Sample usage
const url = createUrl('http://localhost:3000', { redirect: true })
const R = require("ramda");
const table = require("text-table");
const kelvinToCelcius = k => k - 273.15;
const kelvinToFahrenheit = k => k * 9 / 5 - 459.67;
const updateTemperature = R.curry((conversionFn, city) => {
const updatedTemperature = Math.round(conversionFn(city.temp));
return R.merge(city, { temp: updatedTemperature });
});
@blrobin2
blrobin2 / buildString.ts
Last active January 8, 2018 00:25
My version of build string by @peterpme
const buildString = (delimeter: string, ...collection: any[]): string =>
collection.filter(Boolean).join(delimeter);
console.log(buildString('/', 'api', undefined, 'users', 123)); // 'api/users/123'