Skip to content

Instantly share code, notes, and snippets.

View mytholog's full-sized avatar

Igor Gavrilov mytholog

  • 2Gis
  • Russia
View GitHub Profile
@mytholog
mytholog / letitfail.go
Created April 5, 2019 11:37 — forked from campoy/letitfail.go
This example shows how to have a set of goroutines running concurrently and processing requests. Panics from goroutines are recovered and the worker is restarted. You can download it and run it directly using `go run letitfail.go`
package main
import (
"bufio"
"fmt"
"os"
"time"
)
const numWorkers = 3
@mytholog
mytholog / worker_pool.go
Created August 13, 2018 07:33
pool of go routines
package main
import (
"fmt"
"sync"
"time"
)
/*
In many languages, you need a "pool" mechanism to handle threads, because
@mytholog
mytholog / postgresql-set-id-seq.sql
Created April 26, 2018 15:06 — forked from henriquemenezes/postgresql-set-id-seq.sql
PostgreSQL set Next ID Sequence Value to MAX(id) from Table
-- Get Max ID from table
SELECT MAX(id) FROM table;
-- Get Next ID from table
SELECT nextval('table_id_seq');
-- Set Next ID Value to MAX ID
SELECT setval('table_id_seq', (SELECT MAX(id) FROM table)+1);
@mytholog
mytholog / stack_queue.go
Last active September 13, 2017 12:51 — forked from moraes/gist:2141121
LIFO Stack and FIFO Queue in golang
package main
import (
"fmt"
)
type Node struct {
Value int
}
@mytholog
mytholog / revers.go
Last active September 13, 2017 13:00
func reverse(s []int) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func reverseString(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
func binarySearch(a []int, search int) (result int, searchCount int) {
mid := len(a) / 2
switch {
case len(a) == 0:
result = -1 // not found
case a[mid] > search:
result, searchCount = binarySearch(a[:mid], search)
case a[mid] < search:
result, searchCount = binarySearch(a[mid+1:], search)
result += mid + 1
tmp := 0
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr) - 1; j++ {
if arr[j] > arr[j + 1] {
tmp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = tmp
}
@mytholog
mytholog / StreamToString.go
Created July 5, 2017 07:36 — forked from tejainece/StreamToString.go
Golang: io.Reader stream to string or byte slice
import "bytes"
func StreamToByte(stream io.Reader) []byte {
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.Bytes()
}
func StreamToString(stream io.Reader) string {
buf := new(bytes.Buffer)
@mytholog
mytholog / closest.js
Created March 6, 2017 01:52
closest number out of array
const counts = [4, 9, 15, 6, 2];
const goal = 5;
counts
.reduce((prev, curr) => Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
@mytholog
mytholog / deepClone.js
Created February 22, 2017 05:06
deep clone JS object
function deepClone(obj) {
var copy;
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;
// Handle Date
if (obj instanceof Date) {
copy = new Date();
copy.setTime(obj.getTime());