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 Generators For Hex Char And String Values #470

Merged
merged 1 commit into from
Aug 29, 2019

Conversation

isomarcte
Copy link
Member

No description provided.

@isomarcte
Copy link
Member Author

Is there anything I need to do to get this merged?

Copy link
Member

@ChristopherDavenport ChristopherDavenport left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me.

@ashawley
Copy link
Contributor

This seems like a nice first contribution. Making this generally useful might be a little more complicated, though. I wonder how much it is needed. What context were you using it for?

Gen.choose(97.toChar, 102.toChar), // a-f
Gen.choose(65.toChar, 70.toChar) // A-F
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be:

  def hexChar: Gen[Char] =  Gen.frequency(
    10 -> Gen.oneOf('0' to '9'),
     3 -> Gen.oneOf('a' to 'f'),
     3 -> Gen.oneOf('A' to 'F'))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I was trying to be consistent with the rest of the file.

*/
def hexStr: Gen[String] =
listOf(hexChar).map(_.mkString)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This produces a lengthy string by default (up to 100?). That works for your test with BigInteger, but may not for when people want byte, int or long from hex?

I guess you've copied the definition of Gen.numStr. I guess it has these same sort of problems.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I agree. I did this for consistency. That being said, I'm not too worried about this. If people want to create a String which represents a bounded type, such as i32, then it would be consistent with current idioms to just map on this generator.

@isomarcte
Copy link
Member Author

isomarcte commented Jul 11, 2019

@ashawley parsing or emitting is, in my experience, a pretty common task in CS domains. My specific use case was emitting values for an API which required hex numbers encoded in a JSON string.

@isomarcte
Copy link
Member Author

@ChristopherDavenport any thoughts on merging this?

@non
Copy link
Contributor

non commented Aug 29, 2019

@isomarcte Hey, I'm going through the PRs one more time before 1.14.1 and came back to this.

I'm not necessarily wedded to @ashawley's proposed definition, but I do think it would be best if we had equal probability of generating each of the 16 hex digits (right now the last 6 digits are twice as likely as the first 10). You could fix this the way @ashawley suggested, or by another method (I sort of like Gen.choose(Gen.choose("0123456789abcdef"), Gen.choose("0123456789ABCDEF")) due to how explicit it is).

I think your rationale for generating large strings in hexStr is correct. If you're willing to fix the frequencies of hexChar I'm happy to merge this before releasing 1.14.1.

I'm sure it's annoying to get this feedback after so long, but thanks for sticking with this!

@isomarcte
Copy link
Member Author

@non it's not annoying at all. I completely agree about your reasoning with the probability being equally distributed across the domain. I'll update it. Your proposed definition looks great to me.

@isomarcte
Copy link
Member Author

@non updated

@non
Copy link
Contributor

non commented Aug 29, 2019

Looks good! 👍

@non non merged commit eb7a85c into typelevel:master Aug 29, 2019
@isomarcte isomarcte deleted the hex-string-gen branch December 22, 2020 04:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants