Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add csharp complex generator example #359

Open
lupin-de-mid opened this issue Sep 14, 2021 · 3 comments
Open

Add csharp complex generator example #359

lupin-de-mid opened this issue Sep 14, 2021 · 3 comments

Comments

@lupin-de-mid
Copy link
Contributor

In tutorial we have sample for genarator of IpAddress

open System.Net
let ipAddressGen : Gen<IPAddress> = gen {
    let! addr = Range.constantBounded () |> Gen.byte |> Gen.array (Range.singleton 4)
    return System.Net.IPAddress addr
}

Anybody could check my translation to csharp? It is'nt idiomatic csharp IMHO

var byteRange = Range.ConstantBoundedByte();
var byteGen = Gen.Byte(byteRange);
var byteArrayGen = Hedgehog.Gen.array(Range.FromValue(4), byteGen);
var ipAddressGen = byteArrayGen.Select(bytes => new IPAddress(bytes));

May be it's suitable to add csharp.md with more examples?
Right now I try get how to use Property.CounterExample

@ghost
Copy link

ghost commented Sep 15, 2021

Anybody could check my translation to csharp? It is'nt idiomatic csharp IMHO

@lupin-de-mid I think the following example may be more what you're looking for.

var ipAddressGen =
    Gen.Byte(Range.ConstantBoundedByte())
       .Array(Range.FromValue(4))
       .Select(bytes => new IPAddress(bytes));

Also, since Hedgehog.Linq adheres to the LINQ "interface", you can use query syntax as well:

var ipAddressGen =
    from bytes in Gen.Byte(Range.ConstantBoundedByte()).Array(Range.FromValue(4))
    select new IPAddress(bytes);

May be it's suitable to add csharp.md with more examples?

I think that's a good idea. I am starting to think the README.md is a little cluttered.

Right now I try get how to use Property.CounterExample

From the C# side this may be a little clunky still. I'm on a work machine right now and don't have the repo cloned, I'll take a look after I switch to my personal machine.

@TysonMN
Copy link
Member

TysonMN commented Sep 16, 2021

I think the first code block by @adam-becker is better. The advantage of LINQ query syntax is avoiding the pyramid of doom that results from nested calls to Bind. Since that isn't the case here, its use is not warranted.

@ghost
Copy link

ghost commented Sep 16, 2021

Since that isn't the case here, its use is not warranted.

Just noting that it's possible :)

@ghost ghost added this to the 0.12.0 milestone Sep 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants