Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find component #647

Open
emil14 opened this issue May 20, 2024 · 1 comment
Open

Find component #647

emil14 opened this issue May 20, 2024 · 1 comment

Comments

@emil14
Copy link
Collaborator

emil14 commented May 20, 2024

Almost same as #289 except

  1. Emits single elements instead of stream(s)
  2. Emits result as soon as first element found and skips other elements

API

pub interface IFindHandler<T>(data T) (res bool)

pub component Find<T>(data stream<T>) (res T, miss any)) {
  nodes { handler IFindHandler<T> }
  // ...
}

Example usage

type Obj struct {
    id int
    name string
}

const objs list<Obj> = [
    { id: 1, name: "a" }
    { id: 2, name: "b" }
    { id: 3, name: "c" }
    { id: 42, name: "You found me!" }
    { id: 4, name: "d" }
]

component Main(start) (stop) {
    nodes {
        Find<Obj>{IsID42},
        Println
    }
    :start -> ($objs -> find)
    find:res -> println
    find:miss -> ("not found :(" -> println)
    println -> :stop
}

component IsID42(data Obj) (res bool) {
    nodes { Eq }
    :data.id -> eq:actual
    42 -> eq:expected
    eq -> :res
}

Output should be "You found me!"

@emil14 emil14 changed the title Find for steam Find component May 20, 2024
@emil14
Copy link
Collaborator Author

emil14 commented May 20, 2024

[Idea] Add idx to API

type FindResult<T> struct {
  idx int
  data T
}

Find<T>(data stream<T>) (res FindResult, miss any)

Alternative Idx component

Alternative could be having Idx or Index component for cases where you need index.

However, if you need both idx and data you'll have to chain it with some "Get" component.

Problem is we currently only have lists.Get (I think?) and to use it with Idx one have to pack stream into list which kinda defats the purpose of the stream

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant