Skip to content

Instantly share code, notes, and snippets.

View cipharius's full-sized avatar

Valts Liepiņš cipharius

  • Latvia
  • 01:02 (UTC +03:00)
View GitHub Profile
@cipharius
cipharius / mergeInPlace.zig
Last active May 16, 2023 14:55
Merges two sorted slices of one contiguous memory
const std = @import("std");
fn mergeInPlace(comptime T: type, s: []T, midpoint: usize) void {
std.debug.assert(midpoint <= s.len);
if (midpoint == 0 or midpoint == s.len) return;
var a: usize = 0;
var b: usize = midpoint;
var c: usize = midpoint;
@cipharius
cipharius / Arcan.nix
Last active May 1, 2021 11:43 — forked from egasimus/Arcan.nix
Building Arcan on NixOS, 2021 version
({ lib, newScope, stdenv, pkgs }: let
# nicer aliases
derive = stdenv.mkDerivation;
concat = builtins.concatStringsSep " ";
# vendored libuvc: don't build, just make sources available
libuvc-src = derive {
name = "libuvc-src";
# using fetchgit instead fetchFromGitHub because
@cipharius
cipharius / RPN-calculator.hs
Created September 10, 2020 08:36
Simple revers polish notation parser and calculator in Haskell
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text as T
import Data.Attoparsec.Text
data Op
= Add Op Op
| Sub Op Op
| Mul Op Op
| Div Op Op
| Const Double
@cipharius
cipharius / random-camera-sway.lua
Last active February 9, 2020 06:46
Roblox random camera sway
local camera = workspace.CurrentCamera
local fixedCFrame = CFrame.new(0, 100, 0) -- CFrame of default looking direction/position
local rate = 1/5 -- Speed at which the camera will sway
local xSwayAngle = math.pi/9 -- X angle change interval
local ySwayAngle = math.pi/9 -- Y angle change interval
local rollAngle = math.pi/18 -- Roll angle change interval
function update(t)
local phi = math.pi/2 + math.noise(t*rate, 0) * xSwayAngle
local theta = math.pi/2 + math.noise(t*rate, 10) * ySwayAngle
@cipharius
cipharius / sort-selections.kak
Last active April 18, 2024 14:39
Sort kakoune selections using GNU sort utility
define-command sort-selections -params 0.. -override -docstring '
sort-selections: Sort current selections using GNU sort utility
All parameters will be passed to the GNU sort utility
' %{
# Copy current selections to a temporary sort buffer
execute-keys %{"sy}
edit -scratch *sort-selections*
execute-keys %{"s<a-p>}
# Seperate selections with null characters
proc planeFit(cluster: Tensor[float], threshold = 10e-2): Option[Plane] =
## Fit points to a 3D plane
# At least 3 points are required to fit a plane
if cluster.shape[0] < 3: return
let points = cluster[_, 0..2]
let mean = points.mean(axis=0)
let delta = points .- mean
let covarMatrix = delta.covariance
for slice in inputTensor.axis(0):
if slice[0, dotProductColumn] >= 0:
a.add(slice)
else:
b.add(slice)
@cipharius
cipharius / unrollLoop.nim
Created June 7, 2018 21:46
Macro for unrolling for loop with generic range iterators
import macros
proc replaceIdent(node, ident: NimNode, replacement: NimNode) =
for i, child in node:
if child == ident:
node[i] = replacement.copy()
if child.len > 0:
child.replaceIdent(ident, replacement)
@cipharius
cipharius / yes-in-nim.md
Last active January 22, 2018 17:41
Blazing fast yes in Nim

Recently I stumbled upon a post which takes a closer look at the yes command line tool. The main purpose of it is to write endless stream of a single letter y at a ridiculous speed.

On the first glance this seems like a really simple problem, just two lines of Nim and you're done, right?

while true:
  echo "y"

And indeed, this gives us plenty of y's. But when we take a look at the write speed..

@cipharius
cipharius / yes.nim
Last active January 22, 2018 02:09
Yes implementation in Nim
import os
# Handle Ctrl-C signal
setControlCHook(proc() {.noconv.} = quit 0)
# Use POSIX write
proc write(fd: cint, buffer: pointer, count: cint) {.header: "<unistd.h>", importc: "write".}
const pageSize = 4096
var yes: string