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

feat(array): add support to pipe into array.from #5165

Merged
merged 1 commit into from
Sep 9, 2022

Conversation

nathanielc
Copy link
Contributor

With this change you can now pipe the rows argument into the
array.from function.

Checklist

Dear Author 👋, the following checks should be completed (or explicitly dismissed) before merging.

  • ✏️ Write a PR description, regardless of triviality, to include the value of this PR
  • 🔗 Reference related issues
  • 🏃 Test cases are included to exercise the new code
  • 🧪 If new packages are being introduced to stdlib, link to Working Group discussion notes and ensure it lands under experimental/
  • 📖 If language features are changing, ensure docs/Spec.md has been updated

Dear Reviewer(s) 👋, you are responsible (among others) for ensuring the completeness and quality of the above before approval.

@nathanielc nathanielc requested review from a team as code owners September 2, 2022 21:27
@nathanielc nathanielc requested review from jsternberg and lwandzura and removed request for a team September 2, 2022 21:27
@@ -45,7 +45,7 @@ package array
//
// ## Metadata
// tags: inputs
builtin from : (rows: [A]) => stream[A] where A: Record
builtin from : (<-rows: [A]) => stream[A] where A: Record
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'm the biggest fan of this. I can't really think of another example where we do this kind of thing. I saw the PR and thought I'd like it, but the test cases are a bit hard for me to read.

Is there a specific request asking for this or is this just a quality of life change? I'd like to get some opinions from others on the team if they feel it looks right before clicking approve. If others like it, I've got no problem with hitting approve from a functional standpoint.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsternberg This is coming from me as I prepare content for the rollout of dynamic types. It just removes the need to assign an array to a variable to render it as a table when parsing JSON using the dynamic package:

import "array"
import "experimental/dynamic"

jsonData = bytes(v: "
    {
    \"items\":  [
        {\"name\": \"a\", \"pos\": {\"x\": 10, \"y\": 10, \"z\": 20}},
        {\"name\": \"b\", \"pos\": {\"x\": 30, \"y\": 99}},
        {\"name\": \"c\", \"pos\": {}},
        {\"name\": \"d\"}
        ]
    }
")

parsed = dynamic.parseJSON(data: jsonData)

parsed.items
    |> dynamic.asArray()
    |> array.map(
        fn: (x) => (
            {
                name: string(v: x.name),
                posX: int(v: x.pos.x),
                posY: int(v: x.pos.y)
            }
        )
    )
    |> array.from()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sure. I'll just say I discourage people from using it this way outside of this context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes with the json use case its a bit awkward when every other function has a pipeforward argument.

For example:

dynamic.parseJSON(...)
    |> dynamic.asArray()
    |> array.filter(fn: (x) => x.bar == 42)
    |> array.map(fn: (x) => ({foo: x.foo}))
    |> array.from()
    |> to()
    

Without this change users must do:

rows = dynamic.parseJSON(...)
    |> dynamic.asArray()
    |> array.filter(fn: (x) => x.bar == 42)
    |> array.map(fn: (x) => ({foo: x.foo}))
    
array.from(rows: rows)
    |> to()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What @sanderson said. My github hadn't refreshed with the above comments before writing my reply.

Copy link
Contributor

@sanderson sanderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one additional update. Need to add this to the parameter description:

Default is the piped-forward array (`<-`).

With this change you can now pipe the `rows` argument into the
array.from function.
@nathanielc nathanielc merged commit b8a1e11 into master Sep 9, 2022
@nathanielc nathanielc deleted the feat/array-from-pipe branch September 9, 2022 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants