Skip to content

Instantly share code, notes, and snippets.

View ythirion's full-sized avatar

Yoan Thirion ythirion

View GitHub Profile
public class Addition_properties_with_vavr_test {
private final int size = 10_000, tries = 100;
@Test
public void commutative() {
Property.def("Addition is commutative")
.forAll(Arbitrary.integer(), Arbitrary.integer())
.suchThat((x, y) -> Calculator.add(x, y) == Calculator.add(y, x))
.check(size, tries)
@RunWith(JUnitQuickcheck.class)
public class Account_properties {
@Property
public void balance_should_be_decremented_of_the_amount(
@From(AccountGenerator.class) Account account,
@From(WithdrawGenerator.class) Withdraw withdraw) {
withEnoughMoney(account, withdraw);
withoutReachingMaxWithdrawal(account, withdraw);
public class WithdrawGenerator extends Generator<Withdraw> {
public WithdrawGenerator() {
super(Withdraw.class);
}
@Override
public Withdraw generate(SourceOfRandomness sourceOfRandomness, GenerationStatus generationStatus) {
return new Withdraw(sourceOfRandomness.nextDouble(), new Date());
}
import io.vavr.control.Try;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@Getter
public class Account {
private final double balance;
private final boolean isOverdraftAuthorized;
import com.pholser.junit.quickcheck.Property;
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck;
import org.junit.Assert;
import org.junit.runner.RunWith;
@RunWith(JUnitQuickcheck.class)
public class Addition_properties {
@Property
public void commutative(int randomX, int randomY) {
int result1 = Calculator.add(randomX, randomY);
<properties>
<junit-quickcheck.version>0.9.1</junit-quickcheck.version>
</properties>
<dependency>
<groupId>com.pholser</groupId>
<artifactId>junit-quickcheck-core</artifactId>
<version>${junit-quickcheck.version}</version>
<scope>test</scope>
</dependency>
@ythirion
ythirion / real_life.cs
Created December 18, 2019 15:56
Real life example
private readonly PersonRepository personRepository;
private readonly ILogger logger;
private readonly TwitterService twitterService;
public PersonService(ILogger logger)
{
this.logger = logger;
personRepository = new PersonRepository();
twitterService = new TwitterService();
}
@ythirion
ythirion / try.cs
Created December 18, 2019 15:48
Try
public void file_monad_example()
{
GetLine()
.Bind(ReadFile)
.Bind(PrintStrln)
.Match(success => Console.WriteLine("SUCCESS"),
failure => Console.WriteLine("FAILURE"));
}
static Try<string> GetLine()
@ythirion
ythirion / foldvsreduce.cs
Created December 18, 2019 15:44
Fold vs reduce
[Fact]
public void fold_vs_reduce()
{
//fold takes an explicit initial value for the accumulator
//Can choose the result type
var foldResult = List(1, 2, 3, 4, 5)
.Map(x => x * 10)
.Fold(0m, (x, s) => s + x); // 150m
//reduce uses the first element of the input list as the initial accumulator value
@ythirion
ythirion / partial.cs
Created December 18, 2019 15:38
partial app
static Func<int, int, int> Multiply = (a, b) => a * b;
static Func<int, int> TwoTimes = par(Multiply, 2);
[Fact]
public void partial_app_example()
{
Multiply(3, 4); // 12
TwoTimes(9); // 18
}