This page is no longer maintained — Please continue to the home page at www.scala-lang.org

scala newbie question

25 replies
sw2wolf
Joined: 2009-07-16,
User offline. Last seen 42 years 45 weeks ago.

LiftRules.dispatch.append(NamedPF("Web Services Example") {
case Req("webservices" :: "all_users" :: Nil, _, GetRequest) =>
() => Full(all_users())

case Req("webservices" :: "add_user" :: Nil, _, rt)
if rt == GetRequest || rt == PostRequest =>
() => Full(add_user())
}
)

what do those case match ?

Sincerely!

-----
nightmare = unsafePerformIO(getWrongWife >>= sex)

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 4 days ago.
Re: scala newbie question
Pattern matching syntax isn't just used for "match" statements. It can also be used to construct a PartialFunction[A, B]. So the following:

  val pf = {
    case ... => ...
    case ... => ...
  }

Creates a PartialFunction

--j

On Thu, Jul 23, 2009 at 6:48 PM, sw2wolf <czsq888@163.com> wrote:

LiftRules.dispatch.append(NamedPF("Web Services Example") {
       case Req("webservices" :: "all_users" :: Nil, _, GetRequest) =>
         () => Full(all_users())

       case Req("webservices" :: "add_user" :: Nil, _, rt)
         if rt == GetRequest || rt == PostRequest =>
         () => Full(add_user())
     }
)

what do those case match ?

Sincerely!

-----
nightmare = unsafePerformIO(getWrongWife >>= sex)
--
View this message in context: http://www.nabble.com/scala-newbie-question-tp24636494p24636494.html
Sent from the Scala - User mailing list archive at Nabble.com.


sw2wolf
Joined: 2009-07-16,
User offline. Last seen 42 years 45 weeks ago.
Re: scala newbie question

thanks you ! but i still not very understand it ! the result is:
NamedPF("Web Services Example") {() => Full(all_users())} or
NamedPF("Web Services Example") {() => () => Full(add_user())} or
NamedPF("Web Services Example") {}

Jorge Ortiz-3 wrote:
>
> Pattern matching syntax isn't just used for "match" statements. It can
> also
> be used to construct a PartialFunction[A, B]. So the following:
>
> val pf = {
> case ... => ...
> case ... => ...
> }
>
> Creates a PartialFunction
>
> --j
>
> On Thu, Jul 23, 2009 at 6:48 PM, sw2wolf wrote:
>
>>
>> LiftRules.dispatch.append(NamedPF("Web Services Example") {
>> case Req("webservices" :: "all_users" :: Nil, _, GetRequest) =>
>> () => Full(all_users())
>>
>> case Req("webservices" :: "add_user" :: Nil, _, rt)
>> if rt == GetRequest || rt == PostRequest =>
>> () => Full(add_user())
>> }
>> )
>>
>> what do those case match ?
>>
>> Sincerely!
>>
>> -----
>> nightmare = unsafePerformIO(getWrongWife >>= sex)
>> --
>> View this message in context:
>> http://www.nabble.com/scala-newbie-question-tp24636494p24636494.html
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>
>

-----
nightmare = unsafePerformIO(getWrongWife >>= sex)

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: scala newbie question
Not knowing the Lift library, I can't do nothing but make an educated guess.
I assume that this call is appending the partial function {...} that follows it to an internal data structure, under the heading indicated by the NamedPF.
A partial function has two calls. One call applies the function, and one call sees if the function is defined for a value. Given the "dispatch" in the name, this probably adds a handler to "Web Services Example". When something comes in, the dispatcher tests each handler to see if it can handle the value. If the value matches either of these two cases below, then this partial function would be called passing the value.
Note, in particular, that this partial function most likely isn't being used in this part of the code. I imagine. :-)

On Thu, Jul 23, 2009 at 8:48 PM, sw2wolf <czsq888@163.com> wrote:

LiftRules.dispatch.append(NamedPF("Web Services Example") {
       case Req("webservices" :: "all_users" :: Nil, _, GetRequest) =>
         () => Full(all_users())

       case Req("webservices" :: "add_user" :: Nil, _, rt)
         if rt == GetRequest || rt == PostRequest =>
         () => Full(add_user())
     }
)

what do those case match ?

Sincerely!

-----
nightmare = unsafePerformIO(getWrongWife >>= sex)
--
View this message in context: http://www.nabble.com/scala-newbie-question-tp24636494p24636494.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 4 days ago.
Re: scala newbie question
NamedPF just takes a PartialFunction and wraps it inside a "name". It helps when you're debugging, you can get a better sense of what PFs you're in.

In this case, you're matching against Req (Lift's Request) objects.

If it's a GET request to /webservices/all_users, then you return () => Full(all_users()), which is just a function that when called, returns a Full Box (Lift's Option-like class) with the result of the all_users() method.

If it's a GET or a POST request to /webservices/add_user, then you return () => Full(add_user()), which is just a function that when called, returns a Full Box with the result of the add_user() method.

The "if rt == GetRequest || rt == PostRequest =>" part is actually part of the pattern match. If the request isn't a GET or a POST request, the PF won't match.

--j

On Thu, Jul 23, 2009 at 8:23 PM, sw2wolf <czsq888@163.com> wrote:

thanks you ! but i still not very understand it !  the result is:
NamedPF("Web Services Example") {() => Full(all_users())}  or
NamedPF("Web Services Example") {() => () => Full(add_user())}  or
NamedPF("Web Services Example") {}


Jorge Ortiz-3 wrote:
>
> Pattern matching syntax isn't just used for "match" statements. It can
> also
> be used to construct a PartialFunction[A, B]. So the following:
>
>   val pf = {
>     case ... => ...
>     case ... => ...
>   }
>
> Creates a PartialFunction
>
> --j
>
> On Thu, Jul 23, 2009 at 6:48 PM, sw2wolf <czsq888@163.com> wrote:
>
>>
>> LiftRules.dispatch.append(NamedPF("Web Services Example") {
>>        case Req("webservices" :: "all_users" :: Nil, _, GetRequest) =>
>>          () => Full(all_users())
>>
>>        case Req("webservices" :: "add_user" :: Nil, _, rt)
>>          if rt == GetRequest || rt == PostRequest =>
>>          () => Full(add_user())
>>      }
>> )
>>
>> what do those case match ?
>>
>> Sincerely!
>>
>> -----
>> nightmare = unsafePerformIO(getWrongWife >>= sex)
>> --
>> View this message in context:
>> http://www.nabble.com/scala-newbie-question-tp24636494p24636494.html
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>>
>
>


-----
nightmare = unsafePerformIO(getWrongWife >>= sex)
--
View this message in context: http://www.nabble.com/scala-newbie-question-tp24636494p24637266.html
Sent from the Scala - User mailing list archive at Nabble.com.


assafzar
Joined: 2009-06-23,
User offline. Last seen 1 year 6 weeks ago.
Newbie question
Hi guys,
I am new to Scala. My question is basic.

Consider the following lines:
val x = 5
var y = x
y = 6

I know that x hasn't change, and that, there are no primitives in Scala. How is this behavior implemented? Is it by overloading the operator "=" of class Int?

Thanks,
Assaf
David Flemström
Joined: 2009-08-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Newbie question
p, li { white-space: pre-wrap; }

Scala uses JVM where possible when the code is compiled, so numbers in general will behave just like they wold in Java.

Also, why would your code fail? There's nothing to indicate that the code would behave unintuitively...

An analog example:

class X(val value: Int)

val a = new X(3)

var b = a

b = new X(15) //a is still X(3)

On Friday 11 September 2009 19:45:26 Assaf Zaritsky wrote:

> Hi guys,

> I am new to Scala. My question is basic.

>

> Consider the following lines:

> val x = 5

> var y = x

> y = 6

>

> I know that x hasn't change, and that, there are no primitives in Scala.

> How is this behavior implemented? Is it by overloading the operator "=" of

> class Int?

>

> Thanks,

> Assaf

>

David Flemström

david.flemstrom@gmail.com

assafzar
Joined: 2009-06-23,
User offline. Last seen 1 year 6 weeks ago.
Re: Newbie question
Thanks David,

So is it true that after
val x = 5
val y = x

x and y are references to the same memory address? If "=" is not overloaded it should behave this way

On Fri, Sep 11, 2009 at 10:55, David Flemström <david.flemstrom@gmail.com> wrote:

Scala uses JVM where possible when the code is compiled, so numbers in general will behave just like they wold in Java.

Also, why would your code fail? There's nothing to indicate that the code would behave unintuitively...

An analog example:

class X(val value: Int)

val a = new X(3)

var b = a

b = new X(15) //a is still X(3)

On Friday 11 September 2009 19:45:26 Assaf Zaritsky wrote:

> Hi guys,

> I am new to Scala. My question is basic.

>

> Consider the following lines:

> val x = 5

> var y = x

> y = 6

>

> I know that x hasn't change, and that, there are no primitives in Scala.

> How is this behavior implemented? Is it by overloading the operator "=" of

> class Int?

>

> Thanks,

> Assaf

>

David Flemström

david.flemstrom@gmail.com


David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Newbie question


On Fri, Sep 11, 2009 at 11:04 AM, Assaf Zaritsky <assafzar@gmail.com> wrote:
Thanks David,

So is it true that after
val x = 5
val y = x

x and y are references to the same memory address? If "=" is not overloaded it should behave this way

Look at it this way:

val x = "Hello"
var y = x

y = "World"

We have not changed x, despite that y has a different reference.

Scala implements AnyVals in the most efficient way possible.

You can make a List[Int] (Int is a subclass of AnyVal).  In the case of each element in the List, there will be a boxed reference to an instance of the java.lang.Integer class.

However, if you're doing something like:

def myMethod() {
  val x = 5
  val y = x
  y = 7
  val z = x + y
  println("z is "+z)
}
 
The generated bytecode will look a whole lot like javac generated it.  The primitives will be used because there's nothing to box.


On Fri, Sep 11, 2009 at 10:55, David Flemström <david.flemstrom@gmail.com> wrote:

Scala uses JVM where possible when the code is compiled, so numbers in general will behave just like they wold in Java.

Also, why would your code fail? There's nothing to indicate that the code would behave unintuitively...

An analog example:

class X(val value: Int)

val a = new X(3)

var b = a

b = new X(15) //a is still X(3)

On Friday 11 September 2009 19:45:26 Assaf Zaritsky wrote:

> Hi guys,

> I am new to Scala. My question is basic.

>

> Consider the following lines:

> val x = 5

> var y = x

> y = 6

>

> I know that x hasn't change, and that, there are no primitives in Scala.

> How is this behavior implemented? Is it by overloading the operator "=" of

> class Int?

>

> Thanks,

> Assaf

>

David Flemström

david.flemstrom@gmail.com





--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
David Flemström
Joined: 2009-08-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Newbie question
p, li { white-space: pre-wrap; }

Because primitives are stored on the stack, they don't use references, but you could say that they use references conceptually, because it really doesn't matter if thet do or not.

So, in other words, after those two lines of code have executed, x will contain the value 5 and y will also contain the value 5, but they don't point to the same 5; they contain copies instead. It's more efficient that way.

Also, the difference is that you would need 24 bytes of memory if you would use references, and 8 bytes of memory if you use primitives like Scala does at the moment, so it's not a bad thing that Scala behaves like this.

On Friday 11 September 2009 20:04:44 Assaf Zaritsky wrote:

> Thanks David,

>

> So is it true that after

> val x = 5

> val y = x

>

> x and y are references to the same memory address? If "=" is not overloaded

> it should behave this way

>

> On Fri, Sep 11, 2009 at 10:55, David Flemström <david.flemstrom@gmail.com>wrote:

> > Scala uses JVM where possible when the code is compiled, so numbers in

> > general will behave just like they wold in Java.

> >

> > Also, why would your code fail? There's nothing to indicate that the code

> > would behave unintuitively...

> >

> > An analog example:

> >

> > class X(val value: Int)

> >

> > val a = new X(3)

> >

> > var b = a

> >

> > b = new X(15) //a is still X(3)

> >

> > On Friday 11 September 2009 19:45:26 Assaf Zaritsky wrote:

> > > Hi guys,

> > >

> > > I am new to Scala. My question is basic.

> > >

> > >

> > >

> > > Consider the following lines:

> > >

> > > val x = 5

> > >

> > > var y = x

> > >

> > > y = 6

> > >

> > >

> > >

> > > I know that x hasn't change, and that, there are no primitives in

> > > Scala.

> > >

> > > How is this behavior implemented? Is it by overloading the operator "="

> >

> > of

> >

> > > class Int?

> > >

> > >

> > >

> > > Thanks,

> > >

> > > Assaf

> >

> > David Flemström

> >

> > david.flemstrom@gmail.com

>

David Flemström

david.flemstrom@gmail.com

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Newbie question

val x = 5

x points at the value 5

var y = x

At this point, x is evaluated, so this line is equivalent to

var y = 5

y points at the value 5 (so does x)

y = 6

y now points at the value 6, x still points at the value 5.

2009/9/11 Assaf Zaritsky :
> Hi guys,
> I am new to Scala. My question is basic.
>
> Consider the following lines:
> val x = 5
> var y = x
> y = 6
>
> I know that x hasn't change, and that, there are no primitives in Scala. How
> is this behavior implemented? Is it by overloading the operator "=" of class
> Int?
>
> Thanks,
> Assaf
>

Josh Stratton
Joined: 2009-04-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Newbie question

I'm not sure if we're hitting the question. He understands the
behaivor, but wants to know how it's implemented. If Ints are
objects, too, x = 5 creates a Int pointing to 5, var y = x points to
the reference of x, and from then on, if x changes, you'll get the
same value in, which obviously isn't the case. If it switches at "var
y = x" to pass by value, how is that implemented? in Scala.Int
there's no overloaded "=" sign.

Josh

On Fri, Sep 11, 2009 at 2:17 PM, Ricky Clarkson
wrote:
> val x = 5
>
> x points at the value 5
>
> var y = x
>
> At this point, x is evaluated, so this line is equivalent to
>
> var y = 5
>
> y points at the value 5 (so does x)
>
> y = 6
>
> y now points at the value 6, x still points at the value 5.
>
> 2009/9/11 Assaf Zaritsky :
>> Hi guys,
>> I am new to Scala. My question is basic.
>>
>> Consider the following lines:
>> val x = 5
>> var y = x
>> y = 6
>>
>> I know that x hasn't change, and that, there are no primitives in Scala. How
>> is this behavior implemented? Is it by overloading the operator "=" of class
>> Int?
>>
>> Thanks,
>> Assaf
>>
>
>
>
> --
> Ricky Clarkson
> Java Programmer, AD Holdings
> +44 1565 770804
> Skype: ricky_clarkson
> Google Talk: ricky.clarkson@gmail.com
>

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Newbie question

x = 5 does not create an Int, but makes x refer to the Int 5.

"var y = x points to the reference of x" is confusing, but vague
enough to be possibly correct.

There is no switch to pass by value. Only call by value and call by
name are supported in Scala, and this example only uses call by value.

= is defined in the compiler, not in scala.Int.

The behaviour is the same as in Java.

2009/9/11 Josh Stratton :
> I'm not sure if we're hitting the question.  He understands the
> behaivor, but wants to know how it's implemented.  If Ints are
> objects, too, x = 5 creates a Int pointing to 5, var y = x points to
> the reference of x, and from then on, if x changes, you'll get the
> same value in, which obviously isn't the case.  If it switches at "var
> y = x" to pass by value, how is that implemented?  in Scala.Int
> there's no overloaded "=" sign.
>
> Josh
>
>
> On Fri, Sep 11, 2009 at 2:17 PM, Ricky Clarkson
> wrote:
>> val x = 5
>>
>> x points at the value 5
>>
>> var y = x
>>
>> At this point, x is evaluated, so this line is equivalent to
>>
>> var y = 5
>>
>> y points at the value 5 (so does x)
>>
>> y = 6
>>
>> y now points at the value 6, x still points at the value 5.
>>
>> 2009/9/11 Assaf Zaritsky :
>>> Hi guys,
>>> I am new to Scala. My question is basic.
>>>
>>> Consider the following lines:
>>> val x = 5
>>> var y = x
>>> y = 6
>>>
>>> I know that x hasn't change, and that, there are no primitives in Scala. How
>>> is this behavior implemented? Is it by overloading the operator "=" of class
>>> Int?
>>>
>>> Thanks,
>>> Assaf
>>>
>>
>>
>>
>> --
>> Ricky Clarkson
>> Java Programmer, AD Holdings
>> +44 1565 770804
>> Skype: ricky_clarkson
>> Google Talk: ricky.clarkson@gmail.com
>>
>

David Flemström
Joined: 2009-08-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Newbie question
p, li { white-space: pre-wrap; }

Well, we could just say that there are a lot of singleton objects called "1", "2", "3" etc, and those objects are immutable. That would fit the object model without any more issues, right? Because an Int is basically immutable; it's always "the reference" that changes.

> var y = x points to

> the reference of x, and from then on, if x changes, you'll get the

> same value in, which obviously isn't the case.

That's not how var works IIRC... Continuing with this simplified object model, we can say that x also only contains a reference to 5. The assignment operator never copies references; it creates another reference to the object that the assigned value points to, so nothing is changed in the original ("x") variable.

On Friday 11 September 2009 23:25:41 Josh Stratton wrote:

> I'm not sure if we're hitting the question. He understands the

> behaivor, but wants to know how it's implemented. If Ints are

> objects, too, x = 5 creates a Int pointing to 5, var y = x points to

> the reference of x, and from then on, if x changes, you'll get the

> same value in, which obviously isn't the case. If it switches at "var

> y = x" to pass by value, how is that implemented? in Scala.Int

> there's no overloaded "=" sign.

>

> Josh

>

>

> On Fri, Sep 11, 2009 at 2:17 PM, Ricky Clarkson

>

> <ricky.clarkson@gmail.com> wrote:

> > val x = 5

> >

> > x points at the value 5

> >

> > var y = x

> >

> > At this point, x is evaluated, so this line is equivalent to

> >

> > var y = 5

> >

> > y points at the value 5 (so does x)

> >

> > y = 6

> >

> > y now points at the value 6, x still points at the value 5.

> >

> > 2009/9/11 Assaf Zaritsky <assafzar@gmail.com>:

> >> Hi guys,

> >> I am new to Scala. My question is basic.

> >>

> >> Consider the following lines:

> >> val x = 5

> >> var y = x

> >> y = 6

> >>

> >> I know that x hasn't change, and that, there are no primitives in Scala.

> >> How is this behavior implemented? Is it by overloading the operator "="

> >> of class Int?

> >>

> >> Thanks,

> >> Assaf

> >

> > --

> > Ricky Clarkson

> > Java Programmer, AD Holdings

> > +44 1565 770804

> > Skype: ricky_clarkson

> > Google Talk: ricky.clarkson@gmail.com

>

David Flemström

david.flemstrom@gmail.com

Josh Stratton
Joined: 2009-04-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Newbie question

>> know that x hasn't change, and that, there are no primitives in Scala. How is this behavior implemented? Is it by overloading the operator "=" of class Int?

> = is defined in the compiler, not in scala.Int.

I assume that's what he was looking for. Thanks.

Josh

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: Newbie question
How about this then:
//evaluate 5, assign the result (5) to the val xval x = 5//now x = 5

//evaluate x, assign the result (5) to the var y
var y = x //now x = 5, y = 5
//evaluate 6, assign the result (6) to the var y
y = 6//now x = 5, y = 6


This same logic (evaluate then assign) also makes sense in the presence of mutable objects:

//create a mutable class for democase class Foo(var bar : Int)
//evaluate Foo(5), assign the resulting Foo(5) to the val x val x = Foo(5)//now x = (reference to a) Foo(5)

//evaluate x, assign the result Foo(5) to the var y
var y = x//now y = x = reference to a Foo(5) //both x and y point to the same instance
//mutate the object that x points to x.bar = 6 //now y = x = reference to a Foo(6)//both x and y point to the same instance

//evaluate Foo(6), assign the result Foo(6) to the var y
y = Foo(6)//now x = reference to a Foo(5), y = reference to a Foo(6)
//x and y point to different instances


On Fri, Sep 11, 2009 at 10:33 PM, Josh Stratton <strattonbrazil@gmail.com> wrote:
>> know that x hasn't change, and that, there are no primitives in Scala. How is this behavior implemented? Is it by overloading the operator "=" of class Int?

> = is defined in the compiler, not in scala.Int.

I assume that's what he was looking for.  Thanks.

Josh

didi1
Joined: 2009-09-16,
User offline. Last seen 3 years 7 weeks ago.
Newbie Question

Hey Guys,

I am new to Scala. I had a question about a method that I am trying to write
which looks fine to me but the output is basically blank.

>def factors(n: Int): List[Int] = {
|var list = List()
|var i = 1
|while( i < n ) {
| if(n % i == 0)
| i :: list
| i+=1
|}
| list
}

I get no errors for doing this but if I type in factors(6) for example, I
get nothing as output (blank).
In the 6th line, I tried writing it like this : list = i :: list but then I
get an error of mismatch saying : Found = Int,, Required: Nothing

If you could help with this, I would deeply appreciate it.

Thanks

michaelg
Joined: 2009-06-11,
User offline. Last seen 1 year 23 weeks ago.
Re: Newbie Question
In your code, you are not appending the factor (i) to the list. You can use ::= to do that, which is shorthand for list = i :: list . You need to type the empty list. Try:
  def factors(n:Int) = {     var list:List[Int] = Nil    var i = 1    while (i < n){      if (n % i == 0){        list ::= i      }      i += 1    }    list   }
-- Michael

On Tue, Sep 15, 2009 at 10:28 PM, didi1 <diedonst@gmail.com> wrote:

Hey Guys,

I am new to Scala. I had a question about a method that I am trying to write
which looks fine to me but the output is basically blank.

>def factors(n: Int): List[Int] = {
|var list =  List()
|var i = 1
|while( i < n ) {
|   if(n % i == 0)
|       i :: list
|   i+=1
|}
| list
}

I get no errors for doing this but if I type in factors(6) for example, I
get nothing as output (blank).
In the 6th line, I tried writing it like this : list = i :: list but then I
get an error of mismatch saying : Found = Int,, Required: Nothing

If you could help with this, I would deeply appreciate it.

Thanks
--
View this message in context: http://www.nabble.com/Newbie-Question-tp25466170p25466170.html
Sent from the Scala - User mailing list archive at Nabble.com.


ewilligers
Joined: 2008-08-20,
User offline. Last seen 3 years 17 weeks ago.
Re: Newbie Question

didi1 wrote:
> Hey Guys,
>
> I am new to Scala. I had a question about a method that I am trying to write
> which looks fine to me but the output is basically blank.
>
>> def factors(n: Int): List[Int] = {
> |var list = List()
> |var i = 1
> |while( i < n ) {
> | if(n % i == 0)
> | i :: list
> | i+=1
> |}
> | list
> }
>
> I get no errors for doing this but if I type in factors(6) for example, I
> get nothing as output (blank).
> In the 6th line, I tried writing it like this : list = i :: list but then I
> get an error of mismatch saying : Found = Int,, Required: Nothing

You can avoid the error with
var list = List[Int]()

Either of the following are fine:-
list = i :: list
list ::= i

You might want to use
while( i <= n ) {
as n is a factor of n.

didi1
Joined: 2009-09-16,
User offline. Last seen 3 years 7 weeks ago.
Re: Newbie Question

Michael,

Thanks for the quick answer. It's working!

I deeply appreciate it!

Doni,

I was

michaelg wrote:
>
> In your code, you are not appending the factor (i) to the list. You can
> use
> ::= to do that, which is shorthand for list = i :: list . You need to type
> the empty list. Try:
> def factors(n:Int) = {
> var list:List[Int] = Nil
> var i = 1
> while (i < n){
> if (n % i == 0){
> list ::= i
> }
> i += 1
> }
> list
> }
>

Eastsun 2
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Newbie Question

A shorter solution:

scala> def factors(n :Int) = (1 until n) filter { n%_ == 0 } toList
factors: (n: Int)List[Int]

scala> factors(12)
res7: List[Int] = List(1, 2, 3, 4, 6)

didi1 wrote:
>
> Hey Guys,
>
> I am new to Scala. I had a question about a method that I am trying to
> write which looks fine to me but the output is basically blank.
>
>>def factors(n: Int): List[Int] = {
> |var list = List()
> |var i = 1
> |while( i < n ) {
> | if(n % i == 0)
> | i :: list
> | i+=1
> |}
> | list
> }
>
> I get no errors for doing this but if I type in factors(6) for example, I
> get nothing as output (blank).
> In the 6th line, I tried writing it like this : list = i :: list but then
> I get an error of mismatch saying : Found = Int,, Required: Nothing
>
> If you could help with this, I would deeply appreciate it.
>
> Thanks
>

-----
My scala solutions for http://projecteuler.net/ Project Euler problems:
http://eastsun.javaeye.com/category/34059 Click here

Peter Robinett
Joined: 2009-09-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Newbie Question
And if you plan on using the method with very large numbers it might be useful to optimize it by only doing checking the first half of the sequence of integers:
scala> def factors(n :Int) = (1 to Math.round(n/2)) filter { n%_ == 0 } toList factors: (Int)List[Int]
scala> factors(2000000)res12: List[Int] = List(1, 2, 4, 5, 8, 10, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 128, 160, 200, 250, 320, 400, 500, 625, 640, 800, 1000, 1250, 1600, 2000, 2500, 3125, 3200, 4000, 5000, 6250, 8000, 10000, 12500, 15625, 16000, 20000, 25000, 31250, 40000, 50000, 62500, 80000, 100000, 125000, 200000, 250000, 400000, 500000, 1000000)
Of course, this isn't necessary to answer Doni's question...
Peter Robinett
On Wed, Sep 16, 2009 at 12:41 PM, Eastsun <flushtime@126.com> wrote:

A shorter solution:

scala> def factors(n :Int) = (1 until n) filter { n%_ == 0 } toList
factors: (n: Int)List[Int]

scala> factors(12)
res7: List[Int] = List(1, 2, 3, 4, 6)


didi1 wrote:
>
> Hey Guys,
>
> I am new to Scala. I had a question about a method that I am trying to
> write which looks fine to me but the output is basically blank.
>
>>def factors(n: Int): List[Int] = {
> |var list =  List()
> |var i = 1
> |while( i < n ) {
> |   if(n % i == 0)
> |       i :: list
> |   i+=1
> |}
> | list
> }
>
> I get no errors for doing this but if I type in factors(6) for example, I
> get nothing as output (blank).
> In the 6th line, I tried writing it like this : list = i :: list but then
> I get an error of mismatch saying : Found = Int,, Required: Nothing
>
> If you could help with this, I would deeply appreciate it.
>
> Thanks
>


-----
My scala solutions for  http://projecteuler.net/ Project Euler  problems:
http://eastsun.javaeye.com/category/34059 Click here
--
View this message in context: http://www.nabble.com/Newbie-Question-tp25466170p25469908.html
Sent from the Scala - User mailing list archive at Nabble.com.


dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Newbie Question
Doing "Math.round(n/2)" is silly. Since "n" is Int, "n/2" is Int as well. You are then getting an Int, converting it to Double, uselessly rounding it (since it's already an integer), and then converting it back to the same Int you had in first place.

On Wed, Sep 16, 2009 at 12:51 PM, Peter Robinett <peter@equal-networks.com> wrote:
And if you plan on using the method with very large numbers it might be useful to optimize it by only doing checking the first half of the sequence of integers:
scala> def factors(n :Int) = (1 to Math.round(n/2)) filter { n%_ == 0 } toList factors: (Int)List[Int]
scala> factors(2000000) res12: List[Int] = List(1, 2, 4, 5, 8, 10, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 128, 160, 200, 250, 320, 400, 500, 625, 640, 800, 1000, 1250, 1600, 2000, 2500, 3125, 3200, 4000, 5000, 6250, 8000, 10000, 12500, 15625, 16000, 20000, 25000, 31250, 40000, 50000, 62500, 80000, 100000, 125000, 200000, 250000, 400000, 500000, 1000000)
Of course, this isn't necessary to answer Doni's question...
Peter Robinett
On Wed, Sep 16, 2009 at 12:41 PM, Eastsun <flushtime@126.com> wrote:

A shorter solution:

scala> def factors(n :Int) = (1 until n) filter { n%_ == 0 } toList
factors: (n: Int)List[Int]

scala> factors(12)
res7: List[Int] = List(1, 2, 3, 4, 6)


didi1 wrote:
>
> Hey Guys,
>
> I am new to Scala. I had a question about a method that I am trying to
> write which looks fine to me but the output is basically blank.
>
>>def factors(n: Int): List[Int] = {
> |var list =  List()
> |var i = 1
> |while( i < n ) {
> |   if(n % i == 0)
> |       i :: list
> |   i+=1
> |}
> | list
> }
>
> I get no errors for doing this but if I type in factors(6) for example, I
> get nothing as output (blank).
> In the 6th line, I tried writing it like this : list = i :: list but then
> I get an error of mismatch saying : Found = Int,, Required: Nothing
>
> If you could help with this, I would deeply appreciate it.
>
> Thanks
>


-----
My scala solutions for  http://projecteuler.net/ Project Euler  problems:
http://eastsun.javaeye.com/category/34059 Click here
--
View this message in context: http://www.nabble.com/Newbie-Question-tp25466170p25469908.html
Sent from the Scala - User mailing list archive at Nabble.com.





--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
grahamh
Joined: 2011-04-02,
User offline. Last seen 42 years 45 weeks ago.
Newbie Question

I'm afraid i am rather new to Scala, and come from a background with
just a little Ruby, Python and Java experience.

I have a small snippet of code that is causing me some confusion.
Could someone please explain to a newbie what they think is happening
here.

The following code works compiles and works as I expect.

class invoiceNumber {
println("Enter the starting invoice number for the conversion")
var lastNumber : Int = readLine().toInt -1

def getNextInvoiceNumber : Int = {
println(lastNumber)
lastNumber = (lastNumber + 1)
return lastNumber
}
}

However, if I comment out the "return lastNumber" line, the code gives
the following compiler error.

type mismatch;

found : Unit
required : Int
lastNumber = (lastNumber + 1)
^
one error found

Why would commenting out that last line change the type of lastNumber?
Isn't it defined as being of type Int already? Can anyone explain,
please.

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Newbie Question
You probably think that "a = 1" returns "1". It doesn't. It returns "Unit", which is kinda like "void".

On Fri, Apr 1, 2011 at 20:54, grahamh <graham@garden-images.co.uk> wrote:
I'm afraid i am rather new to Scala, and come from a background with
just a little Ruby, Python and Java experience.

I have a small snippet of code that is causing me some confusion.
Could someone please explain to a newbie what they think is happening
here.

The following code works compiles and works as I expect.

class invoiceNumber {
 println("Enter the starting invoice number for the conversion")
 var lastNumber : Int = readLine().toInt -1

 def getNextInvoiceNumber : Int = {
   println(lastNumber)
       lastNumber = (lastNumber + 1)
       return lastNumber
 }
}

However, if I comment out the "return lastNumber" line, the code gives
the following compiler error.

type mismatch;

found      :  Unit
required  :  Int
         lastNumber = (lastNumber + 1)
                           ^
one error found


Why would commenting out that last line change the type of lastNumber?
Isn't it defined as being of type Int already? Can anyone explain,
please.




--
Daniel C. Sobral

I travel to the future all the time.
jibal
Joined: 2010-12-01,
User offline. Last seen 1 year 45 weeks ago.
Re: Newbie Question

-- Jim



On Fri, Apr 1, 2011 at 4:54 PM, grahamh <graham@garden-images.co.uk> wrote:
I'm afraid i am rather new to Scala, and come from a background with
just a little Ruby, Python and Java experience.

I have a small snippet of code that is causing me some confusion.
Could someone please explain to a newbie what they think is happening
here.

The following code works compiles and works as I expect.

class invoiceNumber {
 println("Enter the starting invoice number for the conversion")
 var lastNumber : Int = readLine().toInt -1

 def getNextInvoiceNumber : Int = {
   println(lastNumber)
       lastNumber = (lastNumber + 1)
       return lastNumber
 }
}

However, if I comment out the "return lastNumber" line, the code gives
the following compiler error.

type mismatch;

found      :  Unit
required  :  Int
         lastNumber = (lastNumber + 1)
                           ^
one error found


Why would commenting out that last line change the type of lastNumber?
Isn't it defined as being of type Int already? Can anyone explain,
please.


In Scala, the value of an assignment statement is Unit, not the value of the lhs. The  "normal" way to write that in Scala is by explicitly giving the value of the block (rather than using a return statement):

 def getNextInvoiceNumber = {
    println(lastNumber)
    lastNumber += 1
    lastNumber
 }

-- Jim

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland