All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.mperry.fg.SimpleIOStaticExtension.groovy Maven / Gradle / Ivy

There is a newer version: 0.8
Show newest version
package com.github.mperry.fg

import fj.F
import fj.control.Trampoline
import fj.data.Stream
import groovy.transform.TypeChecked

/**
 * Created with IntelliJ IDEA.
 * User: MarkPerry
 * Date: 13/12/13
 * Time: 12:10 PM
 * To change this template use File | Settings | File Templates.
 */
@TypeChecked
class SimpleIOStaticExtension {

    static  SimpleIO> sequenceWhileR(SimpleIO clazz, Stream> stream, F f) {
        if (stream.empty) {
            SimpleIO.lift(Stream.nil())
        } else {
            stream.head().flatMap({ A a ->
                if (!f.f(a)) {
                    SimpleIO.lift(Stream.nil())
                } else {
                    def t = stream.tail()._1()
                    sequenceWhileR(clazz, t, f).map({ Stream s -> s.cons(a)} as F, Stream>)
                }
            } as F)
        }
    }

    static  Trampoline>> empty() {
        Trampoline.pure(SimpleIO.lift(Stream.nil()))
    }

    static  Trampoline>> sequenceWhileC(SimpleIO clazz, Stream> stream, F f) {
        if (stream.empty) {
            empty()
        } else {
            // add loop here
            def io = stream.head().map({ A a ->
                def b = f.f(a)
                if (!b) {
                    empty()
                } else {
                    def tail = stream.tail()._1()
                    sequenceWhileC(clazz, tail, f)
                }
            } as F>>>)
            SimpleIO.transform(io)
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy