Skip to content

Instantly share code, notes, and snippets.

View tfc's full-sized avatar

Jacek Galowicz tfc

View GitHub Profile
@tfc
tfc / short_file.cpp
Created February 20, 2016 20:14
__SHORT_FILE__ Macro
#include <stdio.h>
using cstr = const char * const;
static cstr constexpr past_last_slash(cstr str, cstr last_slash)
{
return
*str == '\0' ? last_slash :
*str == '/' ? past_last_slash(str + 1, str + 1)
: past_last_slash(str + 1, last_slash);
}
@tfc
tfc / comparison_impl.cpp
Last active November 22, 2017 19:14
comparison_impl<T> implementation example code
#include <assert.h>
template <typename T>
class comparison_impl
{
const T& thisT() const { return *static_cast<const T*>(this); }
public:
// operator== is implemented by T
template <typename U>
@tfc
tfc / base64_enc.cpp
Last active March 1, 2016 11:53
Iterator experiment with use case of base64 encoding
#include <iostream>
#include <assert.h>
// This one vanishes in library
template <typename T, typename DerefT>
class iterator_facade
{
T& thisT() { return static_cast< T&>(*this); }
const T& thisT() const { return static_cast<const T&>(*this); }
public:
@tfc
tfc / uncaught_exception.cpp
Created March 18, 2016 09:55
How uncaught exceptions are resolved
#include <iostream>
#include <string>
class Foo
{
const std::string str;
public:
Foo(const std::string &str_) : str{str_} {}
~Foo() { std::cout << str << " Foo's dtor called." << std::endl; }
};
@tfc
tfc / trivial_generator_example.py
Last active September 4, 2016 15:33
Trivial python generator example
odd_squares = []
for x in range(0, 19):
if x % 2 == 1:
odd_squares.append(x * x)
print(odd_squares)
# same in one line:
print([x * x for x in range(0, 19) if x % 2 == 1])
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cassert>
using namespace std;
template <typename IT>
#include <iostream>
int main() {
auto List ([](auto ...params) {
return [=](auto f) {
return f(params...);
};
});
auto Tail ([=](auto f) {
return f([=](auto p, auto ...rest) {
@tfc
tfc / optional_monad.cpp
Last active July 4, 2017 07:39
Transform a function from `R f(P ... ps)` to `std::optional<R> f(std::optional<P> ... ps)` and use it monadic style. needs C++17.
#include <iostream>
#include <optional>
/// library part
template <typename R, typename ... P>
auto lift_optional(R (*f)(P ... ps))
{
return [f](std::optional<P> ... xs) -> std::optional<R> {
if ((xs && ...)) {
@tfc
tfc / RocketExample.hs
Created November 19, 2017 14:59
class example
#!/usr/bin/env stack
{- stack --install-ghc runghc --package aeson --package hspec -}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Data.Aeson
import Test.Hspec
-- Rocket.hs library file
@tfc
tfc / trafficLight.hs
Created March 1, 2018 11:30
Demonstration of a traffic light with state in haskell
#!/usr/bin/env stack
-- stack --resolver lts-10.7 --install-ghc runghc
import Control.Concurrent (threadDelay)
import Control.Monad.State
data TrafficLightColor = Red | Yellow | Green deriving Show
type TrafficLightState = (TrafficLightColor, Int)