Skip to content

Instantly share code, notes, and snippets.

from collections import defaultdict
class Graph:
def __init__(self):
self.nodes = defaultdict(set)
def add_node(self, node, neighbors):
self.nodes[node].update(neighbors)
@ninedraft
ninedraft / values.go
Created August 17, 2021 11:46
Generic go trash utilities
package value
func If[E any](ok bool, then, or E) E {
if ok {
return then
}
return or
}
@ninedraft
ninedraft / batch.go
Last active March 31, 2021 14:53
Go2 std
func Await[C context.Context, E any](ctx C, ch <-chan E) (E, bool) {
select {
case <-ctx.Done():
var empty E
return empty, false
case v, ok := <-ch:
return v, ok
}
}
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDiGuEm2gFKN6ILanclMT1iYNLjraF9FdiTgH+bPSOAO4kmJusKDjtgmzGfI8SoUs4AxxcNX1ZsJB9u+lYTKW63CRupCeiZP1MnyKMHbDz+iTcTph8UeFGvM4TbMkPEUhXNBlk8t8VHARK0c50sAChhjRIKNnqnWbncGy49wqDFVQqqScslHZMozJIKmvbFX9tf8QfkNpfGlo9WhTMGBQV2Sm2RABd77j/F+5mSGOSHRVYhYlb33aIT3vDm3QQxJ25valspnmjGvHh6cvolUId+pNY2yJ88LG7m9QJnPHKT6KLYjWqcL9S8XIwGXBvGk6hdNR4j/hU23dWqCJxi6p3yrFPF2ikKV9srDRKRY09cgsF/rgIYB0D7tKLnYLT0Eh+h0b6P0zZNvNSvBc6/eBGG2v2teHsOSVtwlgCGF6IJJp08IkRVzzUiHN6V3HxvTt5LkGRNVy0BR4tgyvJulslnxhg8ePVshSTwyXt0D9UhFxuFhVwnxyTt6VXZDh8vnzc= merlin@gekata
@ninedraft
ninedraft / settings.json
Created July 30, 2020 09:39
VS Code settings
{
"workbench.iconTheme": "vscode-great-icons",
"editor.minimap.enabled": false,
"go.formatTool": "goimports",
"go.useLanguageServer": true,
"[json]": {
"editor.defaultFormatter": "HookyQR.beautify"
},
"clock.alignment": "Right",
"clock.format": "🕑HH:MM 📅 d mmm yyyy",
@ninedraft
ninedraft / template_test.go
Last active July 21, 2020 10:50
Snippet for go template resources
import (
"testing"
"text/template"
)
func testTemplate(test *testing.T, pattern string) {
test.Run(pattern, func(test *testing.T) {
var _, err = template.ParseGlob(pattern)
if err != nil {
test.Fatal(err)
@ninedraft
ninedraft / swag.go
Created July 20, 2020 18:51
Swagger spec simplified golang model
package swag
type Swag struct {
Openapi string `yaml:"openapi"`
Info Info `yaml:"info"`
Servers []Servers `yaml:"servers"`
Paths map[string]Path `yaml:"paths"`
}
type Info struct {
@ninedraft
ninedraft / adapter.go
Created July 17, 2020 08:40
Golang STD HTTP to gin middleware adapter
// Copyright 2020 Petrukhin Pavel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@ninedraft
ninedraft / wr.go
Created July 15, 2020 08:12
Writer proxy error monad for Golang
package wr
import "io"
type Wr struct {
err error
written int64
w io.Writer
}
type t interface {
Errorf(string, ...interface{})
}
func testStringer(test t, str stringer, expected ...string) {
var value = str.String()
if value == "" {
test.Errorf("%T.String() must not return an empty string", str)
return