Skip to content

Instantly share code, notes, and snippets.

@Megaprog
Megaprog / test.rs
Created November 8, 2019 13:47
Rust test deal with panic
#[test]
fn test_something_interesting() {
run_test(|| {
let true_or_false = do_the_test();
assert!(true_or_false);
})
}
fn run_test<T>(test: T) -> ()
where T: FnOnce() -> () + panic::UnwindSafe
@Megaprog
Megaprog / Rfc3339Test.java
Last active February 14, 2025 08:10
RFC3339 parsing by java.time (only parsing is possible)
package rfc3339;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
@Megaprog
Megaprog / config.properties
Last active October 28, 2019 11:01
How to append a port to host from ansible inventory
hazelcast.hosts={{ groups.api|zip_longest([], fillvalue=':5701')|map('join')|join(',') }}
endpoint.hosts={{ ['http://']|product(groups.query_gui)|map('join')|product([':8099'])|map('join')|join(',') }}
class EnoughParticipantsMonteCarlo extends EnoughParticipants {
override def calculate(probabilities: List[Double], needed: Int): Double = {
(1 to attempts).par.map(i => probabilities.map(p => dice(p)).sum).count(successful => successful >= needed) / attempts.toDouble
}
val Precision = 0.001
val attempts = Math.ceil(Math.pow(1 / Precision, 2)).toInt
trait EnoughParticipants {
/** Given probabilities of each participant attending in "probabilities",
* calculate the probability that at least "needed" participants will participate.
*
* The probability has to be accurate to the precision of 0.001.
*/
def calculate(probabilities: List[Double], needed: Int): Double
}