Skip to content

Instantly share code, notes, and snippets.

View cipharius's full-sized avatar

Valts Liepiņš cipharius

  • Latvia
  • 13:32 (UTC +02:00)
View GitHub Profile
@cipharius
cipharius / json-sort.el
Last active June 26, 2018 17:52
Emacs JSON sort
(require 'json)
(defun sort-json (point)
"Sort JSON-like structure surrounding the point."
(interactive "d")
(let ((object-begin (nth 1 (syntax-ppss point)))
(begin-point (point)))
(when object-begin
(goto-char object-begin)
(forward-list)
@cipharius
cipharius / Vector2.lua
Last active November 17, 2017 06:58
2D Vector implementation in Lua
local Vector2 = {}
function Vector2.new(x, y)
local mt = {}
local unit,magnitude
x = x or 0
y = y or 0
function mt:__index(i)
@cipharius
cipharius / polish-notation.lua
Last active November 22, 2017 01:18
Polish notation parser in Lua
-- Operations definitions
local operations = {
["+"] = {
Arguments = 2,
Operation = function(a, b) return a + b end
},
["-"] = {
Arguments = 2,
Operation = function(a, b) return a - b end
},
@cipharius
cipharius / translate-to-josh.nim
Last active November 26, 2017 23:33
Algorithms to translate ASCII message to JoshScript code
import algorithm
import strutils
proc splitChars(str: string): seq[char] =
result = @[]
for ch in str:
add result, ch
proc translateSimple(text: string): string =
## Translates ASCII to JoshScript
@cipharius
cipharius / rbxForumFetcher.py
Created December 7, 2017 16:03
Script for fetching roblox forum posts
import requests
from bs4 import BeautifulSoup
from time import sleep
forumID = 32
# Get the required form content
request = requests.get("https://forum.roblox.com/Forum/ShowForum.aspx?ForumID={}".format(forumID))
soup = BeautifulSoup(request.text, "html.parser")
@cipharius
cipharius / inputActions.nim
Created December 8, 2017 07:46
Keyboard input actions for SDL2 on Nim
import strutils
import sdl2
type
ListenerPtr = proc (action: Action) {.closure.}
Action* = ref object
active: bool
kind: string
keysyms: seq[cint]
@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
@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 / 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)
for slice in inputTensor.axis(0):
if slice[0, dotProductColumn] >= 0:
a.add(slice)
else:
b.add(slice)