scala - Why does scalaz's implementation of Monoid for Option evaluate the f2 function twice? -


the definition of scalaz's option monoid follows:

implicit def optionmonoid[a: semigroup]: monoid[option[a]] = new monoid[option[a]] {   def append(f1: option[a], f2: => option[a]) = (f1, f2) match {     case (some(a1), some(a2)) => some(semigroup[a].append(a1, a2))     case (some(a1), none)     => f1     case (none, some(a2))     => f2     case (none, none)         => none   }    def zero: option[a] = none } 

f2 pass name param means each call evaluate expression. why should evaluated again when evaluated in pattern match? returning some(a2) should same result , expression f2 expensive.

am missing something?

scalaz's option.scala source

it looks me written highlight symmetry of problem , clarity, not speed. can't drop laziness of second argument since semigroup defines way, , in other contexts times laziness of second argument may essential. preserve visual representation of symmetry of problem, want add

val g2 = f2  // force evaluation (f1, g2) match { ... 

or somesuch.

(it nice if name arguments called lazy automatically memoize them.)


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -