This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
collection = [ | |
{ | |
'contract_number' => '123456', | |
'contract_level' => 1, | |
'contact_name' => 'craig' | |
}, { | |
'contract_number' => '654321', | |
'contract_level' => 2, | |
'contact_name' => 'david' | |
}, { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Function: pythagorean Triples | |
* | |
* How to derive the side_b formula: | |
* - a² + b² = c² | |
* - b² = c² - a² | |
* - 2bc + b² + b² = c² - a² + 2bc + b² | |
* - 2bc + b² + b² = c² + 2bc + b² - a² // Line above reorganized to see simplification | |
* - 2bc + b² + b² = (b + c)² - a² | |
* - 2b² + 2bc = (b + c)² - a² |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Setoid<T> { | |
equals(that: T): Boolean; | |
} | |
interface Ord<T> extends Setoid<T> { | |
lte(that: T): Boolean; | |
} | |
class Coord implements Ord<Coord> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const buildString = (delimeter: string, ...collection: any[]): string => | |
collection.filter(Boolean).join(delimeter); | |
console.log(buildString('/', 'api', undefined, 'users', 123)); // 'api/users/123' |
NewerOlder