Skip to content

Instantly share code, notes, and snippets.

View maxclav's full-sized avatar
Turning Coffee into Code

Maxime Clavel maxclav

Turning Coffee into Code
View GitHub Profile
@maxclav
maxclav / kadane-algorithm.go
Last active August 5, 2023 04:50
Kadane's Algorithm in Go (GoLang).
package array
func maxSubArraySum(a []int, size int) int {
maxSoFar, currMax := a[0], a[0]
for i := 1; i < size; i++ {
currMax = max(a[i], currMax + a[i])
maxSoFar = max(maxSoFar, currMax)
}
@maxclav
maxclav / stack-generics.go
Created July 24, 2023 17:10
Stack Data Structure with generics in Go (GoLang).
package stack
// Stack represents a basic stack data structure.
type Stack[T any] struct {
items []T
}
// Push adds an item to the top of the stack.
func (s *Stack[T]) Push(item T) {
s.items = append(s.items, item)
@maxclav
maxclav / stack.go
Created July 24, 2023 16:52
Stack Data Structure in Go (GoLang)
package stack
// Stack represents a basic stack data structure.
type Stack struct {
items []any
}
// Push adds an item to the top of the stack.
func (s *Stack) Push(item any) {
s.items = append(s.items, item)
@maxclav
maxclav / queue-generics.go
Last active July 24, 2023 16:38
Queue Data Structure with generics in Go (GoLang)
package queue
// Queue represents a generic queue data structure.
type Queue[T any] struct {
items []T
}
// Enqueue adds an item to the end of the queue.
func (q *Queue[T]) Enqueue(item T) {
q.items = append(q.items, item)
@maxclav
maxclav / queue.go
Last active July 24, 2023 16:42
Queue Data Structure in Go (GoLang)
package queue
// Queue represents a basic queue data structure.
type Queue struct {
items []any
}
// Enqueue adds an item to the end of the queue.
func (q *Queue) Enqueue(item any) {
q.items = append(q.items, item)
@maxclav
maxclav / orderedmap.go
Last active July 24, 2023 17:04
[WIP] Ordered Dictionary (LinkedHashMap) in Go (GoLang) without container/list pkg.
// orderedmap implements an ordered map (ordered dictionary or linked hash map).
// This can be useful in scenarios where we need to maintain the order of access and/or insertion (ex: LRU Cache).
// For learning purposes. Use container/list.
package orderedmap
func NewOrderedMap() *OrderedMap {
return &OrderedMap{
dict: make(map[string]*Node),
}
}
@maxclav
maxclav / builder.go
Last active December 19, 2022 03:48
Builder Design Pattern example in Go (GoLang).
package httprequest
import (
"context"
"fmt"
"io"
"net/http"
)
// You will need a builder pattern
@maxclav
maxclav / prototype.go
Last active December 19, 2022 03:45
Prototype Design Pattern example in Go (GoLang).
package main
import "fmt"
/*
In go, primitive types, and structs containing only primitive types, are copied by value.
You can copy them by simply assigning to a new variable (or returning from a function).
If your struct happens to include arrays, slices, or pointers, then you'll need to perform a deep copy of the referenced objects
unless you want to retain references between copies.
Source: https://stackoverflow.com/questions/51635766/how-do-i-copy-a-struct
@maxclav
maxclav / factory_method.go
Last active December 20, 2022 16:52
Factory Method Design Pattern example in Go (GoLang).
package storage
import (
"errors"
"io"
)
// Product interface
type Storage interface {
@maxclav
maxclav / singleton.go
Last active December 19, 2022 03:45
Singleton Design Pattern in Go (GoLang).
package designpattern
import (
"sync"
)
type singleton struct {}
var instance *singleton
var once sync.Once