Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
typing...

Tao He sighingnow

💭
typing...
View GitHub Profile
@sighingnow
sighingnow / stack-config.yaml
Created Dec 26, 2016
Personal stack global configuration.
View stack-config.yaml
# This file contains default non-project-specific settings for 'stack', used
# in all projects. For more information about stack's configuration, see
# http://docs.haskellstack.org/en/stable/yaml_configuration.html
#
# {}
package-indices:
- require-hashes: false
gpg-verify: false
name: TUNA
@sighingnow
sighingnow / win-cpu-number.py
Created Nov 29, 2016
Get CPU number on Windows platform.
View win-cpu-number.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def get_cpu_number():
''' Return the processors' number, an integer value.
'''
import os
return int(os.environ['NUMBER_OF_PROCESSORS'])
@sighingnow
sighingnow / merge-archive
Created Nov 22, 2016
Merge two archive files (.a) into a single one.
View merge-archive
## place object files (.o) in two different folders to vaoid overlapping.
mkdir libA
cd libA
ar -x ../libA.a
cd ..
mkdir libB
cd libB
ar -x ../libB.a
@sighingnow
sighingnow / hemispheres.mma
Created Oct 19, 2016
Night Hemispheres in Mathematica.
View hemispheres.mma
(* set geography data server *)
server := "http://www.staremapy.cz/naturalearth/`1`/`2`/`3`.png"
(* plot *)
GeoGraphics[{GeoStyling[Opacity[0.5], GeoServer -> server],
NightHemisphere[]},
GeoBackground -> GeoStyling["ReliefMap", GeoServer -> server]]
(* more simple graph *)
GeoGraphics[{NightHemisphere[Now], Red, Point[$GeoLocation]}]
@sighingnow
sighingnow / auto-return-type.cxx
Created Oct 4, 2016
When two type parameters are different, how to choose a proper return type in C++ ?
View auto-return-type.cxx
#include <iostream>
#include <typeinfo>
template<typename T, typename S>
auto max(T a, S b) -> decltype(a+b) { // note that T and S may be different.
// for numeric type, automatic type convension will be performed when compare two values.
if (a > b) {
return a;
}
else {
@sighingnow
sighingnow / constexpr.cpp
Created Oct 1, 2016
Notes on constexpr in C++.
View constexpr.cpp
#include <type_traits>
#include <iostream>
#include <string>
// valid in C++14, but not valid in C++11.
// for statement not allowed in constexpr function in C++11.
constexpr size_t addition(size_t n) {
size_t s = 0;
for (size_t i = 0; i <= n; ++i) {
s += i;
@sighingnow
sighingnow / unsafe.hs
Last active Jul 6, 2018
Unsafe perform computation in some monads.
View unsafe.hs
import Control.Monad.ST
import System.IO.Unsafe
import Unsafe.Coerce
unsafePerformST :: ST s a -> a
unsafePerformST m = unsafePerformIO $
stToIO $ -- unsafely do it
unsafeCoerce m -- convert to ST Realworld a
{-# NOINLINE unsafePerformST #-}
@sighingnow
sighingnow / prime_sum.py
Created Oct 1, 2016
Summary of all prime numbers less than or equal to n.
View prime_sum.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from time import time
import math
def prime_sum_impl(n):
r = int(math.sqrt(n))
v = [n//i for i in range(1, r+1)]
v += list(range(v[-1]-1, 0, -1))
@sighingnow
sighingnow / rdstc-demo.c
Created Oct 1, 2016
Demo of Read Time-Stamp Counter (rdstc instruction).
View rdstc-demo.c
/**
* author: Tao He, sighingnow@gmail.com
*/
uint64_t rdtsc() {
uint32_t lo, hi;
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
return (uint64_t)hi << 32 | lo;
}
@sighingnow
sighingnow / signal-slot.hs
Last active Sep 30, 2016
Signal-slot mechanism in Haskell.
View signal-slot.hs
-------------------------------------------------------------
-- |
-- Copyright: (c) Tao He 2016
-- License: MIT
-- Maintainer: sighingnow@gmail.com
--
-- Signal-slot mechanism in Haskell.
--
import Control.Monad