The Artima Developer Community
Sponsored Link

Weblogs Forum
Scala, Patterns and The Perl Effect

19 replies on 2 pages. Most recent reply: Sep 22, 2013 6:26 AM by Oliver Plohmann

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 19 replies on 2 pages [ « | 1 2 ]
Trond Olsen

Posts: 14
Nickname: tolsen
Registered: Mar, 2007

Re: Scala, Patterns and The Perl Effect Posted: Dec 25, 2011 6:55 AM
Reply to this message Reply
Advertisement
Further extending the example with a catch-all clause:

val f4: PartialFunction[Any,String] = {
case _ => "undefined"
}
// f4: PartialFunction[Any,String] = <function1>

val f5 = f3 orElse f4
// f5: PartialFunction[Any,String] = <function1>

f5("a")
// res3: String = undefined

Channing Walton

Posts: 32
Nickname: channing
Registered: May, 2003

Re: Scala, Patterns and The Perl Effect Posted: Dec 30, 2011 3:41 PM
Reply to this message Reply
Here is a another simple, concrete example.

val root:PartialFunction[Int, Double] = {
case x if x >=0 => math.sqrt(x)
}

val printIt:PartialFunction[Int, String] = { case i => "Root(" + i + ")"}

val roots = root orElse printIt

(-3 to 3) map (roots(_))
= Vector(Root(-3), Root(-2), Root(-1), 0.0, 1.0, 1.4142135623730951, 1.7320508075688772)

philippos papadatos

Posts: 6
Nickname: firesoft
Registered: Mar, 2012

Re: Scala, Patterns and The Perl Effect Posted: Mar 28, 2012 7:02 PM
Reply to this message Reply
I’m in the process of evaluating scala for a new project.
The Perl effect indeed! I had a look at scala's website and i downloaded the free ebook.
I don't know about you but for me it feels like common best practices in Scala introduce very cryptic syntax in order to produce one liners. I don't see the value in that , I rather keep my code base stupidly simple, rather to reduce LOC, which at a glance can make sense rather spend 1-2 minutes figuring out what each line do.
Where is the productivity of scala then is it all hype ?
I don't know about you but i almost never write a component once and move on, i almost always revisit the code and make improvements- modifications. I feel with scala if i revisit a part of my code base say after a few months it will take me a long time to figure out what i was doing then!
I mainly use java & python and i don't see the benefits of Scala. If i need functional - dynamic features i use python and the combination of java + jython is very appealing to me.

Any suggestions on what Scala brings to the table?

philippos papadatos

Posts: 6
Nickname: firesoft
Registered: Mar, 2012

Re: Scala, Patterns and The Perl Effect Posted: Nov 28, 2012 4:19 AM
Reply to this message Reply
well upon further investigation Scala is very interesting and complex ! if choose to make it complex...

Oliver Plohmann

Posts: 13
Nickname: olli
Registered: Jun, 2008

Re: Scala, Patterns and The Perl Effect Posted: Sep 22, 2013 6:26 AM
Reply to this message Reply
This is the code from the article:

List(41, "cat") collect { case i: Int ⇒ i + 1 }

The equivalent Smalltalk code looks like this:


| list |
list := OrderedCollection new add: 41; add: "cat"; yourself.
(list select: [ :each | each isInteger]) collect: [ :each | each + 1]

So the Scala solution is more concise as the concatenation of select and collect is shorter. Nevertheless, also in a dynamically typed languages like Smalltalk people don't throw strings and ints into the same collection. Especially in a dynamically typed language this will likely result in runtime errors that don't disappear until you made this collection only contain elements of a common type.

I really don't understand what message the author wants to convey. If I want to understand what partial functions are, then I will have a look into the book by Odersky and I hope the sample code is not simply a mess eben a Python/Ruby/Perl/Smalltalk/etc. developer wouldn't write. Okay, I was a bit rough. But the way Scala is explained sometimes misses a bit rigor.

Flat View: This topic has 19 replies on 2 pages [ « | 1  2 ]
Topic: Atomic Scala eBook & Downloadable Hands-On Java eSeminar Previous Topic   Next Topic Topic: Reflecting generics

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use