Skip to content

Instantly share code, notes, and snippets.

@kkspeed
kkspeed / csp_formula.cc
Created January 18, 2020 11:56
CSP化学方程式
#include <iostream>
#include <map>
#include <memory>
using Formula = std::map<std::string, int>;
class ParseContext {
public:
ParseContext(const std::string& string, int position):
string_(string), position_(position) {}
@kkspeed
kkspeed / main.rs
Last active April 4, 2019 04:12
Protobuf 3 decode raw
use std::str::from_utf8;
fn main() {
println!(
"{}",
read_proto_raw(&[0x12, 0x07, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67]).unwrap()
);
println!(
"{}",
read_proto_raw(&[0x1a, 0x03, 0x08, 0x96, 0x01]).unwrap()
@kkspeed
kkspeed / simple_plot.cljs
Created March 25, 2019 17:45
Use D3 with Reagent
;; Assumes D3 is provided as foreign lib. Add following to :compiler section of build.
;; :foreign-libs [{:file "lib/d3.v5.js"
;; :provides ["d3"]
;; :requires ["d3"]}]
;; Then (require [d3]) in the code namespace.
(def performance-data [{:group 0
:name "Update()"
:start 200
:end 240}
{:group 0
@kkspeed
kkspeed / toy_frp.cpp
Last active February 11, 2019 06:58
A toy C++ FRP framework
// A toy C++ FRP framework.
// Author: Bruce Li (github.com/kkspeed)
// Licensed under BSD.
//
// Note:
// 1. Compiles with C++ 17
// 2. Signal values are copy-constructable. Non-copyconstructable values
// could be wrapped in shared_ptr.
// 3. Signal graph is statically connected and unsubscription is not
// an option.
@kkspeed
kkspeed / type_list_fun.cc
Created January 27, 2019 07:47
Func with C++ type list
// Demonstrates C++ type list.
// Author: Bruce Li (github.com/kkspeed)
// Licensed under BSD.
// Requires C++ 11 except Filter. Filter requires C++14.
#include <string>
#include <type_traits>
// A typelist is a list of types. The list is similar to
// lists in functional languages like Lisp.
@kkspeed
kkspeed / naive.rs
Created January 18, 2019 04:55
A naive Rust Functional Reactive Programming Framework
use std::cell::RefCell;
use std::rc::Rc;
struct Core<T> {
observers: Vec<Box<Fn()>>,
value: Option<T>,
}
impl<T> Core<T>
where
@kkspeed
kkspeed / my_bind.hpp
Last active January 12, 2019 22:19
C++ Possible Alternate Bind Implementation
// Alternative implementation of bind without using placeholders.
// usage:
// Given void func(int a, int b, int c);
// auto f = my_bind(func, 3, 2);
// Then f(5) is the same as func(3, 2, 5);
// Also if first parameter is a weak pointer, this bind does the
// weak check automatically.
// For weak check to work successfully, the function needs to have void
// return type.
@kkspeed
kkspeed / render.py
Created January 17, 2016 04:47
Adding TOC automatic display for Google IO-2012 slide template
#!/usr/bin/env python
# Original template: Google io 2012:
# https://code.google.com/p/io-2012-slides/
# Now you can add section to your slide's metadata, which makes it
# easier to adapt to academic presentation
# e.g:
# ---
# section: foo bar
# title: .....
#
@kkspeed
kkspeed / blackjack.hs
Created January 11, 2016 03:27
Blackjack simple simulation
{-# Language GeneralizedNewtypeDeriving #-}
import Data.Maybe (fromJust)
import System.Random (randomIO)
import Control.Applicative
import Control.Monad.State.Strict
newtype GameAction s a = GameAction { getAction :: StateT s Maybe a }
deriving (Monad, Applicative, Functor, MonadState s, Alternative)
condition :: (s -> Bool) -> GameAction s ()
@kkspeed
kkspeed / menumanager.lua
Created November 24, 2015 04:54
More Weapon Stats menumanager.lua
-- More Weapon Stats menumanager.lua
-- by TdlQ and KarateF22
-- at http://paydaymods.com/mods/138/MWS
_G.MoreWeaponStats = _G.MoreWeaponStats or {}
MoreWeaponStats._path = ModPath
MoreWeaponStats._data_path = SavePath .. "more_weapon_stats.txt"
MoreWeaponStats.settings = {
show_dlc_info = true,
separate_extended_stats = false,
show_spread_and_recoil = true,