This is a SCRIPT-8 cassette.
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
// Start the connection to the WebSocket server at echo.websocket.org | |
ws = new WebSocket("ws://echo.websocket.org/"); | |
// Register event listeners for the open, close, and message events | |
ws.onopen = () => { | |
console.log("WebSocket ready!"); | |
console.log('yo') |
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
id = x => x | |
results = [1,'',null].filter(id) | |
console.log(JSON.stringify(results, null, 2)) | |
// => [ 1 ] |
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
// Finally wrapped your head around Promises? Time to toss out all that knowledge and learn the functional alternative! | |
// Here's a super simple implementation of a Task "type" | |
const __Task = fork => ({fork}) | |
// Absurdly simple! All we're doing is using a function that returns some unknown value, by name, in an object. | |
// At this point "fork" is just a cute name though: what matters is how we use it. | |
// What makes Task a "Task" is that is that the "fork" value here... will be a higher-order function. | |
// Here's a usage example, already fully functional, already nearly as powerful as Promise! |
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
// Simple example, but the idea holds for more complex objects. | |
/* 1) Start with OO */ | |
// user.js | |
class User { | |
constructor(firstName, lastName, email) { | |
this.firstName = firstName | |
this.lastName = lastName |
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
//Task: Transform this simple sorting algorithm into a unique sort. | |
// It should not return any duplicate values in the sorted array. | |
//input: [1,5,2,1] => output: [1,2,5] | |
//input: [4,2,2,3,2,2,2] => output: [2,3,4] | |
const uniqSort = obj => arr => | |
Object | |
.keys(arr.reduce((acc, curr) => { |
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
[1,2,3] | |
.map(x => (console.log(x), x)) // Tiniest "tap" fn...trick from DrBoolean | |
.map(x => x + 1) |
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 Maybe = { | |
Just: value => ({ | |
value, | |
map: f => Just(f(value)), | |
toString: () => `Just(${value})` | |
}), | |
Nothing: (value = null) => ({ | |
value: null, | |
map: f => Nothing(null), | |
toString: () => `Nothing(${value})` |
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 is from './is-util'; | |
/** | |
* Either Monad class (from Functional Programming in JavaScript) | |
*/ | |
class Either { | |
constructor(value) { | |
this._value = value; | |
} | |
get value () { |
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 { Observable } from 'rxjs'; | |
// Adapted from https://github.com/Reactive-Extensions/rx-node/blob/87589c07be626c32c842bdafa782fca5924e749c/index.js#L52 | |
export default function fromStream(stream, finishEventName = 'end', dataEventName = 'data') { | |
stream.pause(); | |
return new Observable((observer) => { | |
function dataHandler(data) { | |
observer.next(data); | |
} |