Skip to content

Instantly share code, notes, and snippets.

View antfu's full-sized avatar

Anthony Fu antfu

View GitHub Profile
@Splagoon
Splagoon / time.kt
Created November 11, 2014 18:50
Time magics with Kotlin
import java.util.Date
import kotlin.concurrent.*
fun at(date: Date, func: () -> Unit) {
val waitTime = date.getTime() - Date().getTime()
thread {
Thread.sleep(waitTime)
func()
}
}
@antfu
antfu / swap.md
Created April 11, 2019 22:45
Create swap
@Zhenmao
Zhenmao / README.MD
Created January 20, 2018 10:06
Donut Chart with Labels and Missing Values D3.js V4
type ParseResult = {
success: boolean,
data: any,
source: string
}
type ParseFailed = {
suceess: false,
data: any,
source: string
//Task: Find the greatest common divisor for an array of integers
//Tags: array, gcd
let arr = [6, 9, 21]
let gcd = function(a, b) {
a = Math.abs(a)
b = Math.abs(b)
while (a != b) {
if (a > b) a -= b
@antfu
antfu / doc-table.md
Last active October 14, 2023 20:09
Doc Table in Markdown

Example

Name

Description


@JedWatson
JedWatson / 1-proposal.md
Last active January 2, 2024 17:59
Proposal: adding reverse-relationship population to Mongoose (as implemented in KeystoneJS)

I've developed a useful feature in KeystoneJS that lets you populate a relationship from either side, while only storing the data on one side, and am looking for feedback on whether it is something that could / should be brought back into mongoose itself. (It might be possible to add as a separate package but I suspect there'd be too much rewriting of mongoose internals for that to be a good idea).

I've added this as an issue in mongoose for consideration: #1888 but am leaving this gist in place because the examples are easier to read.

I've used Posts and Categories as a basic, contrived example to demonstrate what I'm talking about here; in reality you'd rarely load all the posts for a category but there are other real world cases where it's less unreasonable you'd want to do this, and Posts + Categories is an easy way to demo it.

The problem

The built-in population feature is really useful; not just for

@blixt
blixt / prng.js
Last active January 14, 2024 07:01
A very simple, seedable JavaScript PRNG. NOTE: Please read comments on why this is not a good choice.
// NOTICE 2020-04-18
// Please see the comments below about why this is not a great PRNG.
// Read summary by @bryc here:
// https://github.com/bryc/code/blob/master/jshash/PRNGs.md
// Have a look at js-arbit which uses Alea:
// https://github.com/blixt/js-arbit
/**
@facultymatt
facultymatt / roles_invesitgation.md
Last active April 16, 2024 09:31
Roles and permissions system for Nodejs
@reimund
reimund / dict2xml.py
Last active May 30, 2024 10:58
Simple dictionary to xml serializer.
"""
Simple xml serializer.
@author Reimund Trost 2013
Example:
mydict = {
'name': 'The Andersson\'s',
'size': 4,