Skip to content

Instantly share code, notes, and snippets.

View Tomotoes's full-sized avatar
🍅
PRESS START

Simon Ma Tomotoes

🍅
PRESS START
View GitHub Profile
@Tomotoes
Tomotoes / index.html
Last active August 18, 2019 07:17
Loading renderings
<div class="loading"></div>
@Tomotoes
Tomotoes / Fibonacci sequence.js
Last active August 18, 2019 07:15
Es6 realizes Fibonacci sequence
function* fibonacci() {
let [prev, curr] = [0, 1];
while (true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
for (let n of fibonacci()) {
if (n > 1000) break;
@Tomotoes
Tomotoes / generateDOM.js
Last active August 18, 2019 07:15
Generate DOM using proxy.
const dom = new Proxy({},{
get(target,property){
return function (attrs={},...children){
const el = document.createElement(property)
for(let prop of Object.keys(attrs)){
el.setAttribute(prop,attrs[prop])
}
console.log(property) // a li li li ul div
for(let child of children){
@Tomotoes
Tomotoes / promisify.js
Last active August 18, 2019 07:47
Gengrate asynchronous function.
const promisify = callback => (...args) =>
new Promise((resolve, reject) =>
callback(...args, (err, data) => err ? reject(err) : resolve(data))
)
@Tomotoes
Tomotoes / SleepSort.java
Last active August 18, 2019 07:45
Legendary sleep sort
import java.util.stream.IntStream;
public class SleepSort implements Runnable {
private int number;
private SleepSort(int number) {
this.number = number;
}
public static void main(String[] args) {
@Tomotoes
Tomotoes / ActiveObject.go
Created August 18, 2019 08:48
Design Pattern - Active Object
package main
import "fmt"
type MethodRequest int
const (
Incr MethodRequest = iota
Decr
)
@Tomotoes
Tomotoes / merge.go
Created August 18, 2019 08:54
Merge Channels
package main
import "sync"
func merge(cs ...chan interface{}) <-chan interface{} {
var wg sync.WaitGroup
out := make(chan interface{})
output := func(c <-chan interface{}) {
for e := range c {
out <- e
@Tomotoes
Tomotoes / Command.go
Created August 18, 2019 08:57
Command tool
package main
import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"strings"
)
@Tomotoes
Tomotoes / Context.go
Last active August 18, 2019 09:18
Officially provided Context package
package context
import (
"errors"
"reflect"
"sync"
"time"
)
// Context 四个方法
@Tomotoes
Tomotoes / Mutex.go
Created August 18, 2019 09:12
Mutex with channel
package main
import "time"
type Mutex struct {
lock chan struct{}
}
func New() *Mutex {
mu := &Mutex{lock: make(chan struct{}, 1)}