Skip to content

Instantly share code, notes, and snippets.

@demotomohiro
demotomohiro / test_inotify.nim
Created March 8, 2023 05:55
Example Nim code to use inotify on Linux
# https://nim-lang.org/docs/inotify.html
import std/[macros, os]
import posix, posix/inotify
macro testMasks(v: uint32; m: untyped; mstr: untyped; masks: varargs[untyped]): untyped =
masks.expectMinLen 2
result = newStmtList()
let userCode = masks[^1]
for i in 0 ..< (masks.len - 1):
@demotomohiro
demotomohiro / afterHakkou3.JPG
Last active October 2, 2022 07:03
きな粉で作るヨーグルト
afterHakkou3.JPG
@demotomohiro
demotomohiro / bigintsqrt.nim
Created August 11, 2022 14:00
Fast approximated BigInt sqrt
import std/[math, options]
import bigints
func sqrt(a: BigInt): Option[BigInt] =
if a < 0'bi:
none(BigInt)
else:
# Approximate BigInt as z = x * 2^(2y)
# (0 ≦ x < 2^62, y ∈ N)
# Then sqrt(z) = sqrt(x) * 2^y.
@demotomohiro
demotomohiro / amn.nim
Last active September 28, 2022 22:38
Calculate an automorphic number
# This program calculate an automorphic number.
# 何乗しても下N桁が同じになる数(自己同形数 Automorphic number)を計算して出力します。
#
# How to compile and run
# 1. Install Nim (https://nim-lang.org/install.html)
# 2. $ nimble refresh
# 3. $ nimble install cligen
# 4. $ nimble install https://github.com/demotomohiro/bigints@#add-per-bit-op
# 5. $ nim c -d:danger --passC:-flto --passL:"-flto -s" amn.nim
# 6. $ ./amn run --numDigits=10000 > result.txt
@demotomohiro
demotomohiro / testimg.nim
Last active June 30, 2022 09:40
Realtime pixel drawing
import std/[strformat, strutils, bitops]
import pixie, chroma
# define `nort` when opengl or windows system is not available.
when not defined(nort):
import nimgl/[glfw, opengl]
proc `{}`(image: var Image, x, y: int): var ColorRGBX =
## Accessing any pixels outside the bounds of the image is error
when not defined(danger):
@demotomohiro
demotomohiro / randomseed.nim
Created August 30, 2021 09:25
## Visualize how Pseudo-random number generators created with jump function and with random seed use PRNG state space.
## Visualize how Pseudo-random number generators created
## with jump function and with random seed use PRNG state space.
## Install pixie with `nimble install pixie`
import std/[strformat, random]
import pixie
const
numRows = 32
numColumns = 64
@demotomohiro
demotomohiro / build_libnice.nims
Created October 10, 2020 01:24
Build & Install libnice 0.1.17 on gentoo linux
import os
proc execQuote(args: varargs[string]) =
args.quoteShellCommand.exec
proc download(url, dist: string) =
mkDir dist.parentDir
execQuote "wget", url, "-O", dist
proc untar(src, dist: string) =
@demotomohiro
demotomohiro / libnice_simple_example.nim
Last active October 31, 2020 13:38
Nim version of libnice example code
# Nim version of example code of libnice:
# https://libnice.freedesktop.org
#
# Refer libnice/examples/simple-example.c
# Push enter key at same time as possible when you enter remote data.
#
# Public STUN server list:
# https://gist.github.com/mondain/b0ec1cf5f60ae726202e
import gintro/[nice, gio, glib, gobject]
@demotomohiro
demotomohiro / gintro_test.ipynb
Created October 9, 2020 22:12
Test gintro in google colab
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@demotomohiro
demotomohiro / printoffsets.c
Created October 8, 2020 05:23
Print offsets of object field in Nim
#include <stdio.h>
#include <stddef.h>
#define PRINTOFFSET(typ, field) printf("%s(%ld): %ld\n", #field, sizeof(((typ*)0)->field), offsetof(typ, field))
typedef struct {
char a;
int b;
short c;
double d;