Reduce<T> returns T instead of Option<T>
See original GitHub issueConsider this reduce
method:
public static T reduce<T>(IEnumerable<T> list, Func<T, T, T> reducer) =>
match(headOrNone(list),
Some: x => fold(tail(list), x, reducer),
None: () => failwith<T>("Input list was empty")
);
I was surprised to see that it throws an exception when list
is empty.
Wouldn’t the functional approach be to have the return type be Option<T>
and return None
when list
is empty?
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (13 by maintainers)
Top Results From Across the Web
OptionT
OptionT can be more convenient to work with than using F[Option[A]] directly. Reduce map boilerplate. Consider the following scenario: import scala.concurrent ...
Read more >Reducing Two Option[T]'s in Scala -
Option[T ] is a container which either has a value ( Some ), or doesn't have a value ( None ). It is...
Read more >Scala Standard Library 2.13.11 - scala.Option
Represents optional values. Instances of Option are either an instance of scala.Some or the object None .
Read more >Query Hints (Transact-SQL) - SQL Server
Query hints specify that the indicated hints are used in the scope of a query. They affect all operators in the statement.
Read more >pandas.DataFrame.apply — pandas 2.0.3 documentation
'reduce' : returns a Series if possible rather than expanding list-like results. This is the opposite of 'expand'. 'broadcast' : results will be...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have added
reduceOrNone
andreduceBackOrNone
I find it interesting that Java is more functional (in my opinion) in this regard than language-ext, F#, or even Haskell.
https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#reduce-java.util.function.BinaryOperator-