Skip to content

Instantly share code, notes, and snippets.

View jesperdj's full-sized avatar

Jesper de Jong jesperdj

View GitHub Profile
use crossbeam_channel;
use crossbeam_utils::thread;
/// Multi-threaded processing using Crossbeam.
///
/// This function takes a generator which is an `Iterator` over values of type `V`, a processor function that processes values of type `V` into results of
/// type `R`, and an aggregator function which accepts results of type `R`.
///
/// It starts one generator thread and a number of processor threads. The generator thread gets values from the generator and sends them to an input channel.
/// The processor threads receive from the input channel, call the processor function to compute a result for each value and send the results to an output
@jesperdj
jesperdj / partition.rs
Last active July 17, 2020 17:18
Partition a slice in place.
/// Partitions a slice in place by a predicate.
///
/// The elements in `slice` for which `predicate` returns `true` are all moved to the left side of the slice, leaving
/// the elements for which `predicate` returns `false` on the right side.
///
/// If the slice can be partitioned into two non-empty parts, returns `Some` with the index of the first element of the
/// right partition. Otherwise, returns `None`.
///
/// # Examples
/// ```
@jesperdj
jesperdj / memo.rs
Created July 3, 2020 18:33
Memoization in Rust - demo of Cell and Rc
use std::cell::Cell;
use std::rc::Rc;
// Memoization - lazily compute some value and cache it once it's been computed.
//
// This uses a Cell containing an Option containing an Rc.
//
// A Cell is a structure that provides interior mutability - it's possible to mutate the cell's value without requiring
// the cell itself, or the struct that contains it (in this case the struct Memo) to be mutable. There are to types of
// cell: Cell and RefCell. A Cell owns the value that's inside it. If that value is non-copy, then the only way to
@jesperdj
jesperdj / binarytree.rs
Last active June 29, 2020 22:31
Binary tree with depth-first iterator in Rust.
// Binary tree and depth-first iterator in Rust.
//
// Pay close attention to the types, they are carefully chosen:
//
// enum BinaryTree:
// - We want a Leaf to own its value of type T, so its value is a T (not a reference or pointer to T).
// - We want an Internal node to own its two children, so its elements are Boxes (owned heap pointers).
// - The methods new_internal() and new_leaf() in its impl return a Box<BinaryTree<T>> and not a BinaryTree<T>
// because we want to allocate nodes on the heap and it would be very inconvenient if the caller had to
// create a Box for each node.
@jesperdj
jesperdj / expr.rs
Created June 29, 2020 08:01
Algebraic data types: enums in Rust - expression evaluator
use std::collections::HashMap;
use Expression::*;
#[derive(Debug)]
enum Expression<'a> {
Literal(f64),
Variable(String),
Add(&'a Expression<'a>, &'a Expression<'a>),
Subtract(&'a Expression<'a>, &'a Expression<'a>),
@jesperdj
jesperdj / index.html
Last active January 22, 2020 18:17
Navbar with CSS flexbox example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>Flex Navbar</title>
</head>
@jesperdj
jesperdj / Example.java
Created December 13, 2016 16:37
An enum-like class with a type parameter and a values() method.
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public final class Example<T> {
public static final Example<Integer> INT = new Example<>(Integer.class, () -> 123);
public static final Example<String> STR = new Example<>(String.class, () -> "Test");
@jesperdj
jesperdj / HexUtil.java
Last active August 29, 2015 14:21
Utility class for parsing and formatting hex strings to / from byte arrays.
/*
* Copyright 2015 Jesper de Jong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@jesperdj
jesperdj / PackageUtils.java
Last active September 30, 2015 13:31
Utilities for working with Java packages.
/*
* Copyright 2014 Jesper de Jong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@jesperdj
jesperdj / Example01.java
Last active February 10, 2018 03:19
Examples with Java 8 lambda expressions.
package org.jesperdj.lambda;
import java.util.ArrayList;
import java.util.List;
public class Example01 {
// Using a for-loop (external iteration)
public Iterable<String> getBestStudentsOfYear(Iterable<Student> students, int year) {
List<String> result = new ArrayList<>();