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

Provide generic manipulation methods for enumerable objects #8

Open
zero-plusplus opened this issue Sep 8, 2021 · 0 comments
Open
Labels
draft Draft of new features

Comments

@zero-plusplus
Copy link
Owner

zero-plusplus commented Sep 8, 2021

Reference sources:


In v1, there was one basic object and it had the same interface.
In v2, those interfaces were divided into Object, Array and Map.

The problem with this is when dealing with for statements. The following is a general usage of the for statement for each object in v2.

; Object
obj := { key: "value", key2: "value2" }
for key in obj.ownProps() {
  value = obj.%key%
}
; Map
for key, value in Map("key", "value", "key2", "value2") {
}
; Array
for i, value in [ "value", "value2" ] {
}

Only the objects take a bit of work to enumerate. However, there are two things that will be retrieved in the end: the key (or index) and the value.
This library provides a way to handle the same interface regardless of the object type as follows.

bee.Enumerable.values({ key: "value", key2: "value2" )       ; => [ "value", "value2" ]
bee.Enumerable.values(Map("key", "value", "key2", "value2")) ; => [ "value", "value2" ]
bee.Enumerable.values([ "value", "value2" ])                 ; => [ "value", "value2" ]
for key, value in bee.Enumerable({ key: "value", key2: "value2" ) {
}
for key, value in bee.Enumerable(Map("key", "value", "key2", "value2")) {
}
for i, value in bee.Enumerable([ "value", "value2" ]) {
}

Enumerable will be evaluated lazily. In other words, when the method of an Enumerable is called, as follows, only what is to be done is defined, and only when the enumeration is started using a for statement, etc., are they executed.

; It is not executed in the following stages. In other words, it does not take long even for huge objects.
enumerable = bee.Enumerable({ key: "value", key2: "value2" })
  .values(obj)
  .select(value) => value == "value2")

; The process starts when you actually start the enumeration as follows. If it is a huge object, it will take some time.
enumerable.toArray()
for key, value in enumerable {
}
@zero-plusplus zero-plusplus added the draft Draft of new features label Sep 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
draft Draft of new features
Projects
None yet
Development

No branches or pull requests

1 participant