Skip to content

Instantly share code, notes, and snippets.

@YBogomolov
YBogomolov / session.ts
Last active Nov 26, 2022
Session types for TypeScript
View session.ts
// Inspired by https://github.com/Munksgaard/session-types
interface Chan<Env, Protocol> {
env_and_protocol: [Env, Protocol];
}
class Eps implements HasDual {
readonly tag: 'Eps' = 'Eps';
readonly dual!: Eps;
}
View useReducerWithEmitEffect.js
const {useCallback, useEffect, useReducer, useRef} = require('react');
let effectCapture = null;
exports.useReducerWithEmitEffect = function(reducer, initialArg, init) {
let updateCounter = useRef(0);
let wrappedReducer = useCallback(function(oldWrappedState, action) {
effectCapture = [];
try {
let newState = reducer(oldWrappedState.state, action.action);
@ParthDesai
ParthDesai / generic_demo.go
Last active Sep 3, 2021
Generic map function in golang
View generic_demo.go
package main
import (
"fmt"
"reflect"
)
func main() {
r := genericMap([]int{1, 2, 3, 4}, func(x int) string {
return "Hello"
@sebmarkbage
sebmarkbage / react-terminology.md
Last active Jan 9, 2023
React (Virtual) DOM Terminology
View react-terminology.md
@vaskoz
vaskoz / builder.go
Last active Jan 13, 2023
Golang Builder pattern
View builder.go
package main
import "strconv"
import "fmt"
type Color string
type Make string
type Model string
const (
View crc32.rs
extern mod extra;
use std::vec;
use extra::test::BenchHarness;
pub fn crc32(bytes : &[u8]) -> u32 {
crc32_end(crc32_chunk(crc32_begin(), bytes))
}
pub fn crc32_begin() -> u32 {
0xffffffff
@juuliaans
juuliaans / backbone-route-builder.js
Last active Dec 19, 2015
URL builder for Backbone-like router.
View backbone-route-builder.js
resolveURL: function (route, params, appendRoot) {
var appendRoot = (appendRoot == false) ? false : true;
var route, params;
var href = "";
var regExpAllNormalParams = /(\(\?)?\/:\w+/g;
var regExpAllOptionalParams = /\((.*?)\)/g;
href = router.routes[route];
@BurntSushi
BurntSushi / 01-possible-generic-map.go
Last active May 14, 2019
Code from my blog post "Writing type parametric functions in Go."
View 01-possible-generic-map.go
// This is *not* valid Go!
func Map(f func(A) B, xs []A) []B {
ys := make([]B, len(xs))
for i, x := range xs {
ys[i] = f(x)
}
return ys
}
Map(func(x int) int { return x * x }, []int{1, 2, 3})
@dai-shi
dai-shi / benchmark-startswith.js
Created Feb 14, 2013
benchmark of String.startsWith equivalents in Node.js
View benchmark-startswith.js
var Benchmark = require('benchmark');
Benchmark.prototype.setup = function() {
a = ["test"];
for (var i = 0; i < 10000; i++) {
a.push("some other stuff");
}
s = a.join();
re1 = new RegExp("^test");
re2 = new RegExp("^not there");
};
@drewolson
drewolson / reflection.go
Last active Dec 6, 2022
Golang Reflection Example
View reflection.go
package main
import (
"fmt"
"reflect"
)
type Foo struct {
FirstName string `tag_name:"tag 1"`
LastName string `tag_name:"tag 2"`