Flux.map vs Flux.flatMap for a 1-to-1 operation - project-reactor

I keep seeing examples that use flatMap for a 1-to-1 operation, like:
Flux.just("a", "b", "c")
.flatMap(s -> Mono.just(s.toUpperCase())
when I would have expected
Flux.just("a", "b", "c")
.map(String::toUpperCase)
(Note: I know I didn't add a subscriber; assume I print them or something)
The use of flatMap here is to flatten the returned Mono, right? But why not just use the map operation as shown? Is it just because the map operation is synchronous? What use case am I missing?

I don't think you are missing any. According to the documentation, flatMap is used when you require some asynch work to be done within it.
So the operation you would use here is simple map, since all you need is turn one object into another (lower case into upper case).
From what I noticed, if you want to turn one object into another map is enough. If you want extra asynch work done go with flatMap.

Related

Use of extension functions in streaming mode in Saxon/Net

I would like to know whether it is possible to call user extension functions in streaming mode using XSLT 3.0 on Saxon EE for .Net.
And if it is, under what constraints (e.g. "never pass nodes as parameters but atomic values are OK", etc.)
I have looked at the main documentation but could not find anything.
I don't think it's been tested, so I suggest you try it and see. The streamability analysis uses the general streamability rules, so if you pass atomic values (or unstreamed nodes) then it should be OK. You might be able to get away with passing nodes as well, provided you don't try doing any downwards navigation from them. If you pass a sequence of nodes rather than a single node then they may get buffered.

What is a realistic example for the use of mapc?

I was just thinking about the different mapping functions in common-lisp as described in the hyperspec. I am pretty much used to mapcar and think it is the easiest to understand. But what is a real world example of using mapc? The example in the hyperspec uses it for a side-effect as far as I get it. But why does it return the list argument?
Is there a general rule when such a mapping is favourable over an iteration using loop etc.?
What is a real world example of using mapc?
(mapc #'print my-list) is clearer than (dolist (x my-list) (print x))
Why does it return the list argument?
The functional heritage imposes the thinking that every function should return something useful; for mapc it is the original list.
I think mapc returns its list argument for the same reason print does - to simplify debugging by sprinkling your code with output. E.g., suppose you do something like
(mapcar #'important-processing
list-with-weird-elements)
You want to see what's inside the list while preserving the logic:
(mapcar #'important-processing
(mapc #'show-weird-object list-with-weird-elements))
Also, a lot of things in CL are for "hysterical reasons".
Is there a general rule when such a mapping is favourable over an iteration using loop etc.?
Only if you already have a function which does what you need, like print above.

optimizing dask Series filtering - lazy version of Series.isin()

I currently have the following pattern embedded inside a larger computation
seq1.isin(seq2[seq3].unique().compute().values)
where seq3 is a boolean Series.
The performance seems acceptable, but it is ugly and the use of compute() forces evaluation, possibly removing opportunities for parallelism.
Simply saying
seq1.isin(seq2[seq3].unique())
does not work and the documentation says that the argument to isin must be an (I presume Numpy) array.
Is there a bettern way to write the above code?
What if seq1 and seq2 are the same?
I don't think it's possible do an incremental set membership operation. To get a correct result, you'd need to have a fully realized set to answer the question of whether an item is a member of it or not.
You could probably achieve this operation using an inner join.

Is it safe to modify a data structure within a for in loop?

I would like to loop through my collection using a for-in loop, but read it's unsafe to modify the content of my data structure (which would be either a dictionary or an array) within such loop. Is that correct? Could it have any side effects? Should I use a for loop with index instead? Thank you.
As long as you don't add or delete objects, you are OK. That is, if an object in the collection is a mutable object, and you change something about that, its fine. Its only when you go deleting, adding or replacing objects to the collection that you get into trouble.
The simple answer is, yes, in theory it can have side effects. It depends on what you're doing in the loop. Without more information, I'd say to sit down with a pencil and paper and make a reasoned, mathematical argument (induction optional but recommended) to show that this process will never gunk up your data.
If you're modifying the contents of your array (if it's a pixel array, changing colors, for example), you're likely to not run into too much trouble if you plan your code well. If you're adding, deleting, or moving elements, proceed with caution.

What is the difference between a monad and a closure?

i am kinda confused reading the definition between the two. Can they actually intersect in terms of definition? or am i completely lost? Thanks.
Closures, as the word tends to be used, are just functions (or blocks of code, if you like) that you can treat like a piece of data and pass to other functions, etc. (the "closed" bit is that wherever you eventually call it, it behaves just as it would if you called it where it was originally defined). A monad is (roughly) more like a context in which functions can be chained together sequentially, and controls how data is passed from one function to the next.
They're quite different, although monads will often use closures to capture logic.
Personally I would try to get solid on the definition of closures (essentially a piece of logic which also captures its environment, i.e. local variables etc) before worrying about monads. They can come later :)
There are various questions about closures on Stack Overflow - the best one to help you will depend on what platform you're working on. For instance, there's:
What are closures in .NET?
Function pointers, closures and lambda
Personally I'm only just beginning to "grok" monads (thanks to the book I'm helping out on). One day I'll get round to writing an article about them, when I feel I understand them well enough :)
A "closure" is an object comprising 1) a function, and 2) the values of its free variables where it's constructed.
A "monad" is a class of functions that can be composed in a certain way, i.e. by using associated bind and return higher-order function operators, to produce other functions.
I think monads are a little more complicated than closures because closures are just blocks of code that remember something from the point of their definitions and monads are a construct for "twisting" the usual function composition operation.

Resources