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

How to make system calls?

26 replies
akiezun
Joined: 2009-09-02,
User offline. Last seen 3 years 6 weeks ago.

How to call command-line programs from Scala? For example, I have an external
program I want to invoke from my Scala code. In Perl or Python it's trivial
to easy. In Java, it's an unbelivable mess (if you want to wait for the
command to finish or read the command's output). How does Scala adress this
problem?

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: How to make system calls?

On Mon, Sep 21, 2009 at 12:15:00PM -0700, akiezun wrote:
> How to call command-line programs from Scala? For example, I have an
> external program I want to invoke from my Scala code. In Perl or
> Python it's trivial to easy. In Java, it's an unbelivable mess (if you
> want to wait for the command to finish or read the command's output).
> How does Scala adress this problem?

If you can bear to use secret compiler APIs which may change at any
time, you can do this (in trunk, not 2.7.)

scala> import scala.tools.nsc.io._
import scala.tools.nsc.io._

scala> Process("cat /etc/passwd").iterator filter (_ contains "daemon") toList
res0: List[String] = List(daemon:*:1:1:System Services:/var/root:/usr/bin/false)

Frank Teubler
Joined: 2009-01-22,
User offline. Last seen 3 years 37 weeks ago.
Re: How to make system calls?
akiezun wrote: How to call command-line programs from Scala?
Is this simple enough?

def exec(cmd:String) = Runtime.getRuntime exec cmd

/* result to System.out */
def exec2out(cmd:String) {
&nbsp val process = exec (cmd)
&nbsp val input = process.getInputStream
&nbsp val source = Source fromInputStream input
&nbsp source foreach print
}

View this message in context: Re: How to make system calls?
Sent from the Scala - User mailing list archive at Nabble.com.
hohonuuli
Joined: 2009-08-30,
User offline. Last seen 3 years 9 weeks ago.
Re: How to make system calls?

This is not well documented by Sun but if you make a call using Runtime.getRuntime().exec() you should always get a reference to the Process object that's returned. When you're done with the Process you should either:

  • Explicitly get and close the input, output and error streams for the Process, whether you've used them or not
  • or
  • Call Process.destroy()
If you don't make these calls you will leak resources. Once you start leaking resources you may start to see "IOException: Broken Pipe" errors

On Thu, Sep 24, 2009 at 06:53, Frank Teubler <dft@onlinehome.de> wrote:
akiezun wrote: How to call command-line programs from Scala?
Is this simple enough?

def exec(cmd:String) = Runtime.getRuntime exec cmd

/* result to System.out */
def exec2out(cmd:String) {
&nbsp val process = exec (cmd)
&nbsp val input = process.getInputStream
&nbsp val source = Source fromInputStream input
&nbsp source foreach print
}

View this message in context: Re: How to make system calls?
Sent from the Scala - User mailing list archive at Nabble.com.



--
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Brian Schlining
bschlining@gmail.com
akiezun
Joined: 2009-09-02,
User offline. Last seen 3 years 6 weeks ago.
Re: How to make system calls?
Well, this ignores the fact that you need to wait for the process to finish, the process may write to its stdout and stderr which you need to read immediately in separate threads, and you need to wait for exit value, etc. A good list of things that you need to worry about is here: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html In Java, I wrote a big class that handles all of that. I was hoping in Scala this is somehow easier. ./adam Frank Teubler wrote: > > > akiezun wrote: >> >> How to call command-line programs from Scala? >> > > Is this simple enough?
>
> > def exec(cmd:String) = Runtime.getRuntime exec cmd
>
> /* result to System.out */
> def exec2out(cmd:String) {
> > &nbsp val process = exec (cmd)
> &nbsp val input = process.getInputStream
> &nbsp val source = Source fromInputStream input
> &nbsp source foreach print
> }
>
>
Detering Dirk
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
RE: How to make system calls?

> In Java, I wrote a big class that handles all of that. I was
> hoping in Scala this is somehow easier.

It is: In Scala you can write a less big class for that ;-)

Saying that: Your "big class" seems to be exactly what should
exist as API in form of a scala library. Eager providing such?
I would be interested in that too...

KR
Det

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: How to make system calls?

On Thu, Sep 24, 2009 at 08:27:05AM -0700, akiezun wrote:
> A good list of things that you need to worry about is here:
> http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
> In Java, I wrote a big class that handles all of that. I was hoping in Scala
> this is somehow easier.

It's not handling 100% of these issues yet (I would just have to finish
it, it's close) but you can use the secret undocumented compiler API.

import scala.tools.nsc.io._
scala> scala.tools.nsc.io.Process("cat /etc/passwd | wc") foreach println
65 185 3667

Mark Harrah
Joined: 2008-12-18,
User offline. Last seen 35 weeks 3 days ago.
Re: How to make system calls?
On Thursday 24 September 2009 11:27, akiezun wrote: > > Well, this ignores the fact that you need to wait for the process to finish, > the process may write to its stdout and stderr which you need to read > immediately in separate threads, and you need to wait for exit value, etc. > A good list of things that you need to worry about is here: > http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html > In Java, I wrote a big class that handles all of that. I was hoping in Scala > this is somehow easier. There is a Process API [1] in sbt that I have thought about making a separate library. If this is of interest to anyone, I could split it out to github or somewhere. -Mark [1] http://code.google.com/p/simple-build-tool/wiki/Process > ./adam > > > Frank Teubler wrote: > > > > > > akiezun wrote: > >> > >> How to call command-line programs from Scala? > >> > > > > Is this simple enough?
> >
> > > > def exec(cmd:String) = Runtime.getRuntime exec cmd
> >
> > /* result to System.out */
> > def exec2out(cmd:String) {
> > > > &nbsp val process = exec (cmd)
> > &nbsp val input = process.getInputStream
> > &nbsp val source = Source fromInputStream input
> > &nbsp source foreach print
> > }
> >
> > >
Tim Perrett 2
Joined: 2009-04-06,
User offline. Last seen 42 years 45 weeks ago.
Re: How to make system calls?
+1 Id be interested in that process library. Cheers, Tim On Thu, Sep 24, 2009 at 6:34 PM, Mark Harrah wrote: > On Thursday 24 September 2009 11:27, akiezun wrote: >> >> Well, this ignores the fact that you need to wait for the process to finish, >> the process may write to its stdout and stderr which you need to read >> immediately in separate threads, and you need to wait for exit value, etc. >> A good list of things that you need to worry about is here: >> http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html >> In Java, I wrote a big class that handles all of that. I was hoping in Scala >> this is somehow easier. > > There is a Process API [1] in sbt that I have thought about making a separate library.  If this is of interest to anyone, I could split it out to github or somewhere. > > -Mark > > [1] http://code.google.com/p/simple-build-tool/wiki/Process > >> ./adam >> >> >> Frank Teubler wrote: >> > >> > >> > akiezun wrote: >> >> >> >> How to call command-line programs from Scala? >> >> >> > >> > Is this simple enough?
>> >
>> > >> > def exec(cmd:String) =  Runtime.getRuntime exec cmd
>> >
>> > /* result to System.out */
>> > def exec2out(cmd:String) {
>> > >> >     &nbsp val process = exec (cmd)
>> >     &nbsp val input   = process.getInputStream
>> >     &nbsp val source  = Source fromInputStream input
>> >     &nbsp source foreach print
>> > }
>> >
>> > >> >
Alì Panick
Joined: 2009-08-01,
User offline. Last seen 42 years 45 weeks ago.
Re: How to make system calls?
I'm interested too. If you want, I'd like to help you. # AliPanick Sent from my iPhone On 24/set/2009, at 20.37, Tim Perrett wrote: > +1 > > Id be interested in that process library. > > Cheers, Tim > > On Thu, Sep 24, 2009 at 6:34 PM, Mark Harrah wrote: >> On Thursday 24 September 2009 11:27, akiezun wrote: >>> >>> Well, this ignores the fact that you need to wait for the process >>> to finish, >>> the process may write to its stdout and stderr which you need to >>> read >>> immediately in separate threads, and you need to wait for exit >>> value, etc. >>> A good list of things that you need to worry about is here: >>> http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html >>> In Java, I wrote a big class that handles all of that. I was >>> hoping in Scala >>> this is somehow easier. >> >> There is a Process API [1] in sbt that I have thought about making >> a separate library. If this is of interest to anyone, I could >> split it out to github or somewhere. >> >> -Mark >> >> [1] http://code.google.com/p/simple-build-tool/wiki/Process >> >>> ./adam >>> >>> >>> Frank Teubler wrote: >>>> >>>> >>>> akiezun wrote: >>>>> >>>>> How to call command-line programs from Scala? >>>>> >>>> >>>> Is this simple enough?
>>>>
>>>> >>>> def exec(cmd:String) = Runtime.getRuntime exec cmd
>>>>
>>>> /* result to System.out */
>>>> def exec2out(cmd:String) {
>>>> >>>> &nbsp val process = exec (cmd)
>>>> &nbsp val input = process.getInputStream
>>>> &nbsp val source = Source fromInputStream input
>>>> &nbsp source foreach print
>>>> }
>>>>
>>>> >>> >>
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: How to make system calls?
I'll quote myself on that page:   Comment by dcsobral, Jul 28, 2009

God, I want this on the standard library!

On Thu, Sep 24, 2009 at 2:34 PM, Mark Harrah <harrah@bu.edu> wrote:
On Thursday 24 September 2009 11:27, akiezun wrote:
>
> Well, this ignores the fact that you need to wait for the process to finish,
> the process may write to its stdout and stderr which you need to read
> immediately in separate threads, and you need to wait for exit value, etc.
> A good list of things that you need to worry about is here:
> http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
> In Java, I wrote a big class that handles all of that. I was hoping in Scala
> this is somehow easier.

There is a Process API [1] in sbt that I have thought about making a separate library.  If this is of interest to anyone, I could split it out to github or somewhere.

-Mark

[1] http://code.google.com/p/simple-build-tool/wiki/Process

> ./adam
>
>
> Frank Teubler wrote:
> >
> >
> > akiezun wrote:
> >>
> >> How to call command-line programs from Scala?
> >>
> >
> > Is this simple enough?<br>
> > <br>
> > <tt>
> > def exec(cmd:String) =  Runtime.getRuntime exec cmd<br>
> > <br>
> > /* result to System.out */<br>
> > def exec2out(cmd:String) {<br>
> >
> >     &nbsp val process = exec (cmd)<br>
> >     &nbsp val input   = process.getInputStream<br>
> >     &nbsp val source  = Source fromInputStream input<br>
> >     &nbsp source foreach print<br>
> > }<br>
> > </tt>
> >
>



--
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.
Mark Harrah
Joined: 2008-12-18,
User offline. Last seen 35 weeks 3 days ago.
Re: How to make system calls?
This is now at http://github.com/harrah/process and available from scala-tools.org as: "org.scala-tools.sbt" % "process" % "0.1" -Mark On Thursday 24 September 2009, Alì Panick wrote: > I'm interested too. > > If you want, I'd like to help you. > > # AliPanick > Sent from my iPhone > > On 24/set/2009, at 20.37, Tim Perrett wrote: > > +1 > > > > Id be interested in that process library. > > > > Cheers, Tim > > > > On Thu, Sep 24, 2009 at 6:34 PM, Mark Harrah wrote: > >> On Thursday 24 September 2009 11:27, akiezun wrote: > >>> Well, this ignores the fact that you need to wait for the process > >>> to finish, > >>> the process may write to its stdout and stderr which you need to > >>> read > >>> immediately in separate threads, and you need to wait for exit > >>> value, etc. > >>> A good list of things that you need to worry about is here: > >>> http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html > >>> In Java, I wrote a big class that handles all of that. I was > >>> hoping in Scala > >>> this is somehow easier. > >> > >> There is a Process API [1] in sbt that I have thought about making > >> a separate library. If this is of interest to anyone, I could > >> split it out to github or somewhere. > >> > >> -Mark > >> > >> [1] http://code.google.com/p/simple-build-tool/wiki/Process > >> > >>> ./adam > >>> > >>> Frank Teubler wrote: > >>>> akiezun wrote: > >>>>> How to call command-line programs from Scala? > >>>> > >>>> Is this simple enough?
> >>>>
> >>>> > >>>> def exec(cmd:String) = Runtime.getRuntime exec cmd
> >>>>
> >>>> /* result to System.out */
> >>>> def exec2out(cmd:String) {
> >>>> > >>>> &nbsp val process = exec (cmd)
> >>>> &nbsp val input = process.getInputStream
> >>>> &nbsp val source = Source fromInputStream input
> >>>> &nbsp source foreach print
> >>>> }
> >>>>
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: How to make system calls?
Hurray!

On Thu, Oct 8, 2009 at 8:37 PM, Mark Harrah <harrah@bu.edu> wrote:
This is now at http://github.com/harrah/process and available from
scala-tools.org as:

 "org.scala-tools.sbt" % "process" % "0.1"

-Mark


On Thursday 24 September 2009, Alì Panick wrote:
> I'm interested too.
>
> If you want, I'd like to help you.
>
>   # AliPanick
> Sent from my iPhone
>
> On 24/set/2009, at 20.37, Tim Perrett <tperrett@googlemail.com> wrote:
> > +1
> >
> > Id be interested in that process library.
> >
> > Cheers, Tim
> >
> > On Thu, Sep 24, 2009 at 6:34 PM, Mark Harrah <harrah@bu.edu> wrote:
> >> On Thursday 24 September 2009 11:27, akiezun wrote:
> >>> Well, this ignores the fact that you need to wait for the process
> >>> to finish,
> >>> the process may write to its stdout and stderr which you need to
> >>> read
> >>> immediately in separate threads, and you need to wait for exit
> >>> value, etc.
> >>> A good list of things that you need to worry about is here:
> >>> http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
> >>> In Java, I wrote a big class that handles all of that. I was
> >>> hoping in Scala
> >>> this is somehow easier.
> >>
> >> There is a Process API [1] in sbt that I have thought about making
> >> a separate library.  If this is of interest to anyone, I could
> >> split it out to github or somewhere.
> >>
> >> -Mark
> >>
> >> [1] http://code.google.com/p/simple-build-tool/wiki/Process
> >>
> >>> ./adam
> >>>
> >>> Frank Teubler wrote:
> >>>> akiezun wrote:
> >>>>> How to call command-line programs from Scala?
> >>>>
> >>>> Is this simple enough?<br>
> >>>> <br>
> >>>> <tt>
> >>>> def exec(cmd:String) =  Runtime.getRuntime exec cmd<br>
> >>>> <br>
> >>>> /* result to System.out */<br>
> >>>> def exec2out(cmd:String) {<br>
> >>>>
> >>>>     &nbsp val process = exec (cmd)<br>
> >>>>     &nbsp val input   = process.getInputStream<br>
> >>>>     &nbsp val source  = Source fromInputStream input<br>
> >>>>     &nbsp source foreach print<br>
> >>>> }<br>
> >>>> </tt>





--
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.
Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: How to make system calls?
This should help feed the budding I/O Library....

On Thu, Oct 8, 2009 at 7:37 PM, Mark Harrah <harrah@bu.edu> wrote:
This is now at http://github.com/harrah/process and available from
scala-tools.org as:

 "org.scala-tools.sbt" % "process" % "0.1"

-Mark


On Thursday 24 September 2009, Alì Panick wrote:
> I'm interested too.
>
> If you want, I'd like to help you.
>
>   # AliPanick
> Sent from my iPhone
>
> On 24/set/2009, at 20.37, Tim Perrett <tperrett@googlemail.com> wrote:
> > +1
> >
> > Id be interested in that process library.
> >
> > Cheers, Tim
> >
> > On Thu, Sep 24, 2009 at 6:34 PM, Mark Harrah <harrah@bu.edu> wrote:
> >> On Thursday 24 September 2009 11:27, akiezun wrote:
> >>> Well, this ignores the fact that you need to wait for the process
> >>> to finish,
> >>> the process may write to its stdout and stderr which you need to
> >>> read
> >>> immediately in separate threads, and you need to wait for exit
> >>> value, etc.
> >>> A good list of things that you need to worry about is here:
> >>> http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
> >>> In Java, I wrote a big class that handles all of that. I was
> >>> hoping in Scala
> >>> this is somehow easier.
> >>
> >> There is a Process API [1] in sbt that I have thought about making
> >> a separate library.  If this is of interest to anyone, I could
> >> split it out to github or somewhere.
> >>
> >> -Mark
> >>
> >> [1] http://code.google.com/p/simple-build-tool/wiki/Process
> >>
> >>> ./adam
> >>>
> >>> Frank Teubler wrote:
> >>>> akiezun wrote:
> >>>>> How to call command-line programs from Scala?
> >>>>
> >>>> Is this simple enough?<br>
> >>>> <br>
> >>>> <tt>
> >>>> def exec(cmd:String) =  Runtime.getRuntime exec cmd<br>
> >>>> <br>
> >>>> /* result to System.out */<br>
> >>>> def exec2out(cmd:String) {<br>
> >>>>
> >>>>     &nbsp val process = exec (cmd)<br>
> >>>>     &nbsp val input   = process.getInputStream<br>
> >>>>     &nbsp val source  = Source fromInputStream input<br>
> >>>>     &nbsp source foreach print<br>
> >>>> }<br>
> >>>> </tt>



phkoester
Joined: 2009-08-23,
User offline. Last seen 42 years 45 weeks ago.
Newbie's Questions About Functions With Placeholders

I'm working my way through "Programming in Scala" to learn this exciting
new language and am about confused about the usage of placeholders (`_')
in functions.

I declare a list like so:

val list = List(1, 2, 3, 4, 5)

Then I use a filter function `f':

def f(n: Int): Boolean = {
print(".")
n > 2
}
println(list.filter(f))

The output from the above code is `.....List(3, 4, 5)', where five dots
indicate that function `f' was called five times, which is correct and
what I expected.

Now I try to be more concise, like this:

println(list.filter({
print(".")
_ > 2
}))

Suprisingly, the output is now `.List(3, 4, 5)', indicating that
`println' was called only once! Why is that so?

The following wouldn't even compile:

println(list.filter({
print(".")
// Atempt to store anon param in a val -> compiler error :
// "unbound placeholder parameter"
val n = _
n > 2
}))

Again: Why is this so?

Cheers
---Phil

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Newbie's Questions About Functions With Placeholders


On Sun, Oct 11, 2009 at 8:46 AM, Philip Köster <philip.koester@web.de> wrote:
I'm working my way through "Programming in Scala" to learn this exciting new language and am about confused about the usage of placeholders (`_') in functions.

I declare a list like so:

       val list = List(1, 2, 3, 4, 5)

Then I use a filter function `f':

       def f(n: Int): Boolean = {
               print(".")
               n > 2
       }
       println(list.filter(f))

The output from the above code is `.....List(3, 4, 5)', where five dots indicate that function `f' was called five times, which is correct and what I expected.

Now I try to be more concise, like this:

       println(list.filter({
               print(".")
               _ > 2
       }))

Suprisingly, the output is now `.List(3, 4, 5)', indicating that `println' was called only once! Why is that so?

The following wouldn't even compile:

       println(list.filter({
               print(".")
               // Atempt to store anon param in a val -> compiler error :
               // "unbound placeholder parameter"
               val n = _
               n > 2
       }))

Again: Why is this so?

{ println("."); _ > 2 } becomes an initializer block that returns a (Int) => Boolean
And Filter expects a (Int) => Boolean, so it runs println(".") once, obtains the reference to the function and passes it to filter.
 

Cheers
---Phil



--
Viktor Klang

Blog: klangism.blogspot.com
Twttr: viktorklang

Lift Committer - liftweb.com
AKKA Committer - akkasource.org
Cassidy - github.com/viktorklang/Cassidy.git
SoftPub founder: http://groups.google.com/group/softpub
Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Newbie's Questions About Functions With Placeholders

Perhaps a simpler example would help.

val x = {
println("Hello")
() => 5
}

println(x() + x())

Hello gets printed once, during the assignment to x.

Only in a def does a { automatically start a thunk. Elsewhere you'll
need a => to do it.

2009/10/11 Philip Köster :
> I'm working my way through "Programming in Scala" to learn this exciting new
> language and am about confused about the usage of placeholders (`_') in
> functions.
>
> I declare a list like so:
>
>        val list = List(1, 2, 3, 4, 5)
>
> Then I use a filter function `f':
>
>        def f(n: Int): Boolean = {
>                print(".")
>                n > 2
>        }
>        println(list.filter(f))
>
> The output from the above code is `.....List(3, 4, 5)', where five dots
> indicate that function `f' was called five times, which is correct and what
> I expected.
>
> Now I try to be more concise, like this:
>
>        println(list.filter({
>                print(".")
>                _ > 2
>        }))
>
> Suprisingly, the output is now `.List(3, 4, 5)', indicating that `println'
> was called only once! Why is that so?
>
> The following wouldn't even compile:
>
>        println(list.filter({
>                print(".")
>                // Atempt to store anon param in a val -> compiler error :
>                // "unbound placeholder parameter"
>                val n = _
>                n > 2
>        }))
>
> Again: Why is this so?
>
> Cheers
> ---Phil
>

phkoester
Joined: 2009-08-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Newbie's Questions About Functions With Placeholders

> Perhaps a simpler example would help.
>
> val x = {
> println("Hello")
> () => 5
> }
>
> println(x() + x())

Dear Viktor and Ricky,

I think I got your point ... well ... almost. I still find it a little
tricky that

list.filter(n => { print("."); n > 2 })

behaves differently from

list.filter({ print("."); _ > 2 })

Maybe the lesson to learn here is that if I have more than one
expression, I always need the `=>'. Is that correct?

Thanks for your help.

---Ph.

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Newbie's Questions About Functions With Placeholders

{ print("."); _ > 2 } is the same as:
{ print("."); n => n > 2 }

I.e., print a dot then return a function that tests whether its
argument is greater than 2.

2009/10/11 Philip Köster :
>> Perhaps a simpler example would help.
>>
>> val x = {
>>   println("Hello")
>>   () => 5
>> }
>>
>> println(x() + x())
>
> Dear Viktor and Ricky,
>
> I think I got your point ... well ... almost. I still find it a little
> tricky that
>
>        list.filter(n => { print("."); n > 2 })
>
> behaves differently from
>
>        list.filter({ print("."); _ > 2 })
>
> Maybe the lesson to learn here is that if I have more than one expression, I
> always need the `=>'. Is that correct?
>
> Thanks for your help.
>
> ---Ph.
>

Alefas
Joined: 2009-06-23,
User offline. Last seen 33 weeks 5 days ago.
RE: Newbie's Questions About Functions With Placeholders

Just for joke. It's not correct, that you need =>.
Look at this code (don't use it:)):
list.filter(_ > 2 + ({print("."}; 0}))

Also you can use such syntax:
for {n <- list if n > 2; print(".")} yield n

Best regards,
Alexander Podkhalyuzin.

-----Original Message-----
From: philip.koester@web.de [mailto:philip.koester@web.de]
Sent: Sunday, October 11, 2009 1:06 PM
To: Ricky Clarkson
Cc: scala-user@listes.epfl.ch
Subject: Re: [scala-user] Newbie's Questions About Functions With
Placeholders

> Perhaps a simpler example would help.
>
> val x = {
> println("Hello")
> () => 5
> }
>
> println(x() + x())

Dear Viktor and Ricky,

I think I got your point ... well ... almost. I still find it a little
tricky that

list.filter(n => { print("."); n > 2 })

behaves differently from

list.filter({ print("."); _ > 2 })

Maybe the lesson to learn here is that if I have more than one
expression, I always need the `=>'. Is that correct?

Thanks for your help.

---Ph.

phkoester
Joined: 2009-08-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Newbie's Questions About Functions With Placeholders

> { print("."); _ > 2 } is the same as:
> { print("."); n => n > 2 }

Okay. Great explanation, thank you.

fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.
Re: How to make system calls?

Le 09/10/2009 01:37, Mark Harrah a écrit :
> This is now at http://github.com/harrah/process and available from
> scala-tools.org as:
>
> "org.scala-tools.sbt" % "process" % "0.1"
>

Hello people, Mark,

I'm seing that the last commit in the sources available on github was on
november, but it seems that no new version of the process jar were
published on scala-tools since the first release.

Is a more recent of the jar available somewhere ?

Thanks,

Mark Harrah
Joined: 2008-12-18,
User offline. Last seen 35 weeks 3 days ago.
Re: How to make system calls?

On Wednesday 10 February 2010 12:54:05 pm Francois wrote:
> Le 09/10/2009 01:37, Mark Harrah a écrit :
> > This is now at http://github.com/harrah/process and available from
> > scala-tools.org as:
> >
> > "org.scala-tools.sbt" % "process" % "0.1"
>
> Hello people, Mark,
>
> I'm seing that the last commit in the sources available on github was on
> november, but it seems that no new version of the process jar were
> published on scala-tools since the first release.
>
> Is a more recent of the jar available somewhere ?

There has only been one commit, which improved setting a different working
directory or environment variables. Do you need that particular change?

-Mark

Blair Zajac
Joined: 2009-01-12,
User offline. Last seen 42 years 45 weeks ago.
Re: How to make system calls?

On 09/21/2009 12:37 PM, Paul Phillips wrote:
> On Mon, Sep 21, 2009 at 12:15:00PM -0700, akiezun wrote:
>> How to call command-line programs from Scala? For example, I have an
>> external program I want to invoke from my Scala code. In Perl or
>> Python it's trivial to easy. In Java, it's an unbelivable mess (if you
>> want to wait for the command to finish or read the command's output).
>> How does Scala adress this problem?
>
> If you can bear to use secret compiler APIs which may change at any
> time, you can do this (in trunk, not 2.7.)
>
> scala> import scala.tools.nsc.io._
> import scala.tools.nsc.io._
>
> scala> Process("cat /etc/passwd").iterator filter (_ contains "daemon") toList
> res0: List[String] = List(daemon:*:1:1:System Services:/var/root:/usr/bin/false)

Cool stuff!

Would you take a patch to overload Process.apply() to take an
Array[String] in addition to a String and so it can be passed commands
directly without invoking 'sh' and it can handle whitespace in arguments?

Regards,
Blair

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: How to make system calls?

On Wed, Feb 10, 2010 at 01:59:29PM -0800, Blair Zajac wrote:
> Would you take a patch to overload Process.apply() to take an
> Array[String] in addition to a String and so it can be passed commands
> directly without invoking 'sh' and it can handle whitespace in
> arguments?

No, but I might point you at Process.exec.

fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.
Re: How to make system calls?

Le 10/02/2010 22:05, Mark Harrah a écrit :
> There has only been one commit, which improved setting a different working
> directory or environment variables. Do you need that particular change?

I don't think so, I didn't see that :) So thanks, I will take the 0.1
release, and if the need appears of that really commit, I will build my
own version (but it seems really unlikely)

Thanks for you answer,

Blair Zajac
Joined: 2009-01-12,
User offline. Last seen 42 years 45 weeks ago.
Re: How to make system calls?

Paul Phillips wrote:
> On Wed, Feb 10, 2010 at 01:59:29PM -0800, Blair Zajac wrote:
>> Would you take a patch to overload Process.apply() to take an
>> Array[String] in addition to a String and so it can be passed commands
>> directly without invoking 'sh' and it can handle whitespace in
>> arguments?
>
> No, but I might point you at Process.exec.

That'll do nicely :)

Blair

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