Skip to content

Instantly share code, notes, and snippets.

View Youmoo's full-sized avatar

youmoo Youmoo

  • 浙江杭州
View GitHub Profile
@Youmoo
Youmoo / README.md
Last active November 28, 2016 09:32
favorite words && 喜欢的单词

收集一些自己喜欢的单词

// 格式化为es批量插入格式
[...$$('[itemprop=text] p')]
  .map(v=>v.textContent)
  .slice(1)
  .map(v=>`{"create":{"_index":"ym","_type":"words","_id":"${v}"}}
{ "word":"${v}", "sentence":"", "def":"", "created":"2016-11-28"}`).join('\n')
@bellbind
bellbind / tcp-echo.js
Last active May 31, 2018 08:26
[nodejs] TCP example
"use strict";
const net = require("net");
const port = process.env.PORT || 44444;
const once = !!process.env.ONCE || false;
function echo(socket) {
const server = this;
const {remoteAddress, remoteFamily, remotePort} = socket;
@bellbind
bellbind / udp-echo.js
Last active May 31, 2018 08:26
[nodejs]UDP example
"use strict";
const dgram = require("dgram");
const port = process.env.PORT || 44444;
const once = !!process.env.ONCE || false;
const opts = {type: "udp4", reuseAddr: true};
const socket = dgram.createSocket(opts, (msg, info) => {
// msg: Buffer, info: {address, family, port, size}

Pattern Matching

This is a strawman proposal for adding pattern matching to ECMAScript. Pattern matching is useful for matching a value to some structure in a similar way to destructuring. The primary difference between destructuring and pattern matching are the use cases involved - destructuring is useful for binding pieces out of larger structures whereas pattern matching is useful for mapping a value's structure to data or a set of behaviors. In practice this means that destructuring tends to allow many shapes of data and will do its best to bind something out of it, whereas pattern matching will tend to be more conservative.

Additionally, the power of pattern matching is increased substantially when values are allowed to participate in the pattern matching semantics as a matcher as well as a matchee. This proposal includes the notion of a pattern matching protocol - a symbol method that can be implemented by objects that enables developers to use those values in pattern matching. A common scenario w

import java.util.Arrays;
import java.util.List;
public class FunWithStaticInitialization {
public static void main(String[] args) {
new SimpleThing();
}
interface Thing {
@getify
getify / 1.md
Last active October 15, 2020 01:44
BetterPromise: a strawman experiment in subclassing Promise and "fixing" a bunch of its awkward/bad parts

Some things that are "better" with this BetterPromise implementation:

  • BetterPromise # then(..) accepts a BetterPromise (or Promise) instance passed directly, instead of requiring a function to return it, so that the promise is linked into the chain.

    var p = BetterPromise.resolve(42);
    
    var q = Promise.resolve(10);
    
    p.then(console.log).then(q).then(console.log);
@pathikrit
pathikrit / README.md
Last active April 24, 2021 17:36
My highly opinionated list of things needed to build an app in Scala
@bdchauvette
bdchauvette / .babelrc
Last active June 21, 2021 17:44
Minimal babel boilerplate
{
"presets": [ "es2015" ]
}
@getify
getify / 1.js
Last active September 29, 2021 11:58
experiment: mimicking React's new "useState()" hook for stand-alone functions, including "custom hooks"
"use strict";
[foo,bar] = TNG(foo,bar);
// NOTE: intentionally not TNG(..) wrapping useBaz(), so that it's
// basically like a "custom hook" that can be called only from other
// TNG-wrapped functions
function foo(origX,origY) {
var [x,setX] = useState(origX);
var [y,setY] = useState(origY);
@Integralist
Integralist / regex.js
Created March 11, 2013 15:15
The difference between JavaScript's `exec` and `match` methods is subtle but important, and I always forget...
var str = "The quick brown fox jumped over the box like an ox with a sox in its mouth";
str.match(/\w(ox)/g); // ["fox", "box", "sox"]
// match (when used with a 'g' flag) returns an Array with all matches found
// if you don't use the 'g' flag then it acts the same as the 'exec' method.
str.match(/\w(ox)/); // ["fox", "ox"]
/\w(ox)/.exec(str); // ["fox", "ox"]