Skip to content

Instantly share code, notes, and snippets.

@ethanresnick
ethanresnick / src.ts
Created January 20, 2020 09:12
Any value monoid
/**
* This is a monoid over _all elements in the JS programming language_.
* It's not really useful on its own, and it's a bit of a hack, but it's valid
* and maybe kinda cool. Here's how it works:
*
* 1) it lifts non-lists into a list, like:
*
* 1234 `mappend` { a: true } === [1234, { a: true }]
*
* 2) then, if given a list, it extracts the items from the list before adding
@ethanresnick
ethanresnick / index.js
Last active January 23, 2019 19:27
Fix "JSON" that contains invalid strings
// Matches two double quotes and any characters between them, without stopping
// at backslash-escaped double quotes that appear in the middle. This is a lot
// like JS's string literal syntax, except that it will some match characters
// between the double quotes that JS would require be backslash escaped --
// most notably, the newline, which must be \n in string literals.
// Note: we use [^] instead of . below to match any character because JS
// doesn't change the meaning of the dot even in the precense of the
// multiline flag.
const STRING_LITERAL_LIKE = /"([^"\\]|\\[^])*"/g;
/**
* reducer :: a -> b -> a
*
* reduce :: Iterable f => (a -> b -> a) -> f b -> a -> a
*
* makeMappingTransducer :: (a -> b) -> (c -> b -> c) -> (c -> a -> c)
*/
const makeMappingTransducer = (transformer) => (reducer) => {
return (acc, input) => reducer(acc, transformer(input))
}
@ethanresnick
ethanresnick / test.js
Created November 22, 2016 01:32
Tail recursive inner functions to remove accumulator vars from the api
// Non tail recursive; will have a stack overflow.
function sum(list) {
if(list.length === 0) {
return 0;
}
return list[0] + sum(list.slice(1));
}
// Naive tail recursive (ugly api)
[{"title":"Building Your First Web App","shortTitle":"Building Your 1st Web App","description":"An overview of the technologies required to make a web app.","details":"For people who may or may not know how to program, this event offers an overview of the different parts of a web app are and how they fit together. It will be followed by HTML & CSS workshops for designers looking to become productive frontend coders. Meanwhile, people looking to learn backend coding will be pointed to appropriate resources. By the end of the event, both technical and non-technical people will know how to get the skills required to contribute to a web app project a hackathon.","startDateTime":"2014-11-01T16:30:00.000Z","endDateTime":"2014-11-01T17:30:00.000Z","rsvpUrl":"https://www.facebook.com/events/1514033535502764/","modified":"2014-10-19T04:26:36.990Z","created":"2014-10-17T00:24:53.217Z","isPlaceholder":false,"id":"544061d56b0287336dfc51d4","links":{"venue":"53f54dd98d1e62ff12539dab","teams":["5440609d6b0287336dfc51cf","5
@ethanresnick
ethanresnick / gist:fcb87a475d02d998b2fd
Last active August 29, 2015 14:07
Add Organization
{
"organizations": {
"name": "StackExchange",
"url": "http://stackexchange.com/",
"description": "StackExchange runs a network of amazing Q&A sites including StackOverflow."
}
}
{
"links": {
"venues.company": {
"type": "companies"
}
},
"venues": {
"name":"Stack Exchange",
"kind":"company_office",
{
"links": {
"presenters.currentEmployer": {
"type": "companies"
}
},
"presenters": [{
"twitterId": "20961134",
"name": "Jon Chan",
//forms help text
jQuery(document).ready(function($) {
var forms = $('form'), className = 'visually-hidden';
//hide all help-text and make links in help text untabbable (because the text itself is hidden)
$('.help-text:not(.help-text-group)', forms).addClass(className).find('a').attr('tabindex', '-1');
//listen to any changes of focus within our forms (uses event delegation for speed)
forms.delegate("input, select, texarea, .tinymce", "focusin focusout", function(event) {
var $this = $(this), labelId, helpText;
@ethanresnick
ethanresnick / gist:a45c4016216a39163c85
Created October 5, 2014 22:08
Character Count Directive Base
//maxlength inputs
$.webshims.ready('DOM forms', function() {
$('input[maxlength]').each(function(i, elem) {
var $elem = $(elem);
//1. Remove maxlength attribute and replace it with a data-maxlength so that the browser will let user type beyond the maximum, which is better usability,
// but then webforms will use a custom constraint to mark the field as invalid (even wihtout maxlength) if the user tries to leave it with a value that's too long
$elem.attr('data-maxlength', $elem.attr('maxlength')).removeAttr('maxlength');
//2. Set up the character counter's HTML
$elem.add('label[for='+ $elem.attr('id') +']').wrapAll('<div class="input-maxlength wrapper" />')
.filter('label').append('<span class="remaining-chars help-text disabled">' + ($elem.attr('data-maxlength') - $elem.val().length) + '</span>');