Skip to content

Instantly share code, notes, and snippets.

View hryniuk's full-sized avatar
🗿

Łukasz hryniuk

🗿
View GitHub Profile
@hryniuk
hryniuk / pre-commit-cargo-fmt
Last active February 7, 2024 09:21
Git `pre-commit` hook that checks Rust code style with `cargo fmt`
#!/bin/bash
diff=$(cargo fmt -- --check)
result=$?
if [[ ${result} -ne 0 ]] ; then
cat <<\EOF
There are some code style issues, run `cargo fmt` first.
EOF
exit 1
@hryniuk
hryniuk / executable.c
Created February 6, 2018 16:38 — forked from jdarpinian/executable.c
Add one line to your C/C++ source to make it executable.
///bin/true;COMPILER_OPTIONS="-g -Wall -Wextra --std=c99 -O1 -fsanitize=address,undefined";THIS_FILE="$(cd "$(dirname "$0")"; pwd -P)/$(basename "$0")";OUT_FILE="/tmp/build-cache/$THIS_FILE";mkdir -p "$(dirname "$OUT_FILE")";test "$THIS_FILE" -ot "$OUT_FILE" || $(which clang || which gcc) $COMPILER_OPTIONS -xc "$THIS_FILE" -o "$OUT_FILE" || exit;exec "$OUT_FILE" "$@"
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
@hryniuk
hryniuk / IOR list
Created January 6, 2018 16:07
Index of refraction of different materials; taken from http://forums.cgsociety.org/showthread.php?t=513458
Silver 0.18
Gold 0.47
Helium 1.000036
Hydrogen (gas) 1.00014
Water (gas) 1.000261
Oxygen (gas) 1.000276
Argon 1.000281
Air 1.0002926
Nitrogen (gas) 1.000297
Carbon Dioxide (gas) 1.000449
@hryniuk
hryniuk / RedisPythonPubSub1.py
Created September 29, 2017 11:29 — forked from jobliz/RedisPythonPubSub1.py
A short script exploring Redis pubsub functions in Python
import redis
import threading
class Listener(threading.Thread):
def __init__(self, r, channels):
threading.Thread.__init__(self)
self.redis = r
self.pubsub = self.redis.pubsub()
self.pubsub.subscribe(channels)
@hryniuk
hryniuk / db.sh
Last active June 19, 2017 11:30
Simple BASH database
#!/bin/bash
# copied from https://jvns.ca/blog/2017/06/11/log-structured-storage/
db_set() {
echo "$1,$2" >> database
}
db_get() {
grep "^$1," database | sed -e "s/^$1,//" | tail -n 1
@hryniuk
hryniuk / web-servers.md
Created May 31, 2017 15:14 — forked from willurd/web-servers.md
Big list of http static server one-liners

Each of these commands will run an ad hoc http static server in your current (or specified) directory, available at http://localhost:8000. Use this power wisely.

Discussion on reddit.

Python 2.x

$ python -m SimpleHTTPServer 8000
@hryniuk
hryniuk / for_each_template.cpp
Created April 15, 2017 12:27
Function template for mapping values with a given functor
#include <iostream>
#include <functional>
#include <vector>
template<typename Container, typename Function>
void f(Container&& c, Function&& f)
{
for (const auto& e : c) {
f(e);
}
@hryniuk
hryniuk / ensure.cpp
Created April 15, 2017 09:14
Ensure macro to check preconditions
#include <iostream>
#define ENSURE(x) do { \
if (not (x)) {std::cerr << __FILE__ << " " << __PRETTY_FUNCTION__ << ":" << __LINE__ << " " << #x << " assert failed!\n";} \
} while(false)
void f(int x)
{
ENSURE(x > 0);
}
@hryniuk
hryniuk / functors.cpp
Created April 6, 2017 22:54
Metafun with functors
#include <iostream>
#include <tuple>
template<typename Tuple, int Size, int Index=0>
struct Apply
{
void operator()(Tuple t) const
{
std::cout << std::get<Index>(t) << std::endl;
Apply<Tuple, Size, Index + 1>().operator()(t);
@hryniuk
hryniuk / p.py
Created March 29, 2017 09:52
Partial method application
import functools
class B():
def __init__(self, n):
self.n = n
def __call__(self):
print(self.n)
class A():