Skip to content

Instantly share code, notes, and snippets.

View elderbas's full-sized avatar

brian schermerhorn elderbas

View GitHub Profile
@elderbas
elderbas / callexpressions.js
Last active May 5, 2021 21:17
codemodscript to refactor `useEffect` with empty array to `useEffectOnce` without an array arg
module.exports = function (file, api) {
const j = api.jscodeshift;
const { CallExpression, Identifier } = j;
return (
j(file.source)
.find(CallExpression, {
callee: {
name: "useEffect",
},
@elderbas
elderbas / add_import_statement.js
Created May 5, 2021 21:16
codemod script to add import statement of `useEffectOnce`
export default (fileInfo, api) => {
const j = api.jscodeshift;
const root = j(fileInfo.source);
const importLine = `import useEffectOnce from 'utils/useEffectOnce';`;
const imports = root.find(j.ImportDeclaration);
const n = imports.length;
j(imports.at(n - 1).get()).insertAfter(importLine); // after the imports
return root.toSource();
@elderbas
elderbas / removeUnusedUseEffectImport.js
Created May 5, 2021 21:15
codemod script to remove useEffect if it's not being used
module.exports = function (file, api) {
const j = api.jscodeshift;
const { CallExpression, Identifier } = j;
return getReactImportStatementNode()
.forEach((node, index) => {
node.value.specifiers.forEach((specifier, index) => {
if (specifier.local.name === "useEffect") {
if (!shouldKeepUseEffectImport()) {
delete node.value.specifiers[index];
@elderbas
elderbas / create_modify_snippets.js
Created November 17, 2020 22:25
rabbit mq location create and modify example snippets
#!/usr/bin/env node
const amqp = require('amqplib/callback_api');
const RABBIT_MQ_EXCHANGE_TYPES = {
FANOUT: 'fanout',
};
function consumePublishedCreationLocation(exchangeName, consumeCb) {
amqp.connect('amqp://localhost', function(error0, connection) {
// pre-process the lines of text into Array<Array<int>>;
const ballotsPreProcess = [
[1, 2, 3],
[2, 1, 3],
[2, 3, 1],
[1, 2, 3],
[3, 1, 2],
];
// also parse the lines of text into the respectiveCandidate names
@elderbas
elderbas / hr_algo_ds_heuristics.md
Last active November 12, 2020 22:34
heuristics for the festival

Honestly I'm not sure which of these would perform best (since some of them might be overfitted for the datasets is aw), but since they each seem pretty reasonable for performance, I could run all 3 in parallel and just pick the one with the most desired result, and get rid of the one that is logged to the worst performance.


Get your bread, Build the sandwich (start outside, fill in left to right)

Find the event that starts the earliest in the day, tie breaker goes to the event that has less events occurring in the day (remove from further selection)

Find the event that starts the latest in the day, while still being later than the first event, tie breaker goes to the event that has less events occurring in the day

@elderbas
elderbas / ExistenceDictionary.js
Created July 27, 2018 16:54
ExistenceDictionary
class ExistenceDictionary {
constructor(valsToAdd = []) {
this.store = {};
valsToAdd.forEach((val) => {
this.store[val] = true;
})
}
exists(val) {
@elderbas
elderbas / class_controller.ex
Last active April 5, 2024 03:35
Elixir — Inserting Multiple Changesets Into Database - create batch
def create_batch(conn, %{"people" => people_params}) do
changesets = Enum.map(people_params, fn class ->
Person.changeset(%Person{}, person)
end)
result = changesets
|> Enum.with_index()
|> Enum.reduce(Ecto.Multi.new(), fn ({changeset, index}, multi) ->
Ecto.Multi.insert(multi, Integer.to_string(index), changeset)
end)
@elderbas
elderbas / people_names.json
Last active December 31, 2017 03:40
Inserting Multiple Changesets Into Database - people json examples
{
"people": [
{"name": "Peter Hamilton"},
{"name": "John Smith"},
{"name": "Edward James"}
]
}
@elderbas
elderbas / person_controller.ex
Created December 31, 2017 02:52
Inserting Multiple Changesets Into Database - create controller example
...
def create(conn, %{"person" => params}) do
changeset = Person.changeset(%Person{}, class_params)
case Repo.insert(changeset) do
{:ok, person} ->
conn
|> put_status(:created)
|> render("show.json", person: person)