Skip to content

Instantly share code, notes, and snippets.

View mfirhas's full-sized avatar
🍧
Funemployed

Muhammad Fathir Irhas mfirhas

🍧
Funemployed
View GitHub Profile
@mfirhas
mfirhas / Dockerfile
Created October 9, 2023 09:41
rust_cargo_chef.Dockerfile
### STAGE 1: install cargo chef
FROM rust AS chef
RUN cargo install cargo-chef
WORKDIR /app
### STAGE 2: create cargo chef recipe.json
FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json
@mfirhas
mfirhas / generics.go
Created September 14, 2023 04:29
Just small experiment and poc with Go's generics
// go version go1.21.1 darwin/amd64
// $ go build -gcflags="-m -m"
package main
func main() {
// p escapes to heap
// because Func has generic parameter with type constraint containing method(s)
// gcshape of pointer argument to type parameter is not monomorphized, hence need to create vtable to contains methods.
@mfirhas
mfirhas / generics.go
Created September 13, 2023 09:24
Sample code describing how to create simple iterator
// suggested solution to question on https://www.reddit.com/r/golang/comments/16g625t/cannot_compile_generic_function_that_takes_either/
package main
import (
"fmt"
)
type Iterator[T any] interface {
// Length return length of data on each iteration. Each iteration will reduce the length by 1.
@mfirhas
mfirhas / main.rs
Last active September 9, 2023 12:12
Print your name with y combinator
// to run: rustc -C opt-level=3 main.rs && ./main
trait YF<T, U> {
fn apply(&self, yf: &dyn YF<T, U>, u: T) -> U;
}
impl<T, U, F> YF<T, U> for F
where
F: Fn(&dyn YF<T, U>, T) -> U,
{
@mfirhas
mfirhas / cloudSettings
Last active December 13, 2021 10:02
Visual Studio Code Settings Sync Gist
{"lastUpload":"2021-12-13T09:57:36.372Z","extensionVersion":"v3.4.3"}
@mfirhas
mfirhas / YCombinator.java
Last active September 20, 2021 04:44
Print your name 1000 times without looping in Java
// Y = πœ†π‘“.(πœ†π‘₯.𝑓(π‘₯π‘₯))(πœ†π‘₯.𝑓(π‘₯π‘₯))
public class YCombinator {
public interface F<T,U> {
Void apply(T name,U n);
}
public interface FF<T,U> {
@mfirhas
mfirhas / ycombinator.go
Last active September 20, 2021 04:39
Print your name 1000 times without looping in Go
package main
// Y = πœ†π‘“.(πœ†π‘₯.𝑓(π‘₯π‘₯))(πœ†π‘₯.𝑓(π‘₯π‘₯))
import "fmt"
type (
F func(string, int)
FF func(F) F
@mfirhas
mfirhas / timediff.go
Created October 17, 2019 08:28
get diff between 2 time in go
func diff(a, b time.Time) (year, month, day, hour, min, sec int) {
if a.Location() != b.Location() {
b = b.In(a.Location())
}
if a.After(b) {
a, b = b, a
}
y1, M1, d1 := a.Date()
y2, M2, d2 := b.Date()
@mfirhas
mfirhas / fakeresponse.go
Created October 11, 2019 08:21 — forked from karlseguin/fakeresponse.go
A fake http.ResponseWriter class, used for testing. See http://openmymind.net/Testing-In-Go/
package fakeresponse
import (
"testing"
"net/http"
)
type FakeResponse struct {
t *testing.T
headers http.Header
@mfirhas
mfirhas / golibs.txt
Created May 13, 2019 03:58
list of go packages
https://github.com/stretchr/testify (TDD)
https://onsi.github.io/ginkgo/ (BDD)
https://github.com/sirupsen/logrus (logging)
https://github.com/juju/errors (errors handling)