Skip to content

Instantly share code, notes, and snippets.

View tfc's full-sized avatar

Jacek Galowicz tfc

View GitHub Profile
@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 / 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 / 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)
@tfc
tfc / hydra_no_restrict.patch
Created October 21, 2018 07:58
hydra patch for relaxing the evaluation restriction
diff --git a/src/hydra-eval-jobs/hydra-eval-jobs.cc b/src/hydra-eval-jobs/hydra-eval-jobs.cc
--- a/src/hydra-eval-jobs/hydra-eval-jobs.cc
+++ b/src/hydra-eval-jobs/hydra-eval-jobs.cc
@@ -281,7 +281,7 @@ int main(int argc, char * * argv)
/* Prevent access to paths outside of the Nix search path and
to the environment. */
- evalSettings.restrictEval = true;
+ evalSettings.restrictEval = false;