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.

Confused about using Try in an enumerable

See original GitHub issue

OK, bear with me with this silly example, but I’m still trying to get my head around all this stuff.

Suppose I have the following (admittedly stupid) situation: I have a Func<int, float> that might throw an exception, for example…

Func<int, float> f = n => 1 / n;

Obviously, if you pass zero into this function it will throw an exception. Suppose I also have an Enumerable<int> and want to apply the Func to it…

var result = nums.Select(n => f(n));

I guess I need to use Try to handle the fact that f might throw an exception…

var result = nums.Select(n => Try(() => f(n));

This gives me an IEnumerable<Try<float>>. What do I do with this now? Let’s say I have two different requirements (probably not at the same time)…

  1. End up with an IEnumerable<float> that includes only the results that didn’t throw an exception
  2. End up with (something) that includes the exceptions. I guess this would be an IEnumerable<Exception> but am not sure

Anyone able to explain how I would achieve each of these goals? Thanks

Again, please forgive the silly examples, but I find playing around like this helps me understand what’s going on.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
colethecodercommented, Nov 25, 2018

In C# a double divided by zero is Infinity, so you wouldn’t get any exceptions, it’s nothing to do with LanguageExt’s Try.

If you go back to your original function n => 1 / n you should see the exceptions.

You can also avoid the ToEither bit in the example as there are extensions to support IEnumerable<Try<T>> so you can simply do:

IEnumerable<Try<int>> result = Range(0, 10)
                .Map(n => Try(() => 1 / n));
IEnumerable<int> r1 = result.Succs();
IEnumerable<Exception> r2 = result.Fails();
0reactions
MrYossucommented, Nov 25, 2018

@colethecoder Thanks, that’s excellent!

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - What return type do I mention in IEnumerable<T> in try ...
If i understand your question you are wondering what to return from your catch block? I'd probably throw the exception to the calling...
Read more >
Using a try-finally (without catch) vs enum-state validation
My dilemma on the best practice is whether one should use a try/catch/finally to return an enum (or an int that represents a...
Read more >
C#: IEnumerable, yield return, and lazy evaluation
An iterator is a method that uses the yield return keywords. yield return is ... First() to try to find a specific item...
Read more >
Foreach and IEnumerable
I've been reading a lot about foreach and the iterator pattern lately. During my research, I came up with these questions: How is...
Read more >
Could somebody point me to a good tutorial that explains ...
I *think* I'm starting to understand IEnumerator but I'm still confused about how to use Yield correctly. I've looked over several different ...
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