Skip to content

Instantly share code, notes, and snippets.

View reverse_linkedlist.scala
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
@gcnyin
gcnyin / option.scala
Created Dec 9, 2021
my implementation of scala Option
View option.scala
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 / init.lua
Created May 26, 2021
Hammerspoon窗口管理器
View init.lua
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()
View replaceEncryptedFields.ts
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 / .vimrc
Last active Jun 7, 2021
my vim configuration
View .vimrc
call plug#begin('~/.vim/plugged')
Plug 'morhetz/gruvbox'
Plug 'ntpeters/vim-better-whitespace'
call plug#end()
set autoindent
set expandtab
set shiftround
@gcnyin
gcnyin / fact.cpp
Created Jul 23, 2020
C++ Compile-time factorial calculation
View fact.cpp
#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 / tolower.cpp
Created Jul 23, 2020
c++ tolower command line tool
View tolower.cpp
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
if (argc < 2) {
exit(1);