Skip to content

Instantly share code, notes, and snippets.

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

Luo Chen luochen1990

🏠
Working from home
View GitHub Profile
@luochen1990
luochen1990 / test-typeclass-callstack.hs
Last active June 17, 2019 14:04
demo: specify `HasCallStack` separately for different instance of same typeclass
{-# language InstanceSigs #-}
import GHC.Stack
class Next v where
next :: HasCallStack => v -> v
instance Next Bool where
next :: Bool -> Bool
next x = if x < maxBound then not x else undefined
@luochen1990
luochen1990 / call-stack-without-hascallstack.hs
Created June 20, 2019 02:48
Get Call Stack from `-xc` without `HasCallStack`
import GHC.Stack
foo :: [Int] -> Int
foo xs = last xs + 1
bar :: [Int] -> Int
bar xs = foo xs
xs :: [Int]
xs = []
@luochen1990
luochen1990 / linked-list.cpp
Last active June 25, 2019 14:23
linked list in cpp
//c++11
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
template<typename A>
struct List {
A label;
@luochen1990
luochen1990 / linked-list.rs
Created June 25, 2019 13:31
linked list in rust
use std::fmt;
enum List<A> {
Nil,
Cons(A, Box<List<A>>),
}
impl<A: fmt::Display> fmt::Display for List<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
@luochen1990
luochen1990 / perf-test-withFrozenCallStack.hs
Created August 15, 2019 03:34
compare the performance of `errorWithoutStackTrace` vs `withFrozenCallStack . error`
import GHC.Stack
import Data.Time.Clock
h0 :: Int -> Int
h0 x = if x == 3 then errorWithoutStackTrace "abuse h" else (x + 3) `div` (x - 3)
h1 :: Int -> Int
h1 x = if x == 3 then withFrozenCallStack . error $ "abuse h" else (x + 3) `div` (x - 3)
timeIt :: IO a -> IO a
@luochen1990
luochen1990 / coq-spacemacs-install.md
Last active November 13, 2019 04:39
Coq Emacs (Spacemacs) εΌ€ε‘ηŽ―ε’ƒε‡†ε€‡ζŒ‡ε—
@luochen1990
luochen1990 / bisect.js
Last active October 14, 2020 09:21
Binary Search in JavaScript
/*
Assuming:
1. l <= r
2. forall u, v, u < v -> f(u) <= f(v)
So, curve of f should looks like
______
f: ___|
So we know:
@luochen1990
luochen1990 / profiles.json
Last active June 4, 2024 10:45
My Windows Terminal Profile (Configuration/Preference)
{
"$help": "https://aka.ms/terminal-documentation",
"$schema": "https://aka.ms/terminal-profiles-schema",
"actions":
[
{
"command": "unbound",
"keys": "ctrl+shift+d"
},
{
@luochen1990
luochen1990 / miio-example.js
Created December 5, 2019 08:14
XiaoMi Mi Home, miio usage demo
const miio = require('miio')
const color = require("cli-color")
miio.device({
address: '192.168.0.120',
token: '0b806f990d9ca4ff7be7f07f0168dcae'
})
.then(device => {
console.info(color.green('> Connected to:'), device)
//console.log('Device Props: ', Object.getOwnPropertyNames(device))
@luochen1990
luochen1990 / math24.hs
Created April 6, 2020 13:08
solution of the math24 game in Haskell, also see https://www.zhihu.com/question/307729091
import Data.List
import Data.Maybe
import Data.Ratio
import Data.Char
import Control.Monad (liftM2, join, forM_)
import Control.Parallel.Strategies (parMap, rseq)
import Control.Arrow ((&&&))
import GHC.Exts (groupWith, sortWith)
type Number = Rational
type VarID = Int