This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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 | |
/// ``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); |
NewerOlder