Skip to content

Instantly share code, notes, and snippets.

View cblp's full-sized avatar

Yuriy Syrovetskiy cblp

  • Montenegro
View GitHub Profile
class RorMethod1:
"""
RorMethod1,2 classes act as functools.partial with method-like ror application.
"""
def __init__(self, function):
""" function may be print or sorted """
self.function = function
def __ror__(self, value):
return self.function(value)
def __call__(self, *args):
@cblp
cblp / printTest.hs
Last active August 29, 2015 14:17
Why?
class Show' a where show' :: a -> String
instance Show' Integer where show' = show
main = do
putStrLn $ show 42 -- OK
putStrLn $ show' 42 -- Error! Why?
import Control.Applicative
newtype Noop a = Noop { run :: () }
instance Functor Noop where
fmap = const $ const $ Noop ()
instance Applicative Noop where
pure = const $ Noop ()
(<*>) = const $ const $ Noop ()
@cblp
cblp / antiId.cpp
Last active August 29, 2015 14:23
#include <iostream>
using namespace std;
template <typename A>
A antiId(A x) { return x; }
template <>
int antiId<int>(int) { return 42; }
int main() {
{-# OPTIONS -Wall -Werror #-}
antiId :: a -> a
antiId x = x
{-# NOINLINE antiId #-}
{-# RULES
"antiId@Int" forall (x :: Int). antiId x = 42
#-}
data A = A
@cblp
cblp / polymorphic_recursion.cpp
Last active August 29, 2015 14:23
Polymorphic recursion
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
template <typename a>
struct Nested {
virtual
int length() const = 0;
};
@cblp
cblp / avg.hs
Created July 6, 2015 12:47
avg
{-# LANGUAGE BangPatterns, ScopedTypeVariables #-}
import Control.Applicative ( liftA2 )
import Control.DeepSeq ( deepseq )
import Control.Monad ( forM_ )
import Control.Monad.ST.Strict ( runST )
import Criterion ( bench, bgroup, nf, runAndAnalyse )
import Criterion.Config ( Config(..), defaultConfig, ljust )
import Criterion.Environment ( measureEnvironment )
import Criterion.Monad ( withConfig )
$ which ghc
/usr/bin/ghc
$ file /usr/bin/ghc
/usr/bin/ghc: symbolic link to `../lib/ghc/bin/ghc'
$ file /usr/lib/ghc/bin/ghc
/usr/lib/ghc/bin/ghc: symbolic link to `ghc-7.6.3'
$ file /usr/lib/ghc/bin/ghc-7.6.3
@cblp
cblp / a.cpp
Created September 20, 2015 18:22
C++ lambda on lambda over lambda
#include <iostream>
#include <vector>
using namespace std;
int main() {
auto add = [](auto x){
return [=](auto y){
return x + y;
};
};
@cblp
cblp / a.cpp
Last active September 20, 2015 20:29
C++ tail recursion optimization (TCO)
#include <iostream>
using namespace std;
#include <experimental/optional>
using namespace std::experimental;
template <class value_type, class... argument_types>
struct Closure {
using arguments_type = tuple<argument_types...>;
using function_type = function<Closure(arguments_type)>;