Skip to content

Instantly share code, notes, and snippets.

View plvhx's full-sized avatar
🏠
Working from home

Paulus Gandung Prakosa plvhx

🏠
Working from home
View GitHub Profile
@plvhx
plvhx / impl.go
Created February 17, 2022 01:49
http.CookieJar interface implementation
package main
import (
"fmt"
"reflect"
"net/http"
"net/url"
)
type CookieJar struct {
@plvhx
plvhx / range.go
Last active February 14, 2022 03:37
range http header
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
package main
import (
"log"
"strconv"
"strings"
"time"
"regexp"
"encoding/json"
@plvhx
plvhx / go_concurrency_101.md
Last active January 1, 2022 06:38
go concurrency 101
  • parameter supplied to the method 'Add' in the sync.WaitGroup struct must be equal to the current running goroutine. no more, no less.
    • if less than number of current running goroutine, it will leads to unexpected behavior.
    • if more than number of current running goroutine, it will leads to deadlock.
// dirty and simple :))
package main

import (
@plvhx
plvhx / dict_filter.erl
Created August 1, 2021 09:25
filtering dict like a boss.
-module(main).
-export([start/0]).
-import(io, [fwrite/2]).
%% internal callback when calling
%% dict:filter/2
filter_even(_, Value) ->
case lists:nth(1, Value) rem 2 of
0 -> true;
1 -> false
@plvhx
plvhx / convert_date_like_hell.erl
Created July 27, 2021 06:07
converting rfc3339 to timestamp and back, like a boss.
-module(main).
-export([start/0]).
to_rfc3339(DateTime) ->
{{Y, M, D}, {H, I, S}} = DateTime,
io_lib:format(
"~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0BZ",
[Y, M, D, H, I, S]
).
@plvhx
plvhx / foobar.erl
Created July 24, 2021 13:18
erlang is shit
-module(main).
-author('u know who i am better than me ~').
-export([start/0]).
%% internal callback when calling
%% array:foldl
adder(_, V, A) -> V + A.
log(Arr) ->
log_impl(Arr, array:size(Arr), 0).
@plvhx
plvhx / day4_part2.js
Created January 23, 2021 13:41
advent of code day 4 part 2
const fs = require('fs')
const splitted = fs.readFileSync('./input.txt', 'utf-8').split(String.fromCharCode(0x0a).repeat(2))
function normalize(splt) {
let tmp = Object.create({})
for (let i = 0; i < splt.length; i++) {
splt[i].split(/\s+/).forEach((el) => {
let _splt = el.split(':');
tmp[_splt[0]] = _splt[1]
@plvhx
plvhx / day4_part1.js
Created January 22, 2021 14:01
advent of code day 4 part 1
const fs = require('fs')
const splitted = fs.readFileSync('./input.txt', 'utf-8').split(String.fromCharCode(0x0a).repeat(2))
function normalize(splt) {
for (let i = 0; i < splt.length; i++) {
splt[i] = splt[i].split(/\s+/).map((el) => { return el.split(':')[0] })
}
return splt
}
@plvhx
plvhx / day3_part2.js
Created January 22, 2021 07:42
advent of code 2020 day 3 part 2
const fs = require('fs')
const splitted = fs.readFileSync('./input.txt', 'utf-8').split(String.fromCharCode(0x0a))
function normalizePattern(splt) {
let nSplt = splt
let normLen = 90
for (let i = 0; i < (splt.length - 1); i++) {
nSplt[i] = splt[i].repeat(normLen)
}
@plvhx
plvhx / day3_part1.js
Created January 22, 2021 05:31
advent of code 2020 day 3 part 1
const fs = require('fs')
const splitted = fs.readFileSync('./input.txt', 'utf-8').split(String.fromCharCode(0x0a))
function normalizePattern(splt) {
let nSplt = splt
let normLen = Math.round((splt.length - 1) / splt[0].length) * 5
for (let i = 0; i < (splt.length - 2); i++) {
nSplt[i] = splt[i].repeat(normLen)
}