Re: [racket-users] Re: Is there a way to format keys/values like raise-arguments-error

2021-09-10 Thread David Storrs
On Fri, Sep 10, 2021 at 1:33 PM kamist...@gmail.com 
wrote:

> I like the apply max instead of foldl, quite a bit easier.
>
> Instead of `(format "\t~a" (~a (~a k #:width width) v #:separator "\t"))`
> I prefer one of these:
> (~a "\t" (~a k #:width width) "\t" v)
> (~a #:separator "\t" "" (~a k #:width width) v)
>

Fine.  If you want to be all efficient and readable, go right ahead.

Thanks, applied.  :>


> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/d652f06d-81ed-4a5c-983c-d1716f838a38n%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAE8gKod3k_wBfCtVPe9daOg%3DXkKvq-ZAcYDcCdHqq-%3DUBQ%3DFbg%40mail.gmail.com.


Re: [racket-users] Re: Is there a way to format keys/values like raise-arguments-error

2021-09-10 Thread kamist...@gmail.com
I like the apply max instead of foldl, quite a bit easier.

Instead of `(format "\t~a" (~a (~a k #:width width) v #:separator "\t"))` I 
prefer one of these:
(~a "\t" (~a k #:width width) "\t" v)
(~a #:separator "\t" "" (~a k #:width width) v)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/d652f06d-81ed-4a5c-983c-d1716f838a38n%40googlegroups.com.


Re: [racket-users] Is there a way to format keys/values like raise-arguments-error

2021-09-10 Thread David Storrs
Thank you, Laurent.  That's a cool package; I think it would work well to
generate tables suitable for org-mode spreadsheets.  I could even pull data
from a sqlite DB, giving me power and portability without needing an
internet connection or a full heavyweight spreadsheet app.

On Thu, Sep 9, 2021 at 5:26 PM Laurent  wrote:

> Maybe take a look at the text-table package.
>
> On Wed, 8 Sep 2021, 14:41 David Storrs,  wrote:
>
>> raise-arguments-errors produces neatly stacked key/value pairs with
>> whitespace arranged such that values line up even when keys are of
>> different lengths.  Is there an easy way to get that for something that is
>> not an error?  I've been through both The Printer and the
>> raise-arguments-error sections in the Reference and can't find anything.
>>
>> For example:
>>
>> (doit "x" 1 "foo" 2 "super" 3)
>>
>> Returns the string "x 1\nfoo   2\super 3"
>>
>>
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to racket-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/racket-users/CAE8gKod_s8EhQPW9uG7P02%3D8Kmpqf2Uw%3DKLORWD%3DwmFrxnGkWA%40mail.gmail.com
>> 
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAE8gKodAzS2-FCt_KxoP-StFvxQpHBnYd1VCwb6sh9n0rPKS-g%40mail.gmail.com.


Re: [racket-users] Re: Is there a way to format keys/values like raise-arguments-error

2021-09-10 Thread David Storrs
Wow, thank you Simon.  That was above and beyond.

I should have thought of ~a to start with.  With your pointers I came up
with a less flexible solution that is sufficient to the current issue:


#lang racket/base
(require racket/format
 racket/string)

(define my-keys   '("bacon" "eggs" "pancakes"))
(define my-vals   '("turkey" "whites" "blueberry"))

(define (make-str keys vals)
  (define width (apply max (map string-length keys)))
  (string-join
   (for/list ([k keys]
  [v vals])
 (format "\t~a" (~a (~a k #:width width) v #:separator "\t")))
   "\n"))

(displayln (make-str my-keys my-vals))


On Thu, Sep 9, 2021 at 4:11 PM kamist...@gmail.com 
wrote:

> I am not really aware of a function that does this you could try digging
> into the implementation of raise-arguments-error,
> usually I roll my own implementation depending on what I really want to
> output.
> racket/format and its ~a, ~v, etc. have a lot of useful optional keyword
> arguments like #:align #:pad-string #:width etc.
>
> This isn't totally what you want, but maybe it has something that is
> useful to you.
> This or similar code is what I have used sometimes:
>
> #lang racket
>
> (define current-prefix-length (make-parameter 0))
> (define (prefixln #:prefix [prefix ""]
>   #:align  [align 'left]
>   . message)
>   (displayln (apply ~a (list* (~a prefix #:width (current-prefix-length)
> #:align align)
>   message
>
> (define-syntax-rule (with-indent body ...)
>   (parameterize ([current-prefix-length (+ (current-prefix-length) 2)])
> body ...))
>
> (define (example-func1)
>   (prefixln "start of example func1")
>   (with-indent
> (example-func2))
>   (prefixln "end of example func1"))
>
> (define (example-func2)
>   (prefixln "start of example func2")
>   (prefixln "end of example func2"))
>
>
> (module+ main
>
>   (displayln "Hello checkout these values:")
>   (define example-values
> (hash 'foo 123
>   'this-is-a-long-key "some value"
>   'blabla #f
>   "cake" "is a lie"))
>
>   ;; ugly oneliner to calculate prefix width
>   (current-prefix-length (+ 2 (foldl max 0 (map (compose1 string-length
> ~a) (hash-keys example-values)
>
>   (for ([(k v) (in-hash example-values)]) ;; probably sorting or assoc
> list would make sense too...
> (prefixln #:prefix (~a k ": ") #:align 'right
>   v))
>
>   (current-prefix-length 0)
>   (displayln "")
>   (displayln "indentation through multiple nested calls:")
>   (with-indent
> (example-func1)))
>
> If you use a current-prefix-string parameter instead you can create other
> interesting things like lines indented with indentation level indicators,
> etc.:
> indent0
> | indent1
> | | indent2
> | indent1
> indent0
>
> But I am getting too off-topic...
>
> Simon
>
> david@gmail.com schrieb am Mittwoch, 8. September 2021 um 15:41:56
> UTC+2:
>
>> raise-arguments-errors produces neatly stacked key/value pairs with
>> whitespace arranged such that values line up even when keys are of
>> different lengths.  Is there an easy way to get that for something that is
>> not an error?  I've been through both The Printer and the
>> raise-arguments-error sections in the Reference and can't find anything.
>>
>> For example:
>>
>> (doit "x" 1 "foo" 2 "super" 3)
>>
>> Returns the string "x 1\nfoo   2\super 3"
>>
>>
>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/de65e4a2-7cd3-4347-949f-d8a1f457961en%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAE8gKoc5md1TUFJHmRb6%3DNTUJ%2BCzCMLDaQV5poUR64YCAo9bbg%40mail.gmail.com.