Skip to content

Instantly share code, notes, and snippets.

@IvanVergiliev
IvanVergiliev / tuple.cpp
Last active August 23, 2023 14:10
C++ Tuple implementation
#include <cstdio>
template<typename First, typename... Rest>
struct Tuple: public Tuple<Rest...> {
Tuple(First first, Rest... rest): Tuple<Rest...>(rest...), first(first) {}
First first;
};
template<typename First>
@IvanVergiliev
IvanVergiliev / predicate_fold.scala
Created June 21, 2021 16:04
Fold a list of predicates
val andPredicates: Array[Column] = parseAndPredicates(...)
val compositePredicate: Column = andPredicates.foldLeft(lit(true))((x, y) => x && y)
@IvanVergiliev
IvanVergiliev / exponential_tree_transform
Created June 21, 2021 16:01
Exponential Tree Transformation Complexity
f(H) =
  f(H - 1) // for the first transform call
  +
  f(H - 1) // for the second transform call
= 2 * f(H - 1) =
= 2 * 2 * f(H - 2) = ... = 2 ^ (H - 1) * f(1) // unfolding further
import org.apache.spark.sql.Column
def treeFold(
  columns: List[Column],
  initialValue: Column,
  op: (Column, Column) => Column
): Column = {
  if (columns.isEmpty) {
    initialValue
  } else if (columns.length == 1) {
@IvanVergiliev
IvanVergiliev / tree_transform_dry.scala
Created June 17, 2021 20:35
Tree Transformation Algorithm with Code Reuse
def transformTree(tree: Tree): Option[Tree] = {
// Notice that there's no `canTranform` - we're using
// `transform().isDefined` to check for transformability!
  val transformedLeft = if (transform(tree.left).isDefined) {
    transform(tree.left)
} else None
...
}
@IvanVergiliev
IvanVergiliev / transform.scala
Created June 17, 2021 20:34
Tree Transformation Algorithm
def transformTree(tree: Tree): Option[Tree] = {
  val transformedLeft = if (canTransform(tree.left)) {
    transform(tree.left)
} else None
  val transformedRight = if (canTransform(tree.right)) {
    transform(tree.right)
} else None
val result = if (transformedLeft.isDefined && transformedRight.isDefined) {
#include <cstdlib>
#include <string>
// All of the below are not really standards-compliant, but good enough
// for experiments.
void* operator new(std::size_t size) {
printf("Calling new(%zu).\n", size);
return std::malloc(size);
}
package io.github.ivanvergiliev;
public class NonVolatileFlag {
private static boolean flag = false;
public static class FlagChecker implements Runnable {
public void run() {
long iter = 0;
while (flag == false) {
++iter;
@IvanVergiliev
IvanVergiliev / pipe.cc
Last active December 20, 2015 01:19
Custom binary search using std::lower_bound over used-defined iterators.
#include <cstdio>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;
struct TestCase {
int N;
int K;
@IvanVergiliev
IvanVergiliev / a_star.cc
Created October 23, 2012 00:06
Implementation of A* for the 8-puzzle
#include <cstdio>
#include <map>
#include <set>
#include <vector>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};