Skip to content
Dinko Srkoč edited this page Aug 7, 2011 · 3 revisions

On this page are documented some of the less expected behaviours one can encounter using Monadologie.

executing instance methods inside foreach

The wrong way:

import static hr.helix.monadologie.MonadComprehension.foreach

class A {
    def foo() { [1, 2] }

    def run() {
        foreach {
            a = takeFrom { foo() }
            b = takeFrom { [3, 4] }
            yield { a + b }
        }
    }
}

new A().run() // throws groovy.lang.MissingMethodException, complains about foo()

The right way (notice the use of this):

import static hr.helix.monadologie.MonadComprehension.foreach

class A {
    def foo() { [1, 2] }

    def run() {
        foreach {
            a = takeFrom { this.foo() }
            b = takeFrom { [3, 4] }
            yield { a + b }
        }
    }
}

assert new A().run() == [4, 5, 5, 6]

the use of yield

Current implementation allows using yield outside of the foreach expression.

import static hr.helix.monadologie.MonadComprehension.foreach

def unyielded = foreach {
    a = takeFrom { [1, 2] }
    b = takeFrom { [4, 5] }
}

assert unyielded.yield { a + b } == [5, 6, 6, 7]
assert unyielded.yield { a - b } == [-3, -4, -2, -3]

Whether this feature will survive future changes is hard to predict at the moment.

Clone this wiki locally