mperry/functionalgroovy

Monad Comprehension with Either fails: method flatMap not found in Either$RightProjection

LuisMuniz opened this issue · 0 comments

Hi,

Is there something missing in Either to be able to use it in your monad comprehension?

class ComprehensionSpec extends Specification {
    def "Uses fj.Either in a monad comprehension"(){
        given:
        def s=Either.right("1")
        def i=Either.right(1)

        when:
        def sum=Comprehension.foreach {
            i1 << s.right()
            i2 << i.right()
            yield {
                (i1 as Integer) + i2
            }
        }


        then:
        sum.isRight()
    }
}

In this example I get an exception:

groovy.lang.MissingMethodException: No signature of method: fj.data.Either$RightProjection.flatMap() is applicable for argument types: (com.github.mperry.fg.Comprehension$_process_closure3) values: [com.github.mperry.fg.Comprehension$_process_closure3@5830b878]
Possible solutions: flatten(), flatten(groovy.lang.Closure)

    at com.github.mperry.fg.Comprehension.process(Comprehension.groovy:66)
    at com.github.mperry.fg.Comprehension.startProcessing(Comprehension.groovy:26)
    at com.github.mperry.fg.Comprehension.yield(Comprehension.groovy:21)
    at com.b2boost.commons.monad.either.ComprehensionSpec._spock_feature_0_0_closure1(ComprehensionSpec.groovy:20)
    at com.b2boost.commons.monad.either.ComprehensionSpec._spock_feature_0_0_closure1(ComprehensionSpec.groovy)
    at com.github.mperry.fg.Comprehension.foreach(Comprehension.groovy:98)
    at com.b2boost.commons.monad.either.ComprehensionSpec.Uses fj.Either in a monad comprehension(ComprehensionSpec.groovy:17)