Skip to content

Instantly share code, notes, and snippets.

View sighingnow's full-sized avatar
💭
typing...

Tao He sighingnow

💭
typing...
View GitHub Profile
@sighingnow
sighingnow / unsafe.hs
Last active July 6, 2018 09:41
Unsafe perform computation in some monads.
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 / constexpr.cpp
Created October 1, 2016 05:11
Notes on constexpr in C++.
#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 / auto-return-type.cxx
Created October 4, 2016 08:37
When two type parameters are different, how to choose a proper return type in C++ ?
#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 / hemispheres.mma
Created October 19, 2016 11:32
Night Hemispheres in Mathematica.
(* 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 / merge-archive
Created November 22, 2016 12:47
Merge two archive files (.a) into a single one.
## 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 / win-cpu-number.py
Created November 29, 2016 05:10
Get CPU number on Windows platform.
#!/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 / stack-config.yaml
Created December 26, 2016 15:02
Personal stack global configuration.
# 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 / T.hs
Created December 27, 2016 14:13
Haskell's $ operator for types.
{-# LANGUAGE TypeOperators #-}
module T where
-- | Infix application.
--
-- @
-- f :: IO $ Maybe Int
-- =
@sighingnow
sighingnow / Makefile
Last active December 27, 2016 15:38
Expose C++ API to Haskell via FFI.
all: libacpp.dll app.exe
GHC := stack ghc --
%.o: %.cpp
$(CXX) -c $< -o $@
libacpp.dll: acpp.o acpp_capi.o
$(CXX) $^ -shared -o $@ # On Linux/Unix, -fPIC is needed.
@sighingnow
sighingnow / misc.hs
Created January 2, 2017 13:23
concatMap and filter using Foldable and Traversable.
concatMap :: (Foldable f, Monoid (f b)) => (a -> f b) -> f a -> f b
concatMap = foldMap
filter :: (Applicative f, Foldable f, Monoid (f a)) =>
(a -> Bool) -> f a -> f a
filter p = foldMap (\a -> if p a then pure a else mempty)