Skip to content

Instantly share code, notes, and snippets.

View optozorax's full-sized avatar

ilya sheprut optozorax

View GitHub Profile
@Onefabis
Onefabis / currentLayer.py
Last active May 31, 2022 15:59
layout switcher for Qmk keyboard
'''
1. Open src.c in lang shift lib and add this function right next to the 'lang_activate' function
########################################
void lang_toggle(int externalLang) {
if (lang_current == lang_should_be && timer_read() - lang_timer >= 200) {
if (externalLang == 0){
layer_off(2);
} else if (externalLang == 1){
layer_on(2);

Postfix macros in Rust

The problem

Rust has many postfix combinators, for example the .unwrap_or(x) and .unwrap_or_else(|| x) functions. They are useful if you want to extract some value from an optionally present value, or if not, provide an alternative value. It's really nice and tidy to read:

@AnthonyMikh
AnthonyMikh / main.rs
Created May 3, 2020 17:02
Решение задачи с Хабра от @Milein: найти максимальное количество идущих подряд символов для каждого сивола строки
fn cut_repeating_prefix(source: &str) -> Option<(char, usize, &str)> {
let mut chars = source.char_indices();
let first = chars.next()?.1;
let len = chars
.find_map(|(i, ch)| if ch != first { Some(i) } else { None })
.unwrap_or(source.len());
let count = len / first.len_utf8();
let (_pre, post) = source.split_at(len);
Some((first, count, post))
}
@mhamilt
mhamilt / get_mouse_position_macos.cpp
Last active June 19, 2022 18:31
Get the mouse position in macOS with C++
#include <ApplicationServices/ApplicationServices.h>
#include <iostream>
#include <string>
///print out location in a nice way to std cout
void fancyPrintLocation(CGPoint location);
/// This callback will be invoked every time the mouse moves.
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type,
CGEventRef event, void *refcon)
// Code from: http://patshaughnessy.net/2020/1/20/downloading-100000-files-using-async-rust
//
// Cargo.toml:
// [dependencies]
// tokio = { version = "0.2", features = ["full"] }
// reqwest = { version = "0.10", features = ["json"] }
// futures = "0.3"
use std::io::prelude::*;
use std::fs::File;
@bazad
bazad / if_value.h
Created September 11, 2019 22:44
A C preprocessor macro to test whether a macro parameter has a value.
//
// if_value.h
// Brandon Azad
//
// Public domain
//
#ifndef IF_VALUE
/*
@optozorax
optozorax / random.cpp
Last active January 12, 2019 12:54
random by mt19937
#include <random>
double random(void) {
static std::mt19937 generator(time(0));
static std::uniform_real_distribution<double> distribution(0, 1);
return distribution(generator);
}
@prideout
prideout / servewasm.py
Created November 8, 2018 00:44
Python WASM server
#!/usr/bin/env python3
import http.server
import socketserver
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
Handler.extensions_map.update({
'.wasm': 'application/wasm',
@davidbgk
davidbgk / server.py
Created April 11, 2017 15:39
An attempt to create the simplest HTTP Hello world in Python3
import http.server
import socketserver
from http import HTTPStatus
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(HTTPStatus.OK)
self.end_headers()
self.wfile.write(b'Hello world')
@ugik
ugik / sample code from tflearn ANN
Last active April 29, 2018 14:14
2-layer perceptron code from tflearn example
# Build neural network
net = tflearn.input_data(shape=[None, 5])
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 2, activation='softmax')
net = tflearn.regression(net)
# Define model and setup tensorboard
model = tflearn.DNN(net, tensorboard_dir='tflearn_logs')
# Start training (apply gradient descent algorithm)