nevalang / neva

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

Unwrap Component 2.0 #612

Closed dorian3343 closed 4 months ago

dorian3343 commented 4 months ago

A new and improved version of the original Unwrap

Why? Before the new interface for streaming, neva's Iter was a simple and elegant language construct, however since v0.20.0 it has become ugly and quite unusable.

Before:


component Main(start) (stop) {
    nodes { Println<maybe<int>>, Iter<int>, Unwrap<int> }
    net {
        :start -> ($lst -> iter -> println -> unwrap)
        unwrap:none -> :stop
    }
}

After:

const lst list<int> = [50, 30, 20, 100]

component Main(start) (stop) {
    nodes { Println<stream<int>>, Iter<int>, Match<bool> }
    :start -> ($lst -> iter -> println) 
    println.last -> match:data
    true -> match:case[0] -> :stop
}

Solution The solution was upgrading unwrap to a more modern implemnation which turns that same code into this.


component Main(start) (stop) {
    nodes { Println<int>, Iter<int>,Unwrap<int>,Del}
    :start -> ($lst -> iter -> unwrap)
    unwrap:some -> println -> del
    unwrap:none -> :stop
}

Use case

Heres how easy it is to print numbers from 1 too 99, now imagine how easy it becomes to rewrite the 99 bottles example

component Main(start) (stop) {
    nodes { Println<int> Unwrap<int>,Del,Range}
        :start -> [
            (1 -> range:from),
            (100 -> range:to)
        ]
    range -> unwrap
    unwrap:some -> println -> del
    unwrap:none -> :stop
}
emil14 commented 4 months ago

I'll copy the essence from the Discord just for the history:

  1. unwrap is for maybe, not stream
  2. The need for a separate component that extracts the data from stream item is not clear because stream item is just a structure
  3. By doing this changes we hoped it will help with #575 but actually it doesn't (I've provided example with time.Sleep that proved that program can unpredictably terminate before all elements of stream were processed)
dorian3343 commented 4 months ago

What do you think about the name Extract? Although it doesn't fix #575 (it help's slightly, a temporary bandaid is better than an open wound metaphorically) it's still useful for not having the user have to manually access the data.

emil14 commented 4 months ago

After discussion on a Discord I decided to close this one. I know you've put effort into it and I appreciate it!

The main reason for closing is that we don't need a component to extract data from stream item because user normally shouldn't do it at all. That was attempt to fix termination problem but we need to go different direction.