Hashing issue between guava versions - hash

I was using guava 14 to do String hashing like so:
Hashing.sha256().newHasher().putString("String").hash().toString();
=>
4d1ca6dce72e20ce214b706168340683bb6b571a7c977c1a9fe029a1cc1c4d06
just upgraded to guava16,
calling this function:
Hashing.sha256().newHasher().putString("String", Charsets.UTF-8).hash().toString() gives me a different result.
=>
b2ef230e7f4f315a28cdcc863028da31f7110f3209feb76e76fed0f37b3d8580
I suspect that the old version was using default charset, but switching Charsets on guava16 doesn't give me the same result as in guava14. What did I do wrong here?

As stated in the docs of Guava 15, the replacement for the old putString(String) method is putUnencodedChars.

As Louis said, the replacement is Hasher.putUnencodedChars(). Or, you can use the shortcuts on the HashFunction interface:
Hashing.sha256().hashUnencodedChars("String").toString();

Related

Scala and Mockito: keyword "then" is deprecated

Future versions of Scala may use then as a keyword, as stated in SIP-12.
The compiler already shows a warning for it:
Usage of then as identifier is deprecated. It can be used as a keyword in future versions of scala.
Word then is reserved as a possible keyword in future versions of scala. It’s usage as identifier is deprecated. See SIP-12.
I'm using Mockito and have many occurrences of then methods called.
BDDMockito.then(entityService).should(Mockito.times(2)).findBy(any)
Does anyone know if there's an alternative to replace it?
In scala, wrapping any identifier in backticks "`" prevents it from being treated as a keyword.
BDDMockito.`then`(entityService).should(Mockito.times(2)).findBy(any)
This has most commonly been used to allow fields/variables/methods to be named type, but it should work for then as well. It's also sometimes used to embed spaces into identifiers.
case class Bar(`class`: Int, `type`: String) {
def `class with type`: String = s"${`class`}_${`type`}"
}
Bar(42, "skidoo").`class with type` == "42_skidoo"
Have you tried mockito-scala? its API is tailored for Scala and it caters for all the language features that the Java version doesn't

How can I parse a string to an integer with Reasonml/Bucklescript?

I'm learning Reasonml, and I can't find any function in the standard library to do so, neither of the Bucklescript Js modules. Is there any better option than using raw javascript?
Right now I'm achieving it with this function:
let parseint: string => int = [%raw {| x => parseInt(x, 10) |}];
int_of_string (and also float_of_string / bool_of_string) should do what you need.
It's in the standard lib, you should be able to search for it https://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html (that site will work better for you if you have the reason-tools browser extension installed so it auto-converts from OCaml to Reason syntax for you)
Note that all of those functions will throw an exception if the string isn't valid for that type (read the link to see how each works and what each expects for the string).
As #glennsl points out, when Bucklescript catches up with a more recent version of the OCaml compiler than 4.02.3, you may want to use the safer _opt variants, e.g. int_of_string_opt that returns a Some(number) or None instead, depending on how much you trust the input, how much you want to avoid exceptions, and how you want to deal with bad input (is it exceptional and should kill the program/stack, or is it normal and should be handled locally?).

Using the java.time API in scala

I'm trying out Java 8 Date Time Api java.time using Scala REPL. Just encountered the problem below:
I do understand that the keyword with is reserved in scala, but also it is used in the API. Any idea on how to mitigate the limits?
Try wrapping the with with tick marks as follows:
val nextWed = today.`with`(java.time.temporal.TemporalAdjusters.next(DayOfWeek.WEDNESDAY))
Adding the ticks designates it a literal identifier.

Int extension not applied to raw negative values

My extensions to the Int type do not work for raw, negative values. I can work around it but the failure seems to be a type inference problem. Why is this not working as expected?
I first encountered this within the application development environment but I have recreated a simple form of it here on the Playground. I am using the latest version of Xcode; Version 6.2 (6C107a).
That's because - is interpreted as the minus operator applied to the integer 2, and not as part of the -2 numeric literal.
To prove that, just try this:
-(1.foo())
which generates the same error
Could not find member 'foo'
The message is probably misleading, because the error is about trying to apply the minus operator to the return value of the foo method.
I don't know if that is an intentional behavior or not, but it's how it works :)
This is likely a compiler bug (report on radar if you like). Use brackets:
println((-2).foo())

Tilde in Scala found in Scalatra example code

Just ran into this sample code learning about Commands in Scalatra:
protected def handle: Handler = {
case c: CreateTodoCommand =>
add(newTodo(~c.name.value))
}
In this particular case, what exactly is the relevance of ~ in ~c.name.value? Not sure where to find more documentation on this particular symbol.
In Scala:
~x
translates to
x.unary_~
(this also applies to +,- and ! as explained in this post). So your example translates to:
add(newTodo(c.name.value.unary_~))
The documentation can hence be found at the type of value.
it seems to be related to the block of code commented out in here: https://github.com/scalatra/scalatra/blob/2.2.x_2.9/core/src/main/scala/org/scalatra/package.scala
that is the only unary tilde operator if found that could be working here. the others seem to mainly be bitwise not operators
It actually seems that this might also be some import from scalaz library, tho the import statements are missing. similar uses of ~Option[_] can be found elsewhere as well...