Skip to content

Instantly share code, notes, and snippets.

@artemklevtsov
artemklevtsov / enum.go
Created May 26, 2022 18:33 — forked from lummie/enum.go
Golang Enum pattern that can be serialized to json
package enum_example
import (
"bytes"
"encoding/json"
)
// TaskState represents the state of task, moving through Created, Running then Finished or Errorred
type TaskState int
@artemklevtsov
artemklevtsov / radix.nim
Created February 21, 2022 16:40 — forked from treeform/radix.nim
Radix sort in Nim.
## Radix sort in O(n) unlike other sorting methods that can be way worse.
## But radix sort needs the bits to operate on, which makes it different then
## other sorting algorithms.
proc getMax(arr: seq[SomeInteger]): SomeInteger =
for i, e in arr:
if i == 0:
result = e
elif e > result:
result = e
@artemklevtsov
artemklevtsov / PKGBUILD
Last active May 5, 2020 06:35
PKGUILBD for the fancon
# Maintainer: Artem Klevtsov <a.a.klevtsov@gmail com>
_pkgbase=fancon
pkgname=fancon-git
pkgver=0.20.1.r3.gbebdfc8
pkgrel=1
pkgdesc="A Linux fan control daemon"
arch=('x86_64')
url="https://github.com/hbriese/${_pkgbase}"
license=('Apache')

I tried searching for the data.table functions to trunc dates.

Bug in round.IDate(x, "week")

round.IDate for the weeks (IDateTime.R#81) seems inaccurate: first week of year have a 6 days instead 7 (fix: should be yday(x) - 1L)

> rle(unclass(round(as.IDate(0:21), "week")))
Run Length Encoding
 lengths: int [1:4] 6 7 7 2
@artemklevtsov
artemklevtsov / floor_date.R
Last active March 16, 2020 17:46
Floor dates function
#' @title Floor Dates
#' @param x A vector of date.
#' @param unit A character string specifying a time unit.
#' @param start.on.monday Should the week start on Mondays or Sundays?
#' @return An object of class "Date".
floor_date <- function(x, unit = c("day", "week", "month", "quarter", "year"), start.on.monday = TRUE) {
stopifnot(is(x, "Date"))
unit <- match.arg(unit)
if (unit == "day") {
return(x)
#' @title Перекодировка столбцов таблицы
#' @param x Вектор или таблица.
#' @param enc Строка, содержащая название кодировки. Если параметр не задан, используется автоопределение кодировки.
#' @param defqult Строка, содержащая кодировку по умолчанию, которая будет использована в случае, если автоматически определить кодировку не удалось.
#' @return Исходный объект, с перекодированными элементами.
#' @details
#' Функция изменяет объект на месте, то есть будет изменён переданный объект.
#' Для определения кодировки используется пакет `Ruchardet`.
#' Предполагается, что вектор (столбец) содердит только одну кодировку.
#' @importFrom data.table set setDT
@artemklevtsov
artemklevtsov / utf8_to_int.cpp
Last active October 15, 2019 15:48
utf8ToInt Rcpp implementation
// [[Rcpp::plugins(cpp17)]]
#include <Rcpp.h>
#include <codecvt>
using namespace Rcpp;
// [[Rcpp::export(rng=false)]]
std::vector<unsigned long> utf8_to_int(const std::string& x) {
std::size_t n = x.size();
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export(rng = false)]]
List df2list(const DataFrame& x) {
std::size_t nrows = x.rows();
std::size_t ncols = x.cols();
CharacterVector nms = x.names();
List res(no_init(nrows));
for (std::size_t i = 0; i < nrows; ++i) {
#include <Rcpp.h>
using namespace Rcpp;
constexpr auto b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// [[Rcpp::export]]
std::string base64_encode(const RawVector& input) {
std::string out;
out.reserve(input.size() / 3 * 4 + 4);
@artemklevtsov
artemklevtsov / split.cpp
Last active August 16, 2019 08:05
Rcpp CRLF string split
// [[Rcpp::plugins(cpp17)]]
#include <Rcpp.h>
#include <regex>
// [[Rcpp::export(rng=false)]]
Rcpp::CharacterVector str_split1(const char* s) {
Rcpp::CharacterVector out;
std::regex re("\r\n");
auto it = std::cregex_token_iterator(s, s + std::strlen(s), re, -1);
auto end = std::cregex_token_iterator();