Op 8-9-2020 om 04:22 schreef Richard
O'Keefe:
Thanks for letting me see this. But still I wonder if this is really the OOP way and if that function does more then 1 thing. It looks to me that its iterating trrough the string. Calculating the crc and checking it. I learned that it is a good thing that a function and a class does only 1 thing. Roelof |
Hi Richard and Roelof, thanks for your comprehensive answer. I brought up Regex only to point out alternative solutions. Another one is the following using transducers, where Tee works like the tee command from the command line. IsbnValidator>>isValidIsbn: aString | length countChars separators getSeparators lastChar getLastChar filterDigits computeCheckSum checkSum | "Count number of characters" length := 0. countChars := [:count :char | length := length + 1] init: length. "Get non-digit characters" separators := Set new getSeparators := separators <~ #isDigit remove. "Get last character" lastChar := nil. getLastChar := [:prev :char | lastChar := char ] init: lastChar. "Get digits" filterDigits := #isDigit filter. "Calculate check sum" computeCheckSum := ([:sum :index :digit | sum + index * digit value] init: 0) completing: #\\ . "Compute" checkSum := aString transduce: (Tee to: countChars) * (Tee to: getSeparators) * (Tee to: getLastChar) * filterDigits reduce: computeCheckSum init: 0. "Check validity" ^((length = 10 or: [length = 13]) and: [separators = Set with: $-]) and: [checkSum = (lastChar = $X ifTrue: [10] ifFalse: [lastChar value])] Kind regards, Steffen Am .09.2020, 08:30 Uhr, schrieb Roelof Wobben via Pharo-users <[hidden email]>:
|
In reply to this post by Pharo Smalltalk Users mailing list
Op 8-9-2020 om 08:30 schreef Roelof
Wobben:
I learned on other oop languages that for mainibility it's needed that a class does one thing and a method does also one thing. So when software changes , you can easily make the changes. Its I think called SOLID and I like that idea. So I try to make that also work in Pharo but it seems not so important. It's look like me that getting it work is more important. Roelof |
Administrator
|
On Tue, Sep 8, 2020, 04:35 Roelof Wobben via Pharo-users <[hidden email]> wrote:
I don't think that is the case here. Identify the objects involved in an ISBN string. I see String and Character. Once parsed, you could have an ISBN itself, with the elements cited by Richard O'keefe. But the exercise isn't about that, but only about the validation. There is very little behaviour that one could add to String and Character that would help in this exercise and that would be appropriate to their respective roles. Arguably, you could solve this exercise by creating a parser. I think that would be a lot like using a 10 pound sledge hammer to install a thumbtack.
|
Free forum by Nabble | Edit this page |