Programming in Scala Forum
Functional Objects Chapter : Rational Numbers

14 replies on 1 page. Most recent reply: Sep 19, 2012 1:56 PM by Dale King

 Previous Topic Next Topic
 Flat View: This topic has 14 replies on 1 page
 Rob Lally Posts: 7 Nickname: roblally Registered: Dec, 2007
Functional Objects Chapter : Rational Numbers Posted: Dec 19, 2007 8:05 AM
I've been enjoying the book but I have to say I was disappointed by Chapter 6 : Functional Objects.

The chapter was well written but, in a book that is trying to explain the value of functional programming to those of us who've largely written it off as an interesting intellectual exercise best left to academia, using rational numbers as an example does nothing but reinforce the idea that it has little, if any, relevance to the vast majority of developers working in commercial environments.

I'm pretty sure that I'm approaching Scala with a much more open mind than most - I'm reading a pre published version of the book after all - and even my heart sank when I came across this.

Is it too late in the cycle to change this to something that might speak better to the "Coming from Java" reader that, I think, will be the majority of your purchasers?

 Rob Lally Posts: 7 Nickname: roblally Registered: Dec, 2007
Re: Functional Objects Chapter : Rational Numbers Posted: Dec 19, 2007 1:29 PM
I know it is considered terrible form to reply to your own post but, in the spirit of identifying solutions not problems ...

As an alternative perhaps you could consider the classic arithemtic with money in multiple currencies example - it would let you cover most of the salient points in the chapter, perhaps all of them.

 Toby Donaldson Posts: 8 Nickname: tjd Registered: Jun, 2003
Re: Functional Objects Chapter : Rational Numbers Posted: Dec 20, 2007 3:59 AM
The Rational example didn't bother me except for one detail: it allows for 0 in the denominator!

 Matt Russell Posts: 1 Nickname: mattr Registered: Dec, 2007
Re: Functional Objects Chapter : Rational Numbers Posted: Dec 22, 2007 9:10 AM
I don't agree with the premise that using fractions as an example will make people conclude that Scala is an academic-only language with no relevance for real-world programming. Granted that fractions don't crop up much in commercial development, but surely the need here is just for a toy example to demonstrate some language features, and the only requirement is that it's something people are familiar with? I think if you used an example such as arithmetic with money in multiple currencies, you'd end up obscuring the points about Scala's features with the complexities of the problem.

 Bill Venners Posts: 2284 Nickname: bv Registered: Jan, 2002
Re: Functional Objects Chapter : Rational Numbers Posted: Dec 24, 2007 7:32 PM
> I know it is considered terrible form to reply to your own
> post but, in the spirit of identifying solutions not
> problems ...
>
> As an alternative perhaps you could consider the classic
> arithemtic with money in multiple currencies example - it
> would let you cover most of the salient points in the
> chapter, perhaps all of them.

Hi Rob. Thanks for the feedback. There is already a currency example. It is way back in chapter 18, starting on page 337 of PrePrint version 1. It's pretty cool stuff.

I've been very sensitive about just the issue you brought up, that having traditional functional programming examples of how to calculate square roots and such will turn off mainstream programmers, because they'll feel like they are taking a CS class. But I did feel sprinkling a few such examples here and there might be nice. I do value such feedback because there is still time to improve the examples.

 Howard Lovatt Posts: 321 Nickname: hlovatt Registered: Mar, 2003
Re: Functional Objects Chapter : Rational Numbers Posted: Jan 10, 2008 2:16 AM
The thing that bothered me about the rational example is that each number has 4 fields, n, d, numer, and denom. At least I think it does? Now you want to be able to reduce the fraction so you need numer and denom so that gcd can be used. Once you called gcd however, you no longer need n and d. How can you get rid of them? Have I missd something?

 Silvestre Zabala Posts: 5 Nickname: silvestre Registered: Dec, 2007
Re: Functional Objects Chapter : Rational Numbers Posted: Jan 10, 2008 5:11 PM
> Have I missd something?

Yes only numer and denom are fields. See Section 9.7.

 Howard Lovatt Posts: 321 Nickname: hlovatt Registered: Mar, 2003
Re: Functional Objects Chapter : Rational Numbers Posted: Jan 10, 2008 9:09 PM
Thanks for your reply - very helpful. The thing that confused me was the greeting example on page 69. Here the constructor argument, greeting, is used as a field, so I guess the example has a typo and they forgot to put in the val.

 Silvestre Zabala Posts: 5 Nickname: silvestre Registered: Dec, 2007
Re: Functional Objects Chapter : Rational Numbers Posted: Jan 11, 2008 12:13 PM
> The thing that
> confused me was the greeting example on page 69. Here the
> constructor argument, greeting, is used as a field, so I
> guess the example has a typo and they forgot to put in the
> val.

No, that example is correct, and greeting is not a field. However contructor arguments are in scope in the whole body of the class definition.

 Howard Lovatt Posts: 321 Nickname: hlovatt Registered: Mar, 2003
Re: Functional Objects Chapter : Rational Numbers Posted: Jan 11, 2008 2:53 PM
How can they be in scope and have a value if they aren't a field? EG:

```class WorldlyGreeter(greeting: String) {
def greet() = {
val worldlyGreeting = WorldlyGreeter.worldify(greeting)
println(worldlyGreeting)
}
}

val wg = new WorldlyGreeter( "Hello" )
wg.greet()
```

If greeting isn't a field then how does greet know that greeting contains Hello; greet is executed after we have left the constructor and each instance of WorldlyGreeter has a different greeting.

 Silvestre Zabala Posts: 5 Nickname: silvestre Registered: Dec, 2007
Re: Functional Objects Chapter : Rational Numbers Posted: Jan 12, 2008 6:08 AM
> If greeting isn't a field then how does greet know that
> greeting contains Hello; greet is executed after we have
> left the constructor and each instance of WorldlyGreeter
> has a different greeting.

It's up to the implementation to decide how to do that.

scalac uses a private final instance variable if it needs to. However:
```class Foo(bar : String){ val baz = bar } // There is no bar in the resulting class file
```

 Howard Lovatt Posts: 321 Nickname: hlovatt Registered: Mar, 2003
Re: Functional Objects Chapter : Rational Numbers Posted: Jan 13, 2008 4:11 AM
> It's up to the implementation to decide how to do that.
>
> scalac uses a private final instance variable if it needs
> to. However:
>
```> class Foo(bar : String){ val baz = bar } // There is no
> bar in the resulting class file
>
```

Thanks for explaining that the compiler infers if a field is needed or not, I hadn't read that into the examples at all.

However I am unconvinced about this technique of inferring if you need a field or not. Sure the compiler can do it, but a person without some considerable effort couldn't work this out for anything other than the simplest class.

 Eric Willigers Posts: 8 Nickname: ewilligers Registered: Dec, 2007
Re: Functional Objects Chapter : Rational Numbers Posted: Jan 29, 2008 8:50 PM
> > Have I missd something?
>
> Yes only numer and denom are fields. See Section 9.7.

Using javap I see that n and d do not become fields, but g does. This is avoidable using

val (numer, denom) = {
val g = gcd(n, d)
(n / g, d / g)
}

But this demands much more of the reader.

 Dale King Posts: 3 Nickname: daleking Registered: Dec, 2003
Re: Functional Objects Chapter : Rational Numbers Posted: Sep 19, 2012 1:46 PM
I have one slight nitpick about the Rational example. It doesn't do a good job handling negative denominators in a "rational" way.

For example:

println(new Rational(1,3) - new Rational(1,-3))
println(new Rational(1,3) - new Rational(-1,3))

which are the same value mathematically, but the output is:

-2/-3
2/3

The code should have one way to handle the sign. Most people want to see any negative sign in the numerator, not the denominator.

The easy fix is to change to these two lines:

val numer = d.signum * n / g
val denom = d.abs / g

This code snippet did not override the == operator, but being consistent with the sign makes implementation of one trivial (just compare numerators and denominators).

 Dale King Posts: 3 Nickname: daleking Registered: Dec, 2003
Re: Functional Objects Chapter : Rational Numbers Posted: Sep 19, 2012 1:56 PM
Never mind, I see that actually that is actually addressed in the chapter on Object Equality.

 Flat View: This topic has 14 replies on 1 page
 Previous Topic Next Topic