Skip to content

Instantly share code, notes, and snippets.

@gcnyin
gcnyin / tolower.cpp
Created July 23, 2020 14:52
c++ tolower command line tool
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
if (argc < 2) {
exit(1);
@gcnyin
gcnyin / fact.cpp
Created July 23, 2020 14:53
C++ Compile-time factorial calculation
#include <iostream>
template <uint64_t n>
struct Fact {
static const uint64_t value = n * Fact<n - 1>::value;
};
template <>
struct Fact<1> {
static const uint64_t value = 1;
@gcnyin
gcnyin / .vimrc
Last active June 7, 2021 04:13
my vim configuration
call plug#begin('~/.vim/plugged')
Plug 'morhetz/gruvbox'
Plug 'ntpeters/vim-better-whitespace'
call plug#end()
set autoindent
set expandtab
set shiftround
@gcnyin
gcnyin / replaceEncryptedFields.ts
Created September 19, 2020 10:58
replaceEncryptedFields
const replaceEncryptedFields = async (
// eslint-disable-next-line
obj: any,
kmsDecryptFn: (encryptedValue: string) => Promise<string>
): Promise<void> => {
for (const [key, value] of Object.entries(obj)) {
if (typeof value === 'string' && value.startsWith(KMS_PREFIX)) {
const encryptedValue = value.slice(KMS_PREFIX.length, value.length);
obj[key] = await kmsDecryptFn(encryptedValue);
} else if (typeof value === 'object') {
@gcnyin
gcnyin / init.lua
Created May 26, 2021 15:44
Hammerspoon窗口管理器
hs.window.animationDuration = 0
units = {
right50 = { x = 0.50, y = 0.00, w = 0.50, h = 1.00 },
left50 = { x = 0.00, y = 0.00, w = 0.50, h = 1.00 },
top50 = { x = 0.00, y = 0.00, w = 1.00, h = 0.50 },
bot50 = { x = 0.00, y = 0.50, w = 1.00, h = 0.50 },
maximum = { x = 0.00, y = 0.00, w = 1.00, h = 1.00 }
}
hs.hotkey.bind({ 'ctrl', 'alt', 'cmd'}, 'n', function()
@gcnyin
gcnyin / option.scala
Created December 9, 2021 05:31
my implementation of scala Option
package example
sealed trait OptionZ[+T] {
def map[U](f: T => U): OptionZ[U]
def flatMap[U](f: T => OptionZ[U]): OptionZ[U]
def filter(f: T => Boolean): OptionZ[T]
def foreach(f: T => Unit): Unit
@gcnyin
gcnyin / reverse_linkedlist.scala
Last active January 11, 2022 09:34
reverse linkedlist
import scala.annotation.tailrec
object Main extends App {
sealed trait Node
case class Branch(value: Int, next: Node) extends Node
case class Leaf(value: Int) extends Node
def reverse: Node => Node = {
case Branch(value, next) => _reverse(Leaf(value), next)
case l @ Leaf(_) => l
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TDownload {