Skip to content

Instantly share code, notes, and snippets.

View jayrbolton's full-sized avatar
🍕

Jay R Bolton jayrbolton

🍕
View GitHub Profile
@jayrbolton
jayrbolton / Promises
Created June 10, 2015 19:12
Explanation of Promises
The primary way of interacting with a promise is through its `then` method,
which registers callbacks to receive either a promise's eventual value or the
reason why the promise cannot be fulfilled.
```js
findUser().then(function(user){
// user is available
}, function(reason){
// user is unavailable, and you are given the reason why
});
@jayrbolton
jayrbolton / stateful-dom
Last active October 15, 2019 22:17
state-based dom manipulation where dom elements get bound to special ui state objects
The goals of this view library are:
- bridge the gap between js-land and html-land without mucking up the syntax and semantics of either
- little to no overhead to setup and use
- no precompilation, no transpiling, lightweight, fast
- declarative
- unit-testable UI behavior
The ideas:
- a lot like virtual-dom except more abstracted
- use plain JS objects and their regular JS semantics to define *view states* that affect the dom
{-
In Haskell
-}
primeFactors n = recur 2 [] n
where
recur divisor facs limit
| divisor > limit = facs
| limit `mod` divisor == 0 = recur divisor (divisor:facs) (limit `div` divisor)
| otherwise = recur (divisor+1) facs limit
{- In Haskell, using lazy list comprehension -}
isPalindrome str = even (length str) && (take half str) == (reverse $ drop half str)
where half = length str `div` 2
answer = maximum [x*y | x <- [100..999], y <- [100..999], (isPalindrome $ show (x*y))]
/* In JS, the imperative way */
function isPalindrome(n) {
@jayrbolton
jayrbolton / virtual-dom-convenience-wrapper.js
Last active September 8, 2015 06:27
Virtual-dom quick setup wrapper
// This is a minimalistic way to easily use virtual-dom and avoid repeating any boilerplate code
// It's a simple object wrapper with a small api that you can use to set new data and re-render quickly and easily.
// Pass in a parentNode (like document.body), a rootComponent (a function that takes a state and returns a VTree), and an initial state object.
function view(parentNode, rootComponent, initialState) {
this.state = initialState
this.tree = rootComponent(initialState)
this.rootNode = createElement(this.tree)
parentNode.appendChild(this.rootNode)
return this
// project euler #7
//
// By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
//
// What is the 10,001st prime number?
// Approach I came up with after a while:
// - I remembered Sieve of Eratosthenes but couldn't figure out the exact idea without lazy lists
// - So instead I just keep track of all the previous primes and test the modulo on each prime in every loop
// - Only iterate on odd numbers, easy elimination of half the set
// project euler 8
// jay r bolton 12/26
//
// The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
//
// 73167176531330624919225119674426574742355349194934
// 96983520312774506326239578318016984801869478851843
// 85861560789112949495459501737958331952853208805511
// 12540698747158523863050715693290963295227443043557
// 66896648950445244523161731856403098711121722383113
// project euler #9
// jay r bolton 1/5/2016
//
//
// A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
//
// a2 + b2 = c2
// For example, 32 + 42 = 9 + 16 = 25 = 52.
//
// There exists exactly one Pythagorean triplet for which a + b + c = 1000.
@jayrbolton
jayrbolton / temperature-converter.js
Last active September 13, 2016 09:49
flyd + snabbdom component example (fahrenheit/celsius converter example)
import flyd from 'flyd'
import h from 'snabbdom/h'
import snabbdom from 'snabbdom'
import render from 'ff-core/render'
// Initialize the state object
const init = ()=> {
// Initialize input change event streams
// Think of streams as values that change over time.
// These two streams are input values that change over time, but start empty.
@jayrbolton
jayrbolton / snabbdom-transformer.js
Last active February 3, 2017 16:59
Snabbdom transformer idea
// this is how you could create composable functions that modify and extend VNodes
// - append or prepend children
// - add surrounding VNode wrappers
// - compose hook and event functions
// - merge props, attrs, style, and class
// snabbdom-transform objects can have these properties
// - wrapper: function that takes the vnode and allows you to add surrounding elements
// - class, style, and attrs: these will merge with existing data using R.merge
// - on, hook: these functions will merge and get composed with existing on/hook functions without overwriting any