Skip to content

Instantly share code, notes, and snippets.

View lotusirous's full-sized avatar
💭
for { Learn() }

Kha Nguyen lotusirous

💭
for { Learn() }
View GitHub Profile
@lotusirous
lotusirous / combine_http.swift
Created March 8, 2022 02:00
A good way to handle HTTP in swift combine
let url = URL(string: "https://postman-echo.com/time/valid?timestamp=2016-10-10")!
struct PostmanResponse: Decodable {
let valid: Bool
}
enum ApiError: Error {
case invalidServerResponse
}
import UIKit
let url = URL(string: "http://127.0.0.1:1234")!
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("you-value-goes-here", forHTTPHeaderField: "X-API-KEY")
let task = URLSession.shared.dataTask(with: request) { data, _, error in
if let data = data {
@lotusirous
lotusirous / ratelimit.go
Last active January 19, 2022 04:08
A simple rate limit for go http client
package ratelimit
import (
"context"
"net/http"
"time"
"golang.org/x/time/rate"
)
@lotusirous
lotusirous / go_luasnip.lua
Created January 16, 2022 08:30
Some go snippets for luasnip
local ls = require "luasnip"
local s = ls.snippet
local i = ls.insert_node
local t = ls.text_node
local f = ls.function_node
local function copyname(args)
return args[1]
end
package main
import (
"log"
"net/http"
"sync/atomic"
"time"
)
var isBusy int32
@lotusirous
lotusirous / c_version.c
Created November 20, 2021 08:26
The version of c you are using
#include <stdio.h>
#define version __STDC_VERSION__
int
main (int argc, char *argv[])
{
printf ("Your c version is: %ld\n", version);
return 0;
}
@lotusirous
lotusirous / byteArray_to_hex.js
Created September 1, 2021 06:44
A helper function to convert a byte array to hex (source: https://mkyong.com/java/java-how-to-convert-bytes-to-hex/)
function toHex(bArr) {
var result = "";
for (var i = 0; i < bArr.length; i++) {
let dec = bArr[i] & 0xff;
var hex = Java.use("java.lang.Integer").toHexString(dec);
if (hex.length % 2 == 1) {
hex = "0" + hex;
}
result += hex;
}
@lotusirous
lotusirous / XOR_2_images.py
Created July 31, 2021 10:08
Xor 2 images with python
from cv2 import cv2
foo = cv2.imread("./foo.png")
bar = cv2.imread("./bar.png")
key = cv2.bitwise_xor(foo, bar)
cv2.imshow("xored data", key)
@lotusirous
lotusirous / init.vim
Created June 14, 2021 05:26
Simple vim configuration for development
set wildignore+=*.pyc
set guicursor=
set relativenumber
set nohlsearch
set hidden
set noerrorbells
set tabstop=4 softtabstop=4
set shiftwidth=4
const array1 = ["A", "B", "C"];
const array2 = ["1", "2", "3"];
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
// acc is the Accumulator for our result, it starts with []
// for each element in array 1, we will use the add operator when `map` with each element in array 2.
const result = array1.reduce(
(acc, v) => [...acc, ...array2.map((x) => v + x)],