Comments (6)
The idea to have a default doesn't necessarily mean an equivalent way of doing it is bad style-wise. The reason for not doing sequence=[]
is because the array is then a singleton across all calls, which leads to very weird behavior. The right way to implement this pattern, is to set sequence=None
and then have a if sequence is None
check, if so then set sequence = []
. In this particular case, I don't think you'd need to change the Sequence
code since the type error is thrown in _parse_args
and the Sequence
constructor usually isn't called by users, rather by the library itself.
Back to the question of whether you should be able to call seq()
with arguments, I can get behind the argument of mirroring set()
and list()
in returning an empty sequence. The example stephan gave also seems like a reasonable use case for this.
Given that, I'd welcome a PR that modifies the len(args) == 0
code section to instead initialize to an empty sequence. I think that is probably the only change needed, but its possible I'm missing something
from pyfunctional.
Is there a use case for this?
from pyfunctional.
Primarily for consistency, and it seems to let users avoid special-casing. Same as usage as tuple()
and set()
for empty containers. (list()
and dict()
too, although []
and {}
are generally preferred.) I happened to want an empty sequence and was surprised.
It seems strange to me to intentionally define a special case that will raise TypeError
. Conceptually, I would expect the following to work:
pattern_of_lists = [
[0, 1, 2],
[0, 1],
[0],
[],
]
for arr in pattern_of_lists:
for_conversion = seq(arr)
for_expressing_literal = seq(*arr) # why specifically TypeError for []?
for_conversion == for_expressing_literal
Also (I'm sure you know):
seq([], [], []) == [[], [], []]
seq([], []) == [[], []]
seq([]) # surprise! But sure, this is special-cased for seq(*literal_elements) convenience
seq() # also surprise! (raises)
Was there a particular edge case or other ambiguity that warrants the TypeError
?
Could (should?) make empty seq
a "singleton" if you wanted, since it's immutable.
Not common, but I've also used "empty container" as the identity object in a reduce/fold before.
from pyfunctional.
Hello friends, I thought about this for a bit today and I had an idea.
I think the easiest way might be to change the constructor in Sequence by giving a default value to the sequence parameter like so.
class Sequence(object):
def __init__(self, sequence=[], transform=None, engine=None, max_repr_items=None):
...
That said, I imagine pylint would be bothered by this.
I do have an alternative that I have used in production myself. For the code your example you provided it would look something like this
seq(pattern_of_lists) \
.map(lambda arr: (seq(arr), seq(*arr) if arr else seq([])))
This results in a sequence of tuples (for_conversion
, for_conversion_literal
to use the original example's variables) and from there you could compare them in a map or run .for_all(lambda pair: pair[0] == pair[1])
depending on your use case.
The output from the above example ends up like this:
[([0, 1, 2], [0, 1, 2]), ([0, 1], [0, 1]), ([0], [0]), ([], [])]
I liked this when I used it because it made the substitution explicit. From the library maintainer perspective it doesn't require a change which may or may not (not the maintainer of this lib so I will defer there) induce a bug down the road.
I know that isn't what was asked for but I hope that helps someone 🙂.
from pyfunctional.
Cool! I've created #161
I've followed CONTRIBUTING.md
except for the TravisCI part, as I'm a little unclear about what to do there.
from pyfunctional.
merged!
from pyfunctional.
Related Issues (20)
- pyfunctional is incompatible with multiprocess because dill HOT 7
- any kind of join with different key name HOT 1
- Could I use the index in the map function HOT 1
- Latest version has pandas/numpy as requirements HOT 2
- for_each doesn't return the original sequence but returns None HOT 8
- Unexpected behaviour on chain with .drop_right HOT 2
- Unexpected Sequence wrapping from grouped HOT 7
- Tuple argument unpacking functionality HOT 4
- lazy_parallellize having trouble with function context? HOT 3
- "peek" functionality HOT 1
- seq() converts pandas DataFrame into Sequence HOT 8
- Cache to file and auto-load from cache HOT 4
- [Question] VSCode language server doesn't see the pyfunctional package HOT 3
- Thank you! HOT 1
- More dictionary/tuple list helpers HOT 5
- the installation of the PyFunctional package has failed due to a missing item in the package archive. HOT 3
- Old link to documentation in pypi HOT 3
- module functions and variables not found HOT 5
- Async Support HOT 4
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 pyfunctional.