Chapter 7 Challenge -- my version vs the book version

The challenge is to update the earlier phone dialer to handle letters as well. It suggests that we use .unwrap from RxSwiftExt to handle nils from the convert process. I thought that was what .compactMap does?

I ended up with

  input
    .compactMap { convert($0) }
    .skipWhile {  $0 == 0 }
    .filter { $0 < 10 }
    .take(10)
    .toArray()
    .map(format)
    .map(dial)
    .subscribe( onSuccess: { number in
        print(number)
    })
    .disposed(by: disposeBag)

The “official” challenge answer in the download has

  input.asObservable()
    .map(convert)
    .unwrap()
    .skipWhile { $0 == 0 }
    .take(10)
    .toArray()
    .map(format)
    .map(dial)
    .subscribe(onSuccess: {
      print($0)
    })
    .disposed(by: disposeBag)

Both give the same result when I run them. In my quest to learn the “correct” way, the Rx way, I am wondering why the book answer is as it is and why it would be better than my way?

(Yes I get that I can dispense with the filter since the convert, nil removed, does that for me)

Thanks

This topic was automatically closed after 166 days. New replies are no longer allowed.