This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func batcher[T any](in chan T, out chan []T, batchSize int, timeout time.Duration) { | |
| for { | |
| batch := make([]T, 0, batchSize) | |
| t := time.After(timeout) | |
| for batchReady := false; !batchReady; { | |
| select { | |
| case e := <-in: | |
| batch = append(batch, e) | |
| if len(batch) == batchSize { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bufio" | |
| "fmt" | |
| "os" | |
| "time" | |
| ) | |
| const numWorkers = 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "sync" | |
| "time" | |
| ) | |
| /* | |
| In many languages, you need a "pool" mechanism to handle threads, because |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| type Node struct { | |
| Value int | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const counts = [4, 9, 15, 6, 2]; | |
| const goal = 5; | |
| counts | |
| .reduce((prev, curr) => Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev); |
NewerOlder