- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
How to make system calls?
Mon, 2009-09-21, 20:15
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?
Thu, 2009-09-24, 14:57
#2
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) {
  val process = exec (cmd)
  val input = process.getInputStream
  val source = Source fromInputStream input
  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.
Thu, 2009-09-24, 16:17
#3
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()
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) {
  val process = exec (cmd)
  val input = process.getInputStream
  val source = Source fromInputStream input
  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
Thu, 2009-09-24, 16:37
#4
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) {
> >   val process = exec (cmd)
>   val input = process.getInputStream
>   val source = Source fromInputStream input
>   source foreach print
> }
> >
>
> > def exec(cmd:String) = Runtime.getRuntime exec cmd
>
> /* result to System.out */
> def exec2out(cmd:String) {
> >   val process = exec (cmd)
>   val input = process.getInputStream
>   val source = Source fromInputStream input
>   source foreach print
> }
> >
Thu, 2009-09-24, 16:57
#5
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
Thu, 2009-09-24, 17:07
#6
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
Thu, 2009-09-24, 18:37
#7
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) {
> > > >   val process = exec (cmd)
> >   val input = process.getInputStream
> >   val source = Source fromInputStream input
> >   source foreach print
> > }
> > > > >
> >
> > > > def exec(cmd:String) = Runtime.getRuntime exec cmd
> >
> > /* result to System.out */
> > def exec2out(cmd:String) {
> > > >   val process = exec (cmd)
> >   val input = process.getInputStream
> >   val source = Source fromInputStream input
> >   source foreach print
> > }
> > > > >
Thu, 2009-09-24, 19:47
#8
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) {
>> > >> >   val process = exec (cmd)
>> >   val input = process.getInputStream
>> >   val source = Source fromInputStream input
>> >   source foreach print
>> > }
>> > >> > >> >
>> >
>> > >> > def exec(cmd:String) = Runtime.getRuntime exec cmd
>> >
>> > /* result to System.out */
>> > def exec2out(cmd:String) {
>> > >> >   val process = exec (cmd)
>> >   val input = process.getInputStream
>> >   val source = Source fromInputStream input
>> >   source foreach print
>> > }
>> > >> > >> >
Thu, 2009-09-24, 20:17
#9
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) {
>>>> >>>>   val process = exec (cmd)
>>>>   val input = process.getInputStream
>>>>   val source = Source fromInputStream input
>>>>   source foreach print
>>>> }
>>>> >>>> >>> >>
>>>>
>>>> >>>> def exec(cmd:String) = Runtime.getRuntime exec cmd
>>>>
>>>> /* result to System.out */
>>>> def exec2out(cmd:String) {
>>>> >>>>   val process = exec (cmd)
>>>>   val input = process.getInputStream
>>>>   val source = Source fromInputStream input
>>>>   source foreach print
>>>> }
>>>> >>>> >>> >>
Thu, 2009-09-24, 20:27
#10
Re: How to make system calls?
I'll quote myself on that page:
Comment by dcsobral, Jul 28, 2009
--
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.
God, I want this on the standard library!
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>
> >
> >   val process = exec (cmd)<br>
> >   val input = process.getInputStream<br>
> >   val source = Source fromInputStream input<br>
> >   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.
Fri, 2009-10-09, 00:47
#11
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) {
> >>>> > >>>>   val process = exec (cmd)
> >>>>   val input = process.getInputStream
> >>>>   val source = Source fromInputStream input
> >>>>   source foreach print
> >>>> }
> >>>>
> >>>>
> >>>> > >>>> def exec(cmd:String) = Runtime.getRuntime exec cmd
> >>>>
> >>>> /* result to System.out */
> >>>> def exec2out(cmd:String) {
> >>>> > >>>>   val process = exec (cmd)
> >>>>   val input = process.getInputStream
> >>>>   val source = Source fromInputStream input
> >>>>   source foreach print
> >>>> }
> >>>>
Sat, 2009-10-10, 23:47
#12
Re: How to make system calls?
Hurray!
On Thu, Oct 8, 2009 at 8:37 PM, Mark Harrah <harrah@bu.edu> wrote:
--
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.
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>
> >>>>
> >>>>   val process = exec (cmd)<br>
> >>>>   val input = process.getInputStream<br>
> >>>>   val source = Source fromInputStream input<br>
> >>>>   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.
Sat, 2009-10-10, 23:57
#13
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:
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>
> >>>>
> >>>>   val process = exec (cmd)<br>
> >>>>   val input = process.getInputStream<br>
> >>>>   val source = Source fromInputStream input<br>
> >>>>   source foreach print<br>
> >>>> }<br>
> >>>> </tt>
Sun, 2009-10-11, 07:57
#14
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
Sun, 2009-10-11, 09:27
#15
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
Sun, 2009-10-11, 09:37
#16
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
>
Sun, 2009-10-11, 10:07
#17
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.
Sun, 2009-10-11, 10:17
#18
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.
>
Sun, 2009-10-11, 10:27
#19
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.
Sun, 2009-10-11, 10:37
#20
Re: Newbie's Questions About Functions With Placeholders
> { print("."); _ > 2 } is the same as:
> { print("."); n => n > 2 }
Okay. Great explanation, thank you.
Wed, 2010-02-10, 19:07
#21
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,
Wed, 2010-02-10, 22:17
#22
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
Wed, 2010-02-10, 23:07
#23
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
Wed, 2010-02-10, 23:17
#24
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.
Wed, 2010-02-10, 23:27
#25
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,
Thu, 2010-02-11, 00:47
#26
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
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)