question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reduce<T> returns T instead of Option<T>

See original GitHub issue

Consider 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:closed
  • Created 4 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
louthycommented, Apr 24, 2019

I have added reduceOrNone and reduceBackOrNone

0reactions
TysonMNcommented, Aug 25, 2021

Here is the equivalent in Haskell:

http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Data.Foldable.html#foldl1

Here is the equivalent in F#:

https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/list.reduce['t]-function-[fsharp]

Both throw exceptions.

I find it interesting that Java is more functional (in my opinion) in this regard than language-ext, F#, or even Haskell.

public interface Stream<T> {
  Optional<T> reduce(BinaryOperator<T> accumulator);
}

https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#reduce-java.util.function.BinaryOperator-

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found