You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "He-Pin (via GitHub)" <gi...@apache.org> on 2024/03/31 13:09:46 UTC

[PR] chore: Add Source.iterate operator. [pekko]

He-Pin opened a new pull request, #1244:
URL: https://github.com/apache/pekko/pull/1244

   Motivation:
   refs: https://github.com/apache/pekko/issues/1189
   
   Result:
   Source.iterate operator added.
   
   Note:
   Because there is an overloading issue in Scala 2, so missing an overloaded method in Scala dsl.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] chore: Add Source.iterate operator. [pekko]

Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on code in PR #1244:
URL: https://github.com/apache/pekko/pull/1244#discussion_r1545921497


##########
docs/src/test/scala/docs/stream/operators/source/Iterate.scala:
##########
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package docs.stream.operators.source
+
+import org.apache.pekko
+import pekko.NotUsed
+import pekko.stream.scaladsl.Source
+
+object Iterate {
+
+  // #countTo
+  def countTo(n: Long): Source[Long, NotUsed] = Source
+    .iterate(1L)(_ => true, _ + 1)

Review Comment:
   Let's keep it as it for now, which seems simpler.
   And user can treat it as ` .iterate(1L)(_ + 1)`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] chore: Add Source.iterate operator. [pekko]

Posted by "laglangyue (via GitHub)" <gi...@apache.org>.
laglangyue commented on PR #1244:
URL: https://github.com/apache/pekko/pull/1244#issuecomment-2028762970

   LGTM


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] chore: Add Source.iterate operator. [pekko]

Posted by "Roiocam (via GitHub)" <gi...@apache.org>.
Roiocam commented on code in PR #1244:
URL: https://github.com/apache/pekko/pull/1244#discussion_r1545657467


##########
stream/src/main/scala/org/apache/pekko/stream/scaladsl/Source.scala:
##########
@@ -17,15 +17,15 @@ import java.util.concurrent.CompletionStage
 
 import scala.annotation.{ nowarn, tailrec }
 import scala.annotation.unchecked.uncheckedVariance
-import scala.collection.immutable
+import scala.collection.{ immutable, AbstractIterator }
 import scala.concurrent.{ Future, Promise }
 import scala.concurrent.duration.FiniteDuration
 
 import org.apache.pekko
 import pekko.{ Done, NotUsed }
 import pekko.actor.{ ActorRef, Cancellable }
 import pekko.annotation.InternalApi
-import pekko.stream.{ Outlet, SourceShape, _ }
+import pekko.stream._
 import pekko.stream.impl.{ PublisherSource, _ }

Review Comment:
   ```suggestion
   import pekko.stream.impl._
   ```



##########
stream/src/main/scala/org/apache/pekko/stream/javadsl/Source.scala:
##########
@@ -275,6 +275,25 @@ object Source {
   def unfoldAsync[S, E](s: S, f: function.Function[S, CompletionStage[Optional[Pair[S, E]]]]): Source[E, NotUsed] =
     new Source(scaladsl.Source.fromGraph(new UnfoldAsyncJava[S, E](s, f)))
 
+  /**
+   * Creates a sequential `Source` by iterating with the given predicate and function,
+   * starting with the given `seed` value. If the predicate returns `false` for the seed,
+   * the `Source` completes with empty.
+   *
+   * @see [[unfold]]
+   */
+  def iterate[T](seed: T, p: function.Predicate[T], f: function.Function[T, T]): Source[T, NotUsed] =
+    new Source(scaladsl.Source.iterate(seed)(elem => p.test(elem), elem => f(elem)))
+
+  /**
+   * Creates an infinite sequential `Source` by iterating with the given function,
+   * starting with the given `seed` value.
+   *
+   * @see [[unfold]]
+   */
+  def iterate[T](seed: T, f: function.Function[T, T]): Source[T, NotUsed] =
+    new Source(scaladsl.Source.iterate(seed)(ConstantFun.anyToTrue, elem => f(elem)))

Review Comment:
   Should we mention in the document that Java has an API that does not require a predicate?



##########
docs/src/test/java/jdocs/stream/operators/source/Iterate.java:
##########
@@ -0,0 +1,13 @@
+package jdocs.stream.operators.source;
+
+import org.apache.pekko.NotUsed;
+import org.apache.pekko.stream.javadsl.Source;
+
+interface Iterate {
+
+  // #countTo
+  static Source<Long, NotUsed> countTo(long n) {
+    return Source.iterate(1L, i -> i + 1).take(n);

Review Comment:
   For comparison, it may be more friendly to use the same API as ScalaDsl.
   
   ```suggestion
       return Source.iterate(1L, i -> true, i -> i + 1).take(n);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] chore: Add Source.iterate operator. [pekko]

Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on code in PR #1244:
URL: https://github.com/apache/pekko/pull/1244#discussion_r1545921497


##########
docs/src/test/scala/docs/stream/operators/source/Iterate.scala:
##########
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package docs.stream.operators.source
+
+import org.apache.pekko
+import pekko.NotUsed
+import pekko.stream.scaladsl.Source
+
+object Iterate {
+
+  // #countTo
+  def countTo(n: Long): Source[Long, NotUsed] = Source
+    .iterate(1L)(_ => true, _ + 1)

Review Comment:
   Let's keep it as it for now, which seems simpler.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] chore: Add Source.iterate operator. [pekko]

Posted by "laglangyue (via GitHub)" <gi...@apache.org>.
laglangyue commented on code in PR #1244:
URL: https://github.com/apache/pekko/pull/1244#discussion_r1545690594


##########
docs/src/test/scala/docs/stream/operators/source/Iterate.scala:
##########
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package docs.stream.operators.source
+
+import org.apache.pekko
+import pekko.NotUsed
+import pekko.stream.scaladsl.Source
+
+object Iterate {
+
+  // #countTo
+  def countTo(n: Long): Source[Long, NotUsed] = Source
+    .iterate(1L)(_ => true, _ + 1)

Review Comment:
   how about `(_ < n, _ + 1)`?
   This show the usage of the predication function
   and don't need the take



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] chore: Add Source.iterate operator. [pekko]

Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin merged PR #1244:
URL: https://github.com/apache/pekko/pull/1244


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org