Skip to content

Instantly share code, notes, and snippets.

View wayneseymour's full-sized avatar
💭
ABC...always be coding :)

Tre' wayneseymour

💭
ABC...always be coding :)
View GitHub Profile
@wayneseymour
wayneseymour / websocket-echo.js
Created October 7, 2020 15:18
SImple echo spike
// 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')
@wayneseymour
wayneseymour / filter-id.js
Created September 24, 2020 21:19
Filter over an identity fn, as a predicate.
id = x => x
results = [1,'',null].filter(id)
console.log(JSON.stringify(results, null, 2))
// => [ 1 ]
// 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!
@wayneseymour
wayneseymour / fp_arch.js
Created September 20, 2020 16:29 — forked from DrBoolean/fp_arch.js
OO => FP architecture refactor
// 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
@wayneseymour
wayneseymour / uniq-sort.js
Last active February 18, 2021 21:26
simplest unique array sort algo in short form
//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) => {
@wayneseymour
wayneseymour / log-trick.js
Created September 7, 2020 20:58
Nice little console.log trick I got from DrBoolean
[1,2,3]
.map(x => (console.log(x), x)) // Tiniest "tap" fn...trick from DrBoolean
.map(x => x + 1)
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})`
@wayneseymour
wayneseymour / fp-either-monad.js
Created June 11, 2020 16:00 — forked from mrosata/fp-either-monad.js
Functional JavaScript Monad Classes - (Maybe Just Nothing) - (Either Left Right) (IOMonad) and my type checking utils
import is from './is-util';
/**
* Either Monad class (from Functional Programming in JavaScript)
*/
class Either {
constructor(value) {
this._value = value;
}
get value () {
@wayneseymour
wayneseymour / rxjs-from-stream.js
Created May 15, 2020 17:22 — forked from QuentinRoy/rxjs-from-stream.js
Transform a node stream into an rxjs Observer
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);
}