wanpeebaw
2020-5-8 11:32:49

Is there any operator that can let me rewrite (for/sum ([i (in-range 1 11)]) (sqr i)) as something like following more concise form? (sum (map sqr (range 1 11))) I know there is stream-map, but there isn’t a corresponding stream-sum.

Or even better some generic operator that can perform on anything sequential as List, Sequence or Stream.


popa.bogdanp
2020-5-8 11:45:50

@wanpeebaw there is stream-fold: (stream-fold + 0 (stream-map sqr (range 1 11)))


soegaard2
2020-5-8 11:47:49

Note that (range 1 11) will allocate a list with 10 elements. So for/sum is more effecient especially for large ranges.


popa.bogdanp
2020-5-8 11:50:45

in-range should work with stream-map, but I assume that stream-fold + stream-map will still be slower than for/sum . sequence-{fold,map} are also available.


soegaard2
2020-5-8 11:52:49

Perhaps @notjack’s rebellion can be used as an alternative? https://docs.racket-lang.org/rebellion/Streaming_Computations.html?q=rebellion


soegaard2
2020-5-8 11:54:50

Some combination of reducer-map and into-sum.


notjack
2020-5-8 18:05:29

specifically, it would be this: (transduce (in-range 1 11) (mapping sqr) #:into into-sum) (as @samdphillips pointed out in #api-design earlier)


sorawee
2020-5-8 19:40:51

Also, if you really want stream-sum, just create one?

(define (stream-sum xs) (for/sum ([x (in-stream xs)]) x))


notjack
2020-5-8 19:48:15

consider accepting the more general sequence interface

(define (sequence-sum xs) (for/sum ([x xs]) x))


notjack
2020-5-8 19:48:43

will automatically work with streams, as well as tons of other things


sorawee
2020-5-8 20:18:40

And unfortunately slower :disappointed:


sorawee
2020-5-8 20:20:20

Why can’t (for ([x xs]) body) be compiled into:

(cond [(list? xs) (current-for ([x (in-list xs)]) body)] [(vector? xs) (current-for ([x (in-vector xs)]) body)] ... [else (current-for ([x xs]) body)])


samdphillips
2020-5-8 20:21:25

All for loops blow up to 20x size in compiled code


sorawee
2020-5-8 20:22:50

Hmm. You are right


sorawee
2020-5-8 20:23:33

Oh, perhaps thunking body first?


sorawee
2020-5-8 20:23:49

Especially in Racket CS, calling functions should be drastically cheaper


alexknauth
2020-5-8 20:37:13

So just thunking body couldn’t do it, you would need to have a lambda that binds the parameter x.


alexknauth
2020-5-8 20:44:09

But I think that would still generate more code than it should. One way of solving that is by putting more things into pre-defined functions. For the simple case of one sequence-clause, the functions for-each, vector-for-each, stream-for-each, and sequence-for-each already exist.


alexknauth
2020-5-8 20:46:44

And what does sequence-for-each do? Does it use a cond like the code above, or does it do something else, better or worse?


sorawee
2020-5-8 20:48:44

(define (sequence-for-each f s) (unless (procedure? f) (raise-argument-error 'sequence-for-each "procedure?" f)) (unless (sequence? s) (raise-argument-error 'sequence-for-each "sequence?" s)) (for ([vs (in-values*-sequence s)]) (if (list? vs) (apply f vs) (f vs))))


sorawee
2020-5-8 20:49:15

:joy:


notjack
2020-5-8 20:59:14

huh, it’s almost like those OOP folks had a point


samdphillips
2020-5-8 20:59:19

Wouldn’t the goal be to expand to some :do-in form?


jcoo092
2020-5-9 03:03:29

Is there a standard or recommend way to access command line parameters when using a (module+ main ...) declaration for a main function? In particular, are there any examples of such? I’m trying to write a small stand-alone program that uses places, and I am under the impression that using module+ main is considered a good way to implement things, but the examples I can dig up never seem to use command line parameters.

I have a working non-parallel version that just uses (define (main param1 param2)...) style, but I think that doesn’t work when you introduce a module+.



jcoo092
2020-5-9 03:12:19

Are you please able to point me towards any examples? The Racket Reference page isn’t totally clear to me.


michaelmmacleod
2020-5-9 03:12:37

There should be an example about halfway down that page


jcoo092
2020-5-9 03:23:31

Thanks :slightly_smiling_face:


samdphillips
2020-5-9 03:24:46

Also (current-command-line-arguments) returns a vector of strings of the arguments


jcoo092
2020-5-9 03:50:01

Thanks folks, that’s definitely been a big help


wanpeebaw
2020-5-9 04:49:40

Is there a hotkey to quickly comment and uncomment code blocks in DrRacket?