nevalang / neva

🌊 Dataflow programming language with static types and implicit parallelism. Compiles to machine code and Go
https://nevalang.org
MIT License
124 stars 7 forks source link

Ternary/if-then-else as HOCs #691

Closed emil14 closed 3 weeks ago

emil14 commented 4 months ago

Naive implementation and example

interface IIfHandler<T>(data T) (res bool)
interface IThenHandler<T, Y>(data T) (res Y)
interface IElseHandler<T>(data T) (res Y)

flow IfElse<T, Y>(data T) (res Y) {
    nodes {
        if IIfHandler<T>
        then IThenHandler<T, Y>
        else IElseHandler<T, Y>
    }
    :data -> if
    if:then -> then -> :res
    if:else -> else -> :res
}

// ---

flow Main(start) (stop) {
    nodes {
        IfElse<int, any> {
            if Eq42
            then ToString
            else ToFloat
        }
        Println
    }
    42 -> ifElse -> println -> :stop
}
emil14 commented 4 months ago

Perhaps more type safe version

interface IIfHandler<T, Y, U>(data T) (then Y, else U)
interface IThenHandler<T, Y>(data T) (res Y)
interface IElseHandler<T, Y>(data T) (res Y)

flow Condition<T, Y, U, I, O>(data T) (then I, else O) {
    nodes {
        if IIfHandler<T, Y, U>
        then IThenElseHandler<Y, I>
        else IElseHandler<U, O>
    }
    :data -> if
    if:then -> then -> :then
    if:else -> else -> :else
}
emil14 commented 3 weeks ago

probably collide with #727

emil14 commented 3 weeks ago

will close for now, looks like we have better ideas about if and ternary at the moment