Skip to content

Instantly share code, notes, and snippets.

View chuntaro's full-sized avatar

chuntaro

View GitHub Profile
@chuntaro
chuntaro / cobj.c
Created June 22, 2019 04:54
C 言語でオブジェクト指向(案)
#include <stdio.h>
#include <stdlib.h>
typedef struct _Base {
int foo;
} Base;
void base_print_foo(const Base* base) {
printf("foo -> %d\n", base->foo);
}
@chuntaro
chuntaro / calc-pi.el
Last active November 20, 2019 05:49
Emacs 27 で円周率の計算
;; 出力結果は 3 の後ろに小数点を付けて最後の 4 桁は未収束の為除いたものが正しい値
;; 2019/11/20: 27 で追加された benchmark-progn を使うように変更
(defun bignum-arccot (x)
(let ((base (expt 10 (+ 10000 4)))
(a x)
(b 1)
pos
(sum 0))
(while (< 1 (setq pos (truncate base (* a b))))
@chuntaro
chuntaro / snprint_double.c
Created December 11, 2019 05:40
リーダブル(入力⇔出力で値が変わらない)な浮動小数文字列を出力するC言語の関数(snprintf_double)
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int snprint_double(char* buf, int size, double f)
{
if (isnan(f)) {
return snprintf(buf, size, "%s", signbit(f) ? "-nan" : "+nan");
} else if (isinf(f)) {
@chuntaro
chuntaro / test-tuple.cpp
Created January 22, 2020 03:33
C++ std::tuple の簡易実装
#include <iostream>
template<typename First, typename... Rest>
struct Tuple: public Tuple<Rest...> {
Tuple(First first, Rest... rest): Tuple<Rest...>(rest...), first(first) {}
First first;
};
template<typename First>
@chuntaro
chuntaro / result.txt
Created January 28, 2020 07:00
仮想継承時の仮想関数呼び出しのベンチマーク
chuntaro@NTEmacs64 windows-nt ~
$ g++ -O3 -Wall -Wextra test.cpp
chuntaro@NTEmacs64 windows-nt ~
$ ./a.exe
500000000
810ms
1000000000
1746ms
@chuntaro
chuntaro / hello.wat
Created March 4, 2020 10:20
WASI で動く最小の Hello, World!
(module
(export "memory" (memory 0))
(export "_start" (func $_start))
(import "wasi_unstable" "fd_write"
(func $__wasi_fd_write (param i32) (param i32) (param i32) (param i32) (result i32)))
(memory 2)
(data (i32.const 1024) "Hello, World!\n")
(func $_start
i32.const 0
i32.const 1024
@chuntaro
chuntaro / dolist.list
Created March 9, 2020 08:51
Xyzzy の dolist マクロ
(defmacro xyzzy-dolist ((var listform &optional (resultform ''nil)) &body body)
`(do* ((#1=#:tailvar ,listform (cdr #1#))
(,var (car #1#) (car #1#)))
((null #1#) ,resultform)
,@body))
@chuntaro
chuntaro / test-dolist.el
Created March 9, 2020 09:22
dolist マクロの実装内容によって結果が違う例
(let (cs)
(dolist (x '(1 2 3 4 5))
(push #'(lambda () x) cs))
(mapcar #'funcall cs))
;;=> (5 4 3 2 1)
;; 展開後 (こんな感じになる)
(let ((ls '(1 2 3 4 5))
cs)
(while ls
@chuntaro
chuntaro / duplicate-removal.js
Created March 10, 2020 03:52
JavaScript で重複削除のテストコード
let array = [...Array(50)].map(_ => Math.floor(Math.random() * 20));
let a = array.filter(function (x, i, self) {
return self.indexOf(x) === i;
});
let b = Array.from(new Set(array));
console.log(a, b);
let loopCount = 1000000;
@chuntaro
chuntaro / vc-git-grep-quick.el
Created April 15, 2020 08:47
`vc-git-grep'はインタラクティブに実行するとファイル名等色々聞いてくるので、Enterだけで実行する為のラッパーコマンド
;; `vc-git-grep'はインタラクティブに実行するとファイル名等色々聞いてくるので
;; Enterだけで実行する為のラッパーコマンド
(defun vc-git-grep-quick (regexp &optional files dir)
(interactive
(progn
(grep-compute-defaults)
(let ((regexp (grep-read-regexp))
(files (concat "*." (file-name-extension (buffer-file-name))))
(dir "."))