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

What is you guys first real-world scala program?

22 replies
linjie nie
Joined: 2009-06-17,
User offline. Last seen 42 years 45 weeks ago.

Hi list -
I have been studying scala for weeks, and coded some toys in it. But
when I am going to do a real-world coding, I always chose Java/Groovy.
So, could you guys please, share here what is your first real-world
scala programe, and why you chose scala for this programe?

Thanks a lot.

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: What is you guys first real-world scala program?
For me, my first "real" use of Scala was in an existing Java program.First via ScalaTest, to help with BDD, and then later gradually converting existing Java classes.
Anywhere I saw that could be simplified with case classes and/or pattern matching was a good candidate here.
I've also done a few bits an pieces with networking and serial communications for personal use (home automation stuff).
The LSUG.org website I did in Scala/lift, before we lost the domain name...
My most significant pure Scala project so far has to be the autoproxy-plugin , as released on github yesterday:http://github.com/scala-incubator/autoproxy-plugin



On Tue, Nov 17, 2009 at 6:21 AM, linjie nie <nielinjie@gmail.com> wrote:
Hi list -
I have been studying scala for weeks, and coded some toys in it. But
when I am going to do a real-world coding, I always chose Java/Groovy.
So, could you guys please, share here what is your first real-world
scala programe, and why you chose scala for this programe?

Thanks a lot.

andrew cooke 2
Joined: 2009-10-24,
User offline. Last seen 42 years 45 weeks ago.
Re: What is you guys first real-world scala program?

Hi,

My first + only Scala program generates MP3 playlists by combining the
information from each file's tags with the metadata provided by
http://last.fm. Together those let me associate each track with a
bunch of words (things like "rock" or "chilean" or whatever), and then
I can work out which tracks are related (because they share the same
words). And from that I can generate a playlist of related songs.

The code is competely uncommented, and currently slightly broken, but
you can see it at http://code.google.com/p/uykfd/ - perhaps the most
interesting (although more to do with Empire DB than Scala) is how I
can define my database schema -
http://code.google.com/p/uykfd/source/browse/src/main/scala/org/acooke/u...

All of the "real work" is done by Java libraries (database access
through Empire DB, graphs with jgrapht, etc).

Andrew

2009/11/17 linjie nie :
> Hi list -
> I have been studying scala for weeks, and coded some toys in it. But
> when I am going to do a real-world coding, I always chose Java/Groovy.
> So, could you guys please, share here what is your first real-world
> scala programe, and why you chose scala for this programe?
>
> Thanks a lot.
>

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: What is you guys first real-world scala program?
My first real-world scala program was a semi-scripting program that managed a Java program needed for data analysis, called several other programs for data analysis and presentation, and snooped into various output files, read them, computed some things, and altered arguments for calling other programs based upon the results.

It also had an extremely simple GUI that let the user point at the directory they wanted to analyze (and the program handled everything else according to sensible defaults).

I chose Scala because I had a mix of string processing/scripting, which is painful to do in Java, and high-performance data I/O and vector computation, which is painful to do quickly enough in the standard scripting languages.

Plus, after working with it for a while, I found that my ability to map what I wanted to happen into Scala code was significantly less error-prone than my mapping to any other code.  This made debugging particularly easy; a lot of things worked on the first try.  (I do not fully understand why, but heavy usage of map/foreach/filter and closures, when appropriate, was certainly part of it.)

  --Rex

On Tue, Nov 17, 2009 at 1:21 AM, linjie nie <nielinjie@gmail.com> wrote:
Hi list -
I have been studying scala for weeks, and coded some toys in it. But
when I am going to do a real-world coding, I always chose Java/Groovy.
So, could you guys please, share here what is your first real-world
scala programe, and why you chose scala for this programe?

Thanks a lot.

Jason Felice
Joined: 2009-11-17,
User offline. Last seen 42 years 45 weeks ago.
Re: What is you guys first real-world scala program?

I'm not sure how "real world" it is since it was a disposable script,
but I learn new languages by making them my "hack language" first.
The first useful Scala thing I did is here:
http://www.jigcode.com/2009/10/05/scala-ugly-hairy-rewrite-c-code-jig/

Warning: It is big. It is ugly. It did accomplish a major feat (a
bulk restructuring of many C++ components), however, and Scala
definitely saved much work over, say, Java or C++ with boost.

Sean Corfield
Joined: 2009-10-25,
User offline. Last seen 2 years 9 weeks ago.
Re: What is you guys first real-world scala program?

My first real-world Scala program reads large numbers of records from
a database, creates a specific XML representation of that data, and
posts it to a search engine.

I wrote it in Scala because I needed something faster than the
scripting language used for most of the rest of the project. I could
have used Java or Groovy but the timing was right for me to try Scala
and I figured the XML support would be a big help.

The next step is to break the main processing down into smaller
concurrent tasks to improve performance as we scale up to millions of
records (another reason for picking Scala over Java / Groovy).

It hasn't been plain sailing. I'm using IDEA 9 and it has lost the
Scala facet a couple of times and I can't use ScalaTest 1.0 so
WordSpec and XML reporting are out of reach for me unless I move up to
Scala 2.8 (IDEA 9 supports ScalaTest 1.0 + Scala 2.8 or ScalaTest
0.9.5 + Scala 2.7.x). That aspect was pretty frustrating but I have
things working well now with automated test scripts as part of my
overall build.

With Scala itself I'm still suffering some confusion over when () can
be omitted vs when it is required, as well as what iteration
constructs work on which structures - but part of that is just the
distance between when I used to do a lot of functional programming
(the 80's) and my last decade of Java :(

Kris Nuttycombe
Joined: 2009-01-16,
User offline. Last seen 42 years 45 weeks ago.
Re: What is you guys first real-world scala program?

My first real-world Scala program has been a billing system for
subscription services. It just crossed a milestone this week, too -
the system has now handled over $1M in transactions. :)

The project started in Java, with some Scala in the test
infrastructure. The Scala escaped the confines of the tests pretty
quickly once it became clear how expressive a language it is.

Kris

On Mon, Nov 16, 2009 at 11:21 PM, linjie nie wrote:
> Hi list -
> I have been studying scala for weeks, and coded some toys in it. But
> when I am going to do a real-world coding, I always chose Java/Groovy.
> So, could you guys please, share here what is your first real-world
> scala programe, and why you chose scala for this programe?
>
> Thanks a lot.
>

Mohamed Bana 2
Joined: 2009-10-21,
User offline. Last seen 42 years 45 weeks ago.
Re: What is you guys first real-world scala program?
Eclipse plug-in written in the Scala 2.7.3/4 days I believe;  http://bitbucket.org/mbana/moodmetricsplug-in/

—Mohamed

On Tue, Nov 17, 2009 at 1:21 AM, linjie nie <nielinjie@gmail.com> wrote:
Hi list -
I have been studying scala for weeks, and coded some toys in it. But
when I am going to do a real-world coding, I always chose Java/Groovy.
So, could you guys please, share here what is your first real-world
scala programe, and why you chose scala for this programe?

Thanks a lot.
mailleux
Joined: 2008-08-23,
User offline. Last seen 4 years 7 weeks ago.
Re: What is you guys first real-world scala program?
My first real-world scala program is Virtual Combat Cards (http://www.exnebula.org/vcc). Its a combat tracker for Dungeons & Dragons Role Playing Game. I'm happy with the project and it is approaching the one year mark.

I chose scala for several reasons:

1) I had tried to express game rules in a DSL and Scala was really good at that (though most of that did not go into the final project)

2) Actor provided a interesting way to model things and decouple components and I liked that.

3) With some nice features and syntactic sugar you can write thinks like :

          aData.value = a.Data.value + 10

    were you really mean:

          aData.setValue(aData.getValue+10, inTransaction)  // The transaction allows for undoability

    This means the code looks straight forward but hides some really complex structures

4) scala.swing has some very nice ideas (an some bugs) that make coding the UI easier

5) XML processing is really nice

6) I had tried this in LISP once and Functional programming had made it much easier

7) Access to a lot of java libraries and cross platform.

I thinks these are the key benefits. The only down site is that it's harder to find people to help in the project. A couple of Java  coders have manifested interest but Scala is not that similar :) Any volunteers ?


Thomas


On Tue, Nov 17, 2009 at 4:21 AM, linjie nie <nielinjie@gmail.com> wrote:
Hi list -
I have been studying scala for weeks, and coded some toys in it. But
when I am going to do a real-world coding, I always chose Java/Groovy.
So, could you guys please, share here what is your first real-world
scala programe, and why you chose scala for this programe?

Thanks a lot.

Michael Davey
Joined: 2009-09-10,
User offline. Last seen 42 years 45 weeks ago.
Re: What is you guys first real-world scala program?

my only real world Scala program is an event processing network taking
production logs from EAI systems and creating various real-time
activity presentations such as response times of SOA services, SLA
compliance stats...

michael

On 11/17/09, linjie nie wrote:
> Hi list -
> I have been studying scala for weeks, and coded some toys in it. But
> when I am going to do a real-world coding, I always chose Java/Groovy.
> So, could you guys please, share here what is your first real-world
> scala programe, and why you chose scala for this programe?
>
> Thanks a lot.
>

Chris Shorrock
Joined: 2009-11-18,
User offline. Last seen 42 years 45 weeks ago.
Re: What is you guys first real-world scala program?
Our "first" production deployment of Scala will be mid next year (date has yet to be finalized) and will serve as the backend for a XBOX 360/PS3 title who aiming to perform some real time distribution of data to a large audience of users both in the console and on the web (not really allowed to say more than that).
Even though the date is far out, the server has been finished for about a month now and is currently undergoing various tests, and is performing quite well.
Scala was used mostly as it provided a more concise language to develop in that allowed us to provide more powerful abstractions, while being able to inter-operate with all our existing Java tech that we've developed up until this point.
Having used Scala for some personal projects it's nice to get the opportunity to use it for some larger scale products.
David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: What is you guys first real-world scala program?


On Mon, Nov 16, 2009 at 10:21 PM, linjie nie <nielinjie@gmail.com> wrote:
Hi list -
I have been studying scala for weeks, and coded some toys in it. But
when I am going to do a real-world coding, I always chose Java/Groovy.
So, could you guys please, share here what is your first real-world
scala programe, and why you chose scala for this programe?

CircleShare and Lift: http://blog.lostlake.org/index.php?/archives/45-A-real-world-use-of-lift.html#extended

And yes, some Java-isms still lurk (abound) in Lift's Mapper classes. ;-)
 

Thanks a lot.



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics
Bruno Woltzenlo...
Joined: 2009-10-04,
User offline. Last seen 42 years 45 weeks ago.
Extractors as Types

Dear scala-users,

 

I would like to be able to use extractors as types. It sounds crazy and it looks like it goes against static typing, but it would make my life much easier. I will clarify below what I mean.

 

Normally, extractors are used for pattern matching. So let’s suppose I have the following extractor sketch:

 

object MyExtractorType {

                def unapply(p: MyClass)  = {

                               if ( hasSomeProperty(p) ) then Some( )

                               else None

                }

}

 

 

The following code pattern occurs often when I am programming:

 

def  myFunction(p:MyClass) = a match {

                case MyExtractorType => “do_something”

                case _ => throw new IllegalArgumentException()

}

 

 

I would like to be able to write this instead:

 

def myFunction(p:MyExtractorType) = “do_something”

 

 

From one point of view, this is crazy, because it looks like a trick to avoid static typing, since we can only know at runtime whether p is of “type” ‘MyExtractorType’ (i.e. whether p matches the extractor ‘MyExtractorType’).  But from another point of view, it seems that it would be relatively easy for the scala compiler to infer that the static type of ‘myFunction’ is actually ‘MyClass’, just by inspecting the type of the argument of the ‘unapply’ method in the ‘MyExtractorType’ object.

 

 

Essentially, I want to be able to use extractors not only for pattern matching, but also for simulating a kind of dynamic subtyping based on the logical principle of comprehension (e.g. I see the ‘MyExtractorType’ as a dynamic subtype of ‘MyClass’, comprehending all instances j of ‘MyClass’ that have ‘hasSomeProperty(j) == true’ ). It would work like an even more expressive and dynamic kind of duck typing. And as I argued above, it seems that it wouldn’t be difficult for Scala to allow this and still remain a statically typed language.

 

So, what I would like to know from the scala community is:

1)      Is there already some feature of Scala that allows me to do something like this?

2)      Is my idea actually a crazy non-sense? If yes, would some of you be patient enough to explain me why?

3)      Can I hope that a feature like this will be added to Scala in the future?

 

Best regards,

 

Bruno

 

--------------------------------

Bruno Woltzenlogel Paleo

Website: http://www.logic.at/people/bruno/

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: What is you guys first real-world scala program?
I rewrote our "message-oriented-middleware" and relayered the whole design of our appliance as a prototype in new architectures to move towards.    We still use this in our "performance testing" suite.

Then I did a whole ton of junk on github.   Now... I try to focus on things i can maintain ;)

On Tue, Nov 17, 2009 at 1:21 AM, linjie nie <nielinjie@gmail.com> wrote:
Hi list -
I have been studying scala for weeks, and coded some toys in it. But
when I am going to do a real-world coding, I always chose Java/Groovy.
So, could you guys please, share here what is your first real-world
scala programe, and why you chose scala for this programe?

Thanks a lot.

Dave Griffith
Joined: 2009-01-14,
User offline. Last seen 42 years 45 weeks ago.
Re: Extractors as Types

Google for "pattern calculus" or "first class patterns". I'm pretty sure
there are no shipping languages that implement anything like this, but you
can at least see the (limited) academic work that has been done along these
lines.

--Dave Griffith

Meredith Gregory
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Extractors as Types
Dear Bruno,
To this i would that Barry Jay has implemented a language based on his pattern calculus.
Best wishes,
--greg

On Wed, Nov 18, 2009 at 6:01 PM, Dave Griffith <dave.l.griffith@gmail.com> wrote:

Google for "pattern calculus" or "first class patterns".  I'm pretty sure
there are no shipping languages that implement anything like this, but you
can at least see the (limited) academic work that has been done along these
lines.

--Dave Griffith

--
View this message in context: http://old.nabble.com/What-is-you-guys-first-real-world-scala-program--tp26384988p26419180.html
Sent from the Scala - User mailing list archive at Nabble.com.




--
L.G. Meredith
Managing Partner
Biosimilarity LLC
1219 NW 83rd St
Seattle, WA 98117

+1 206.650.3740

http://biosimilarity.blogspot.com
bjohanns
Joined: 2009-10-23,
User offline. Last seen 1 year 37 weeks ago.
Re: What is you guys first real-world scala program?

Hi list,

as I discovered scala recently I decided to try my hands directly at a "real
world" task. It was just for fun in itself - but I went for something useful
(in my context) not just another "hello world".

My little project: develop a simple readonly GUI client for the alfresco
content management system (using its CMIS/REST protocoll) that works much like
the webdav/explorer view. I did this, because the webdav/explorer combination
has some downsides and I wanted to try an alternative.

My tool chain: Scala 2.7 / JEdit / SBT

And I have to confess: It worked like a charm.
Here my top points:
- the XML-capabilities of scala are great for parsing the XML-responses
- pattern matching is a great utility (perhaps I overused it but I like it)
- actors are a ! thing concerning GUI development (command-work-update-cycle)
- (tuples) are another favorite, how could I have lived without it?
- Option[]

Things I used - but rather because I wanted to try them - not because I needed
them firsthand:

- traits for logging (there are other and better solutions)
- a little functional stuff (map / filter)

For some things I had to fall back to java libs - but that was none of a
headache either, because everything turned out fine:

- swing JTree - I did not find any support for them in the scala world, so I
had to carve my own code
- URI/URL/HttpURLConnection - I just used them along with some Java Stream-
Stuff because it was the obvious thing to do
- java desktop support

And here some gotchas:
- confused about scala 2.8 I tried to begin with scala 2.8 (as scala 2.7 felt
like "superseeded"). But it was a nightmare, I couldn't get a consistent tool
chain up and running, so I abandoned this in favour of 2.7 which was the right
thing to do (until 2.8 is declared stable of course - then I will happily
move)
- actors and exception handling can be confusing :-)
- the eclipse plugin (it has received some polishing in 2.8 - I heard)
- some examples in the scala references (I was more confused then enlightened)

all in all: after getting over the first "oh my, what are they talking about?"
it worked out fine and was even a joy.
I really like scala... now.

Greetings
Bernd

Roland Kuhn
Joined: 2008-12-26,
User offline. Last seen 3 years 14 weeks ago.
Re: Extractors as Types
Hi Bruno,
does something like this fit your needs?
shadowfax:test rkuhn$ cat pattern.scalapackage test
case class MyClass(p : Boolean)
abstract class MyExtractorType
object MyExtractorType extends MyExtractorType {    def unapply(p : MyClass) = if (p.p) Some(MyExtractorType) else None    implicit def imp(p : MyClass) : MyExtractorType = p match {        case MyExtractorType(x) => x        case _ => throw new IllegalArgumentException()    }}
object Pattern {    def myFunction(p : MyExtractorType) = "do_something"    def main(args : Array[String]) {        println(myFunction(MyClass(true)))        println(myFunction(MyClass(false)))    }}
// vim: set ts=4 sw=4 et:shadowfax:test rkuhn$ scala test.Patterndo_somethingjava.lang.IllegalArgumentException        at test.MyExtractorType$.imp(pattern.scala:11)        at test.Pattern$.main(pattern.scala:19)        at test.Pattern.main(pattern.scala)        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)        at java.lang.reflect.Method.invoke(Method.java:597)        at scala.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:54)        at scala.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:21)        at scala.util.URLClassLoader.asContext(ScalaClassLoader.scala:58)        at scala.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:54)        at scala.util.URLClassLoader.run(ScalaClassLoader.scala:58)        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:154)        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Regards,
Roland
On Nov 19, 2009, at 01:44 , Bruno Woltzenlogel Paleo wrote:
Dear scala-users, I would like to be able to use extractors as types. It sounds crazy and it looks like it goes against static typing, but it would make my life much easier. I will clarify below what I mean. Normally, extractors are used for pattern matching. So let’s suppose I have the following extractor sketch: object MyExtractorType {                def unapply(p: MyClass)  = {                               if ( hasSomeProperty(p) ) then Some( )                               else None                }}  The following code pattern occurs often when I am programming: def  myFunction(p:MyClass) = a match {                case MyExtractorType => “do_something”                case _ => throw new IllegalArgumentException()}  I would like to be able to write this instead: def myFunction(p:MyExtractorType) = “do_something”  From one point of view, this is crazy, because it looks like a trick to avoid static typing, since we can only know at runtime whether p is of “type” ‘MyExtractorType’ (i.e. whether p matches the extractor ‘MyExtractorType’).  But from another point of view, it seems that it would be relatively easy for the scala compiler to infer that the static type of ‘myFunction’ is actually ‘MyClass’, just by inspecting the type of the argument of the ‘unapply’ method in the ‘MyExtractorType’ object.  Essentially, I want to be able to use extractors not only for pattern matching, but also for simulating a kind of dynamic subtyping based on the logical principle of comprehension (e.g. I see the ‘MyExtractorType’ as a dynamic subtype of ‘MyClass’, comprehending all instances j of ‘MyClass’ that have ‘hasSomeProperty(j) == true’ ). It would work like an even more expressive and dynamic kind of duck typing. And as I argued above, it seems that it wouldn’t be difficult for Scala to allow this and still remain a statically typed language. So, what I would like to know from the scala community is:1)      Is there already some feature of Scala that allows me to do something like this?2)      Is my idea actually a crazy non-sense? If yes, would some of you be patient enough to explain me why?3)      Can I hope that a feature like this will be added to Scala in the future? Best regards, Bruno --------------------------------Bruno Woltzenlogel PaleoWebsite: http://www.logic.at/people/bruno/

--Simplicity and elegance are unpopular because they require hard work and discipline to achieve and education to be appreciated.
  -- Dijkstra
Erik Engbrecht
Joined: 2008-12-19,
User offline. Last seen 3 years 18 weeks ago.
Re: What is you guys first real-world scala program?
The first (and currently only, unfortunately) "real" (meaning I was paid to do it) Scala program I wrote analyzed the structure of XML data.  Basically I was loaned to a project that had gigabytes upon gigabytes of authored XML (meaning people make it) in conformance with a very general industry standard.  There was also a software team developing a web application to browse, search, etc all the XML that only supported an undocumented subset of the industry standard.
So I wrote a Scala program that would generate statistics regarding the constructs being used within the entire set of documents, suggest alternations to the DTDs, and identify usage of anomalous constructs or absence of general present but not required ones.
On Tue, Nov 17, 2009 at 1:21 AM, linjie nie <nielinjie@gmail.com> wrote:
Hi list -
I have been studying scala for weeks, and coded some toys in it. But
when I am going to do a real-world coding, I always chose Java/Groovy.
So, could you guys please, share here what is your first real-world
scala programe, and why you chose scala for this programe?

Thanks a lot.



--
http://erikengbrecht.blogspot.com/
Pavol Vaskovic
Joined: 2009-11-10,
User offline. Last seen 42 years 45 weeks ago.
Re: What is you guys first real-world scala program?
My first real scala program was wapping juxy (XSLT test framefork in java, http://juxy.tigris.org/) as matcher for use with specs (http://code.google.com/p/specs/). This allowed us to use BDD (behavior driven development) to develop XSLT based data import solution. Use of XML literals directly in specs was excellent improvement over test XMLs lying around the test suite.

Then we went on and built command line tools to perform single and batch transformations, while integrating with various java libraries. Using pattern matching to handle command line parameters was pleasantly straightforward and elegant.

At the moment we are using parser combinators to process postal addresses in semi-structured format (oh, the joy of cleaning up real world data).

I personally enjoy scala's conciseness and expressiveness. My colleagues do sometimes complain about too flexible syntax...

Regards
Pavol Vaskovic
Bruno Woltzenlo...
Joined: 2009-10-04,
User offline. Last seen 42 years 45 weeks ago.
RE: Extractors as Types

Hi Roland,

 

--------------

does something like this fit your needs?

--------------

 

It seems it does. Thanks!!

 

Although I would of course prefer to have Scala supporting it more automatically, without the need to do the trick with abstract classes and implicit converters.

 

There is something I don’t understand about the code. It’s probably just a basic question about implicit converters:

 

When you call “myFunction(MyClass(true))”, how does Scala know that it has to apply the implicit converter “imp” that was defined inside the “MyExtractorType” object? I thought that Scala could only use implicit converters that have been defined in the current namespace or that have been imported into it, which is not the case for “imp”. I thought I would have to do a “import MyExtractorType._” before Scala could use its implicit converter. Why is this not the case? Why don’t I need to do this import? Is Scala following a convention to look for implicit converters inside objects with the same name of the type that should be returned by the converter? Or is it following some other convention?

 

Best regards,

 

Bruno

 

From: Roland Kuhn [mailto:rk@rkuhn.info]
Sent: Donnerstag, 19. November 2009 09:33
To: Bruno Woltzenlogel Paleo
Cc: scala-user@listes.epfl.ch
Subject: Re: [scala-user] Extractors as Types

 

Hi Bruno,

 

does something like this fit your needs?

 

shadowfax:test rkuhn$ cat pattern.scala

package test

 

case class MyClass(p : Boolean)

 

abstract class MyExtractorType

 

object MyExtractorType extends MyExtractorType {

    def unapply(p : MyClass) = if (p.p) Some(MyExtractorType) else None

    implicit def imp(p : MyClass) : MyExtractorType = p match {

        case MyExtractorType(x) => x

        case _ => throw new IllegalArgumentException()

    }

}

 

object Pattern {

    def myFunction(p : MyExtractorType) = "do_something"

    def main(args : Array[String]) {

        println(myFunction(MyClass(true)))

        println(myFunction(MyClass(false)))

    }

}

 

// vim: set ts=4 sw=4 et:

shadowfax:test rkuhn$ scala test.Pattern

do_something

java.lang.IllegalArgumentException

        at test.MyExtractorType$.imp(pattern.scala:11)

        at test.Pattern$.main(pattern.scala:19)

        at test.Pattern.main(pattern.scala)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at scala.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:54)

        at scala.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:21)

        at scala.util.URLClassLoader.asContext(ScalaClassLoader.scala:58)

        at scala.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:54)

        at scala.util.URLClassLoader.run(ScalaClassLoader.scala:58)

        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:154)

        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

 

Regards,

 

Roland

 

On Nov 19, 2009, at 01:44 , Bruno Woltzenlogel Paleo wrote:



Dear scala-users,

 

I would like to be able to use extractors as types. It sounds crazy and it looks like it goes against static typing, but it would make my life much easier. I will clarify below what I mean.

 

Normally, extractors are used for pattern matching. So let’s suppose I have the following extractor sketch:

 

object MyExtractorType {

                def unapply(p: MyClass)  = {

                               if ( hasSomeProperty(p) ) then Some( )

                               else None

                }

}

 

 

The following code pattern occurs often when I am programming:

 

def  myFunction(p:MyClass) = a match {

                case MyExtractorType => “do_something”

                case _ => throw new IllegalArgumentException()

}

 

 

I would like to be able to write this instead:

 

def myFunction(p:MyExtractorType) = “do_something”

 

 

From one point of view, this is crazy, because it looks like a trick to avoid static typing, since we can only know at runtime whether p is of “type” ‘MyExtractorType’ (i.e. whether p matches the extractor ‘MyExtractorType’).  But from another point of view, it seems that it would be relatively easy for the scala compiler to infer that the static type of ‘myFunction’ is actually ‘MyClass’, just by inspecting the type of the argument of the ‘unapply’ method in the ‘MyExtractorType’ object.

 

 

Essentially, I want to be able to use extractors not only for pattern matching, but also for simulating a kind of dynamic subtyping based on the logical principle of comprehension (e.g. I see the ‘MyExtractorType’ as a dynamic subtype of ‘MyClass’, comprehending all instances j of ‘MyClass’ that have ‘hasSomeProperty(j) == true’ ). It would work like an even more expressive and dynamic kind of duck typing. And as I argued above, it seems that it wouldn’t be difficult for Scala to allow this and still remain a statically typed language.

 

So, what I would like to know from the scala community is:

1)      Is there already some feature of Scala that allows me to do something like this?

2)      Is my idea actually a crazy non-sense? If yes, would some of you be patient enough to explain me why?

3)      Can I hope that a feature like this will be added to Scala in the future?

 

Best regards,

 

Bruno

 

--------------------------------

Bruno Woltzenlogel Paleo

Website: http://www.logic.at/people/bruno/

 

--

Simplicity and elegance are unpopular because they require hard work and discipline to achieve and education to be appreciated.
  -- Dijkstra

 

Bruno Woltzenlo...
Joined: 2009-10-04,
User offline. Last seen 42 years 45 weeks ago.
RE: Extractors as Types

Hi,

 

Firstly, my thanks to Dave and Greg for the replies. The pattern calculus is really interesting. It really gives a nice solid theoretical background to the rough ideas that I was having about how I would like to be programming.

I like how patterns play the central role in the pattern calculus. And this leads me to another question about Scala: are patterns just a nice ad-hoc feature in Scala? Or do they also play a more basic role in the foundations of Scala?

 

Secondly, I would like to leave this as a suggestion for Scala developers: I think a small step (as I explained in my original question) could allow Scala to let users define “functions” that are not functions in the sense of being lambda abstractions in typed lambda calculus, but are rather functions in the sense of being ‘case’ abstractions in the pure pattern calculus.

 

Best regards,

 

Bruno

 

--------------------------------

Bruno Woltzenlogel Paleo

Website: http://www.logic.at/people/bruno/

 

 

From: Meredith Gregory [mailto:lgreg.meredith@gmail.com]
Sent: Donnerstag, 19. November 2009 03:36
To: Dave Griffith
Cc: scala-user@listes.epfl.ch
Subject: Re: [scala-user] Extractors as Types

 

Dear Bruno,

 

To this i would that Barry Jay has implemented a language based on his pattern calculus.

 

Best wishes,

 

--greg

On Wed, Nov 18, 2009 at 6:01 PM, Dave Griffith <dave.l.griffith@gmail.com> wrote:


Google for "pattern calculus" or "first class patterns".  I'm pretty sure
there are no shipping languages that implement anything like this, but you
can at least see the (limited) academic work that has been done along these
lines.

--Dave Griffith

--
View this message in context: http://old.nabble.com/What-is-you-guys-first-real-world-scala-program--tp26384988p26419180.html
Sent from the Scala - User mailing list archive at Nabble.com.




boisvert
Joined: 2009-11-11,
User offline. Last seen 38 weeks 5 days ago.
Re: Extractors as Types
Here's another take that doesn't involve implicits and minimizes the method definition syntax:

trait Extractor[A, B] {
  def unapply(a: A): Option[B]
  def apply[T](f: B => T): A => T =
    (a: A) => unapply(a).map(f(_)).getOrElse { throw new IllegalArgumentException(this.getClass.getSimpleName) }
}

case class MyClass(p : Boolean)

object MyExtractorType extends Extractor[MyClass, MyClass] {
  def unapply(p: MyClass) = if (p.p) Some(p) else None
}

object Pattern {
  def myFunction = MyExtractorType { p =>
    "do something"
  }
 
  def main(args : Array[String]) {
    println(myFunction(MyClass(true)))
    println(myFunction(MyClass(false)))
  }
}

alex

On Sat, Nov 28, 2009 at 9:55 AM, Bruno Woltzenlogel Paleo <bruno.wp.mailinglist@googlemail.com> wrote:

Hi,

 

Firstly, my thanks to Dave and Greg for the replies. The pattern calculus is really interesting. It really gives a nice solid theoretical background to the rough ideas that I was having about how I would like to be programming.

I like how patterns play the central role in the pattern calculus. And this leads me to another question about Scala: are patterns just a nice ad-hoc feature in Scala? Or do they also play a more basic role in the foundations of Scala?

 

Secondly, I would like to leave this as a suggestion for Scala developers: I think a small step (as I explained in my original question) could allow Scala to let users define “functions” that are not functions in the sense of being lambda abstractions in typed lambda calculus, but are rather functions in the sense of being ‘case’ abstractions in the pure pattern calculus.

 

Best regards,

 

Bruno

 

--------------------------------

Bruno Woltzenlogel Paleo

Website: http://www.logic.at/people/bruno/

 

 

From: Meredith Gregory [mailto:lgreg.meredith@gmail.com]
Sent: Donnerstag, 19. November 2009 03:36
To: Dave Griffith
Cc: scala-user@listes.epfl.ch
Subject: Re: [scala-user] Extractors as Types

 

Dear Bruno,

 

To this i would that Barry Jay has implemented a language based on his pattern calculus.

 

Best wishes,

 

--greg

On Wed, Nov 18, 2009 at 6:01 PM, Dave Griffith <dave.l.griffith@gmail.com> wrote:


Google for "pattern calculus" or "first class patterns".  I'm pretty sure
there are no shipping languages that implement anything like this, but you
can at least see the (limited) academic work that has been done along these
lines.

--Dave Griffith

--
View this message in context: http://old.nabble.com/What-is-you-guys-first-real-world-scala-program--tp26384988p26419180.html
Sent from the Scala - User mailing list archive at Nabble.com.




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