Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Why not use string-based solution on Armstrong's number, Clojure track? #1942

Open
Javran opened this issue Mar 20, 2021 · 2 comments
Labels
track/clojure Clojure track type/mentor-notes Mentor notes

Comments

@Javran
Copy link
Contributor

Javran commented Mar 20, 2021

The mentoring note says:

Most users seem to split the number into digits by iterating a string and parsing characters. Suggest that this can be done arithmetically as well.

But compare this:

(defn num->digits [n]
  (->> n
       (iterate #(quot % 10))
       (take-while pos?)
       (map #(rem % 10))))

(defn num->digits-rev [n]
  (map
    #(Character/digit % 10)
   (str n)))

I can't look at this and say num->digits should be preferred in any way:

  • Readability speaking IMO the latter is simpler thus more readable, I know the use of ->> is probably more idiomatic in Clojure, but if that means writing more compliated code, does it worth it?
  • Operationally the former takes more step, one has to think about stream manipulations rather than just break that apart with str and work on each individual pieces.
  • In addition, the backbone data structure is lazy sequence vs. String - to be fair I don't know the details about lazy sequence but my Haskell mental model is telling me to expect little to none allocation (assuming this sequence is then immediately followed by exponentiation and consumed by summation) vs. String-based char buffer allocation. But on the other hand the lazy sequence impose a data dependency as to produce a digit will require knowing all previous results but there's nothing preventing String-based to have some parallelism after str is done - plus toString() attempts to breakdown by 100 (see getChars()) rather than 10 - I'd say using str rather than breaking the number down by 10 manually is a performance gain.

In fact I believe in the opposite - the conciseness of string-based approach in comparison to ->> just proved string-based works better for this particular case.

@angelikatyborska
Copy link
Contributor

@exercism/clojure

@bobbicodes
Copy link
Member

bobbicodes commented Jun 28, 2021

Yeah this could come across as useful advice or utter snobbery depending on the situation.

I personally remember being rather blown away the day that someone showed me you could split a number arithmetically, without entering the world of strings. This is often people's first encounter with Clojure, and the intention is to encourage them to think more functionally.

As a mentor there is an implicit expectation to offer something of value, so it is useful to have some talking points, so I've used this one many times but only after approving the solution already so as not to block their progress. I sure hope that people are not being told that their string-based solution is wrong.

It might be good to make this more clear in the mentoring note. I appreciate the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
track/clojure Clojure track type/mentor-notes Mentor notes
Projects
None yet
Development

No branches or pull requests

3 participants