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.

[transducers] Transducers crash when source is an empty string

See original GitHub issue

Most transducers take a source sequence as a final argument. Since strings are iterable, so you can do this:

[...tx.filter(x => true, "abc")]
"abc"

However, if you provide an empty string, they will crash:

[...tx.filter(x => true, "")]
TypeError: tx.filter(...) is not iterable

This is obviously because the transducer functions do a truthiness test on their final argument to determine whether a source was provided:

export function map<A, B>(fn: Fn<A, B>): Transducer<A, B>;
export function map<A, B>(fn: Fn<A, B>, src: Iterable<A>): IterableIterator<B>;
export function map<A, B>(fn: Fn<A, B>, src?: Iterable<A>): any {
    return src
        ? iterator1(map(fn), src)
        : (rfn: Reducer<any, B>) => {
              const r = rfn[2];
              return compR(rfn, (acc, x: A) => r(acc, fn(x)));
          };
}

This could be corrected by replacing src with isIterable(src) (using @thi.ng/checks). This would incur the additional work of looking up Symbol.iterator, but it would seem to be more correct. I can’t think of a case where this would be a breaking change (i.e. values for src that work now but are not iterable by that test).

@postspectacular I’m willing to PR if you think this is actionable. I understand there’s a lot of branch work going on, so whatever you think best.

A workaround for the time being is to wrap the string argument in [...s].

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
postspectacularcommented, Dec 18, 2019

Thank you for the in-depth analysis, @gavinpc-mindgrub! In this case I’d opt (like I guess you do too) for consistency, even if it means breaking the current behavior for top-level strings (incl. empty ones).

So I’d say we treat top-level strings as atoms (unflattenable, just like nested strings) and update the body of flattenWith to something like:

return isIterable(src)
  ? iterator(flattenWith(fn), isString(src) ? <any>[src] : src)
  : (rfn: Reducer<any, T>) => { ... }

With that change we get:

> [...tx.flatten("")]
[ '' ]
> [...tx.flatten([""])]
[ '' ]

> [...tx.flatten("abc")]
[ 'abc' ]
> [...tx.flatten(["abc"])]
[ 'abc' ]
0reactions
postspectacularcommented, Jan 7, 2020

@gavinpc-mindgrub no pressure pls! take your time!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding a sensor to Netflow causes it to crash
If I try to add that sensor again, the bad entry returns and Netflow crashes again. It seems like that GUID-like string is...
Read more >
The operation <= cannot accept the arguments
Hello, I am trying to make an Android app using MIT App Inventor to read sensor data from and control the onboard LED...
Read more >
Diagnosing Native Crashes | Android Open Source Project
You can reproduce an instance of this type of crash using crasher strlen-NULL . Low-address null pointer dereference. In many cases the fault ......
Read more >
java.lang.IllegalArgumentException: Given String is empty or ...
the object is outisde of the onCreate() Method so it has to use final. package naufal.com.tugasakhir; import android.content.Intent; import android ...
Read more >
Resolved issues - Trellix Product Documentation
NSPMGR-13397, The Quarantine and Alert state Sensor response actions ... The error message TS lookup failed: IP address string cannot be NULL/empty String ......
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