Giter VIP home page Giter VIP logo

Comments (7)

gusty avatar gusty commented on May 22, 2024 2

#268 solves this issue in a generic way

It requires to have available a predicate (as part of the applicative abstractions) which tells whether an element is the cancelling element for the operation, called left-zero.

from fsharpplus.

gusty avatar gusty commented on May 22, 2024 1

Here you can get an idea of how the unified function would look like

from fsharpplus.

gusty avatar gusty commented on May 22, 2024

I'm not sure if is possible to define a foldr working over infinite sequences. I would rather try to write a more efficient traverse for seq using low level enumerators instead of foldr. Still I'm not sure if it would be possible to make it work with infinite sequences, but I would give it a try.

from fsharpplus.

gusty avatar gusty commented on May 22, 2024

From @mausch on April 17, 2014 22:49

Yes, such a foldr is not possible with the usual signature, so yes, the 'optimization' needs to happen in the implementation of traverse. I'm currently trying to implement such a traverse for a LazyList, then the implementation for seq would just internally use the one for LazyList.
Doing this using low-level enumerators as you say should also be possible, it just seems more complicated.

from fsharpplus.

gusty avatar gusty commented on May 22, 2024

Hopefully I'm wrong but I'm afraid that even implementing a traverse for LazyList will not be lazy enough. A poor man solution is to add specific instances, I did one that is able to run your sample code. gusty/FsControl@0496a73

from fsharpplus.

gusty avatar gusty commented on May 22, 2024

The poor man solution seems to be the way to approach this problem.
Each time an optimized or a lazy version is needed a specific instance should be added.
I would say there will be more cases like this, in which the lazyness is not the default since it will not be automatically derived, see for instance the new Delay method for workflows.

from fsharpplus.

gusty avatar gusty commented on May 22, 2024

@mausch I'm evaluating to generalize the 'poor man' solution (and make it a rich man solution).

The idea is that all instances for (infinite) sequences have many things in common and only one difference.

They can use a generic algorithm that first enumerates the sequence and store it in a buffer (an array) but at the same time it checks for a 'failure value', and if that value is present it will stop processing and use that value instead.

The only difference between all instances is precisely that 'failure value'. For options it will be None, for Choices it will be Choice2Of2, for lists it will be [] and so on.

So we can implement something like an IsFailure typeclass, which will be an invokable IsFailure which accepts a value and determines if it represents a failure, or more technically, if it's a monadic (or applicative) short-circuit value.

Besides from helping on eager evaluation of infinite structures, this Invokable might have other uses.

It might be useful for generalizing over error-like monads, very interesting now that F# 4.1 introduces another type to handle errors in a pure way.

from fsharpplus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.