unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Shell commands with output to string
@ 2022-02-22  9:29 Zelphir Kaltstahl
  2022-02-22  9:38 ` Zelphir Kaltstahl
  2022-02-22 14:27 ` Olivier Dion via General Guile related discussions
  0 siblings, 2 replies; 19+ messages in thread
From: Zelphir Kaltstahl @ 2022-02-22  9:29 UTC (permalink / raw)
  To: Guile User

Hello Guile users!

How would I run a shell command from inside Guile and get its output as a 
string, instead of the output being outputted directly? (Guile 3.0.8)

So far I have found

~~~~
(system ...)
~~~~

which I tried to use with

~~~~
scheme@(guile-user)> (with-output-to-string
   (system "ls -al"))

;; lots of output immediately shown and not stored in variable

ice-9/boot-9.scm:1685:16: In procedure raise-exception:
Wrong type to apply: 0

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]> ,bt
In ice-9/ports.scm:
     476:4  2 (with-output-to-string 0)
While executing meta-command:
In procedure frame-local-ref: Argument 2 out of range: 1
~~~~

But this does not give me a string back.

I also tried with

~~~~
scheme@(guile-user)> (call-with-values (lambda () (system "ls -al"))
... (lambda (exit-code output) output))

;; lots of output immediately shown

ice-9/boot-9.scm:1685:16: In procedure raise-exception:
Wrong number of values returned to continuation (expected 2)

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.

scheme@(guile-user) [1]> ,bt
In current input:
     10:29  1 (_)
In ice-9/boot-9.scm:
   1685:16  0 (raise-exception _ #:continuable? _)
~~~~

Is there another function I should be using?

I would like to have the exit code and the output of a command.

Best regards,
Zelphir

-- 
repositories: https://notabug.org/ZelphirKaltstahl




^ permalink raw reply	[flat|nested] 19+ messages in thread
* Re: Shell commands with output to string
@ 2022-02-23 17:48 Blake Shaw
  2022-02-23 18:25 ` Olivier Dion via General Guile related discussions
  0 siblings, 1 reply; 19+ messages in thread
From: Blake Shaw @ 2022-02-23 17:48 UTC (permalink / raw)
  To: Olivier Dion via General Guile related discussions
  Cc: Zelphir Kaltstahl, Leo Butler, Olivier Dion

Olivier Dion via General Guile related discussions <guile-user@gnu.org>
writes:

> On Wed, 23 Feb 2022, Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
> wrote:
>
>>> There's ton of missing stuffs in the standard library IMO.  On top
>>> of my
>>> head, filesystem paths manipulation (e.g. path-join) is also one
>>> that is
>>> probably getting re-invented a lots.
>>
Nice work Zelphir!
>> I actually made something for that, trying to copy mostly the Python
>> behavior for os.path.join:
>>
>> https://notabug.org/ZelphirKaltstahl/guile-fslib
>>
>> Also available as a GNU Guix package, but not updated in a while on
>> Guix.  Repository contains more up to date version.
>
> This is great and should be merged into the standard library of Guile.
> Maybe not at it's (I have not read everything), but this would be trully
> benifical to all Guile users.
>
I very much disagree. While it's a large implementation of Scheme, Guile
is still Scheme. This is all really simple stuff, and there are many
ways to implement these functions. Do the Guile maintainers really need
to be burdened by adding trivial functionality to the core library?

This is really simple stuff, but it was nevertheless difficult for me to
figure out how to do much of it in Guile. Many things that confused me
to death ended up being a big "oh, its just that??" Which seems to
indicate that the issue is less with the core library lacking
functionality and more with the presentation of Guile itself -- the
docs, which in December we heard a large number of complaints about.

> By keeping this as an external library such as a Guix package, I fear
> we're aiming at the 'nodejs syndrome' where everything -- down to
> the simplest hello world -- is a package dependency.
>
>>> I believe that the successful story of Python is not just about its
>>> pretty syntax, but also dues to its very large standard library.
>>
>> I think so too. Although I sometimes have the feeling, that Guile does
>> things in a cleaner way, once one figures out how to do them in the
>> first place. One thing I really like are the ports. Stuff like
>> call-with-output-string. Takes some twisting of the brain, but once
>> one gets it, it becomes very useful and elegant.
>
I agree, there is just a mental leap getting to there. I think with
examples based on the "expressive graduation" strategy I outlined in my
Guix Days presentation can lessen that load quite a bit, and get folks
moving with the goods they need to build their own tools quickly.
> Of course Scheme does things better than Python.  I wouldn't be hacking
> with it if it was not the case ^^.  Not only the syntax, but the
> programming paradigms.  You want FP, you got it.  You want OOP, sure.
> You prefer procedural, no problem.  With Python it's more like just OOP
> although there's been progress on the FP side.
>
>> But yes, Python is very beginner friendly in terms of batteries
>> included.  Although I think that its syntax feels a bit ad-hoc. As in
>> "Oh we want some syntax for X … lets invent this keyword here. or some
>> new operators or things like that."
>
> Yeah they were late to the FP paradigm so they had to invent something
> without breaking their OOP echo chamber I guess.  IIRC, there's talks
> about adding patterns matching which Lisp has since the begining.  So
> yeah very late to the game of FP.
>
>> I like Guile syntax (or Scheme in general) much more. However, it is
>> difficult to motivate others in a quick demo to learn the language,
>> when you cannot take half an hour time to explain, what that for other
>> people weird looking syntax is actually really cool.
>
> I'm not sure if it's only the syntax.  Yes we often see "but the
> parentheses burn my eyes" but I believe that things are more complex
> than that.  What I think is difficult is to explain to someone why they
> should try to learn new languages in the first place.  On top of my
> head, people ask themself the following question before learning a new
> language:
>
>       - Profit -> Can I easily find a good paying job by developing in
>         this language?
So why are Haskell and Rust some of the most popular languages today
besides the "big names" (which I don't its reasonable to imagine a scheme
implementation suddenly breaking the mold and acceding to)
>
>       - Field -> Can I develop in my field (e.g. video game, mathematic)
>         with this language?  Is there a community?
Yes and yes to those fields, with great tools! (Haunt, recent vector libs)
But the community, like many a scheme, is a bit hermetic, and this is
based in practices like mailing lists, which I don't think can/will
change. We could start a guile-hackers discourse server, or something
else more public-facing, but would any of the current users join?

So then we are the Day Zero guile crew, and thats gonna be rough to get
going.
>
>       - Support -> Can I find solutions to my problems easily online?
>         Are there libraries for common problems that I could encounter?
This! While I think the Guile website is really nice, I think something
like a guile wiki which catalogues everything in one place, with a firm
organizational strategy from the outset, could be massively beneficial.

Finding whats happening in Guile is labyrinth, and I still discover new
stuff every couple weeks. The ecosystem is immensely larger than I
imagined a few months ago. And also, lots (maybe half?) of of it isn't
even in Guix, and I'm talking about recent projects. Meanwhile much of
what is in Guix builds but doesn't work.
>
>       - Prototyping -> How easy is it for me to scratch an idea with
>         this language?
This too! I think that this is what the Guile-Cookbook discussion is
hoping to solve
>
> and probably other reasons.  Now, try to explain to someone that does
> web developpement in Javascript or Python that Scheme is far more
> supperior.  That person would just ask: Does Guile has support for
> MongoDB?  And proceed to dismiss it.
>
> Of course, some hackers like me just like to learn new stuffs.  It took
> me about an one hour into Scheme course of the MIT on youtube to
> understand that I just found the holy grail of programming language.
> And yet, I have not find the answer to the `profit` question.

I agree with much of what you're saying, but I disagree with the idea of
Guile becoming a "batteries included" Scheme loaded with conveniences.
Andy et al have far more important issues to worry about.

On the other hand, I would be down to organize and contribute to an
effort to create a battery pack, based on a study of the utilities
offered by Python. We saw that Guix is picking up in production use
and such a library would be very helpful (and I think Tropin's RDE
might be trying to accomplish something like this)

There is the existing Guile-lib, which seems to have intended something
similar in the past. Let me briefly comment on its TOC:

> (apicheck)
>   Describe and verify library programming interfaces
So it tells you what a library does? Could be cool,
but not what I'm looking for as a newcomer. 
> (config load)
>   Loading configuration files
of what? where am I getting these configuration files from?
> (container async-queue)
>   A thread-safe message queue
Sounds like great, powerful tools... if you have already adjusted to
hacking guile
> (container nodal-tree)
>    A tree consisting of nodes with attributes
Ditto.
> (container delay-tree)
>    A nodal tree with lazily evaluated fields
Ditto.
> (debugging assert)
>    Helpful assert macro
This is something I think newcomers should be exposed to early on,
> (debugging time)
>    A simple macro to time the execution of an expression
Ditto.
>(graph topological-sort)
>    Routines to perform topological sorts
if you came for topological sort, guiles got you covered
>(htmlprag)
>    Neil Van Dyke's permissive ("pragmatic") HTML parser
nice, super useful
>(io string)
>    SLIB's IO routines dealing with strings
Ditto
>(logging logger)
>    A flexible logging system
every guix dream, yet I doubt most know this is here
>(logging port-log)
>    A logger that outputs to a port
ditto
>(logging rotating-log)
>    A logger that rotates its output files
ditto
>(match-bind)
>    Nifty and concise regular expression routines
I wasn looking for such a thing weeks ago... another reason a wiki or
some other quick and simple to edit archive of everything in guile would
be nice
>(math minima)
>    A golden-section minimum finder
sicp vibes over here
>(math primes)
>    Functions related to prime numbers and factorization
ditto
>(os process)
>    Spawning processes and capturing their output
where has this been my whole guile experience?!? too late, I made one
myself (:
> (scheme documentation)
>    Macros to define different kinds of variables with documentation
this sounds great! didn't catch it my first few glances through
>(scheme kwargs)
>    Defining functions with flexible keyword arguments
how is that different from existing kwargs?
>(search basic)
>    Classic search functions
useful but also the kind of thing one typically implements to learn a lang 
>(string completion)
>    Building blocks for tab completion
thats cool! but why didnt they just go ahead and... complete it?
>(string soundex)
>    The SOUNDEX string categorization algorithm
no idea what that is
>(string transform)
>    Beyond SRFI-13
would be nice to have subtext say a bit more
>(string wrap)
>    A versatile string formatter
why do I need more than (format ...)?
>(term ansi-color)
>    Generate ANSI color escape sequences
nice!
>(unit-test)
>    A JUnit-style unit testing framework
nice

As you can see, much of this is stuff that won't come in handy until you
already feel compfortable with guile.

In fact, some of it will just seem like weird alien technology until
you've spent some time with scheme.

So while I agree that an organized effort to roll-out a battery pack
would be hugely beneficial, I think efforts to make the existing
knowledge base of guile more accessible/easier to navigate/less messy
would get folks moving with the large ecosystem faster,
speaking/thinking in guile, and thus would be more rewarding than
porting conventional idioms into a box that makes guile more familiar
for python and JS developers.


-- 
“In girum imus nocte et consumimur igni”



^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2022-03-09 14:14 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-22  9:29 Shell commands with output to string Zelphir Kaltstahl
2022-02-22  9:38 ` Zelphir Kaltstahl
2022-02-22 10:20   ` Alex Sassmannshausen
2022-02-22 10:43     ` post
2022-02-23 14:01       ` Josselin Poiret
2022-03-08 23:12         ` Zelphir Kaltstahl
2022-03-09 14:14           ` Josselin Poiret
2022-02-22 11:20     ` Neil Jerram
2022-02-23  1:28       ` Zelphir Kaltstahl
2022-02-23  1:29     ` Zelphir Kaltstahl
2022-02-22 10:21   ` tomas
2022-02-22 14:27 ` Olivier Dion via General Guile related discussions
2022-02-22 16:00   ` Leo Butler
2022-02-22 16:33     ` Olivier Dion via General Guile related discussions
2022-02-23  1:26       ` Zelphir Kaltstahl
2022-02-23 14:13         ` Olivier Dion via General Guile related discussions
2022-02-26  0:32           ` Zelphir Kaltstahl
  -- strict thread matches above, loose matches on Subject: below --
2022-02-23 17:48 Blake Shaw
2022-02-23 18:25 ` Olivier Dion via General Guile related discussions

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).