Comments (7)
#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.
Here you can get an idea of how the unified function would look like
from fsharpplus.
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.
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.
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.
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.
@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)
- `(>>=)` should be constrained with `Map` HOT 8
- New Validation type HOT 2
- Array.replace takes in seq for its two first values instead of arrays
- Adjust package info with icon and license elements
- Add more static member operators like >=> HOT 1
- `String.take 0 s` throws IndexOutOfRangeException HOT 2
- Traverse Lens Documentation Needs more Examples HOT 1
- Documentation table of contents is missing list of namespaces HOT 3
- Proposal: A set of modules for list and tree zippers HOT 1
- Upload packages built on main branch onto GitHub NuGet feed
- Int32.TryParse and F#+ tryParse give different results HOT 6
- Fable 4 support HOT 3
- Type inference issue with applicative2 HOT 1
- [API Proposal] String.(try)FindLastSliceIndex HOT 3
- Clean up preprocessor directives
- Drop net45, net6, Fable3 from F#+ 2 release
- net45 not compatible with updated F# typeprovider
- Naming convention for (non-generic) Sequence-like overloaded methods
- Adjust async/task/value task tests to work on net6 and on release branch
- Cannot destructure ask when using ReaderT HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fsharpplus.