Re: [racket-users] Computed properties for a struct?

2021-06-04 Thread Sorawee Porncharoenwase
I think you can create a custom accessor, and use macros to avoid the Royal
Pain.

One question is, is the derived field expensive to compute? If not, your
custom accessor can just compute the output every time it’s called. But if
it’s expensive, you can use memoization. Both approaches have the benefit
of performing the computation lazily.

#lang racket

(require syntax/parse/define
 (for-syntax racket/syntax))

(define (memoize f)
  (define table (make-hasheq))
  (λ (self) (hash-ref! table self (λ () (f self)

(define-syntax-parse-rule
  (provide+define-struct
   name fields
   {~seq #:field custom-accessor-name e} ...)
  #:with (custom-accessor-name* ...)
  (for/list ([n (attribute custom-accessor-name)])
(format-id #'name "~a-~a" #'name n))
  (begin
(struct name fields)
(define custom-accessor-name* (memoize e)) ...
(provide (struct-out name)
 custom-accessor-name* ...)))

(provide+define-struct
 foo (bar baz)
 #:field bad (λ (self) (+ (foo-bar self) (foo-baz self)))
 #:field bay (λ (self)
   (println 'should-be-called-once)
   (* (foo-bad self) 2)))

(define obj (foo 3 4))
(foo-bay obj)
; 'should-be-called-once
; 14
(foo-bay obj)
; 14


On Sat, Jun 5, 2021 at 5:21 AM flirora  wrote:

> Is there a way to define a struct so that it has a field whose value is
> filled in (instead of passed to the constructor) with a value derived from
> other fields? For example, could you define a struct foo with two
> explicit fields, x and y, plus a field called z whose value is computed
> as (+ x y) (yes, simple, but imagine that this is a more expensive
> operation):
>
> > (define foo1 (foo 1 2))
> > (define foo2 (foo 7 12))
> > (foo-z foo1)
> 3
> > (foo-z foo2)
> 19
>
> The closest I could find in the documentation was the #:auto property,
> but:
>
>1. it makes the field mutable, even though I'm not interested in
>mutating z
>2. the default value is fixed across all constructions, while I want z
>to depend on x and y
>
> Of course, I could make z an explicit field, write a custom constructor,
> and export that instead of the default constructor for foo. But that seems
> to be a Royal Pain, especially since (AFAIK) you can't provide the struct
> as a whole anymore. (And I need to write about 12 of these structs to boot.)
>
> --
> 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/9f9e1548-d01d-469f-b565-22601e02dd82n%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/CADcuegvQdqm5BRx8DGa6Afqgw6vHOjaOhG3C6WdJznaEJW1%3DCQ%40mail.gmail.com.


[racket-users] Computed properties for a struct?

2021-06-04 Thread flirora
Is there a way to define a struct so that it has a field whose value is 
filled in (instead of passed to the constructor) with a value derived from 
other fields? For example, could you define a struct foo with two explicit 
fields, x and y, plus a field called z whose value is computed as (+ x y) 
(yes, simple, but imagine that this is a more expensive operation):

> (define foo1 (foo 1 2))
> (define foo2 (foo 7 12))
> (foo-z foo1)
3
> (foo-z foo2)
19

The closest I could find in the documentation was the #:auto property, but:

   1. it makes the field mutable, even though I'm not interested in 
   mutating z
   2. the default value is fixed across all constructions, while I want z 
   to depend on x and y

Of course, I could make z an explicit field, write a custom constructor, 
and export that instead of the default constructor for foo. But that seems 
to be a Royal Pain, especially since (AFAIK) you can't provide the struct 
as a whole anymore. (And I need to write about 12 of these structs to boot.)

-- 
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/9f9e1548-d01d-469f-b565-22601e02dd82n%40googlegroups.com.


[racket-users] This v 8.1 collection path looks incorrect...

2021-06-04 Thread Don Green

(current-library-collection-paths)
'(# 
# #)

I suspect the above is incorrect because I think the 8.1 path should be to 
dir: /pkgs rather than /collects.  Like this:
#

-- 
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/a48793dd-ef9e-4329-bbee-4d3f1aa2a6f4n%40googlegroups.com.


[racket-users] Re: config.rktd

2021-06-04 Thread Don Green
Now that I have learned of: config.rktd,
I think I'd rather use it, if it can be used to set 
current-library-collection paths.
Should I just add to the config.rktd hash list:
 (current-library-collection paths . 
'(# 
# #)) 

-- 
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/3ca2d559-93bf-4a90-9188-f1f1126638dan%40googlegroups.com.


Re: [racket-users] Regarding collections ...

2021-06-04 Thread Don Green
To answer Mathew Flatt's question: "Do you expect 3" (paths) "because you 
have an
environment variable set, or something like that? "
Answer: Yes. I can run a shell file that contains:
#! /bin/bash
export PLTCOLLECTS="/home/don/.plt-scheme/4.2.1/collects:"

Many of my programs are accessible from that path.
---
Sounds like ideally I should begin creating my own pkg(s) that are outside 
a collection path, and link it by specifying a 
current-library-collection-links parameter.

In the mean time I'd like to add the collection path.
---
Currently,
When I open DrRacket and run: (current-library-collection-paths) it returns:
'(# 
# #) 
which is correct and no problems, however
when I click on a .ss file in the file-manager, the file is opened in 
drracket v.8.1 but
(current-library-collection-paths) returns only:
'(# #)

The problem seems to be that the file-manager needs to set 
PLTCOLLECTS="/home/don/.plt-scheme/4.2.1/collects:
to set the env. variable so that drracket can find 
#.

Is there a more convenient method to achieve what I am after by making some 
configuration in racket rather than in my file-manager?


On Friday, June 4, 2021 at 7:21:17 AM UTC-6 Matthew Flatt wrote:

> At Thu, 3 Jun 2021 20:26:59 -0700 (PDT), Don Green wrote:
> > Using DrRacket in linux:
> > When I run DrRacket from a terminal,
> > 
> > (current-library-collection-paths) returns the expected paths, 3 of them.
> > 
> > However, when I open one of my .ss files which is associated with 
> drracket,
> > 
> > the file opens in drracket but (current-library-collection-paths) 
> returns 
> > only 2 of the 3 desired collection paths.
>
> I think we're missing some context: Why do you expect 3 paths from
> `(current-library-collection-paths)`?
>
> A normal configuration would have just 2 paths, but it's possible to
> have more due to command-line flags, environment variables, or
> configuration in "config.rktd". Do you expect 3 because you have an
> environment variable set, or something like that?
>
>
> > Should I use info.rkt to specify the 3 desired collection paths?
>
> An "info.rkt" file will not change the collection-paths parameter.
> Normally, instead of adding new collection paths, new collections are
> added through packages. Those packages are found through the
> `current-library-collection-links` parameter, which points to files
> that contain more paths.
>
>
> Matthew
>

-- 
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/f65fd3a4-8f74-44ab-933f-fc9a30a93933n%40googlegroups.com.


Re: [racket-users] Defining a function with the same name as a struct with a different constructor name

2021-06-04 Thread flirora
Thanks for the answers!

On Thursday, 3 June 2021 at 17:26:57 UTC-4 shhyou wrote:

> In addition to the constructor, `foo` is also used for compile-time
> struct information. To avoid this, either supply #:name to designate a
> different identifier for compile-time struct type information, or
> declare #:omit-define-syntaxes to dismiss it.
>
> Shu-hung
>
> On Thu, Jun 3, 2021 at 4:04 PM Sage Gerard  wrote:
> >
> > My understanding is that you can shadow `foo`, but you cannot use `foo` 
> in any expression at the same scope where the struct's bindings appear. In 
> this context, some of `foo`'s lexical information is reserved so that 
> `struct-out` works correctly.
> >
> > On 6/3/21 5:01 PM, flirora wrote:
> >
> >
> > Am I correct in assuming that given a definition like
> >
> > (struct foo (x) #:constructor-name bar)
> >
> > I then can't define something named foo in the same module, even though 
> there's no constructor called foo?
> >
> > --
> > 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...@googlegroups.com.
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/02598ee4-4461-4815-bd4f-a8ac02c1118cn%40googlegroups.com
> .
> >
> > --
> > ~slg
> >
> > --
> > 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...@googlegroups.com.
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/76c51dda-b80d-d74a-c691-d92fe11008e8%40sagegerard.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/2026a972-c405-4c0d-8cff-f9ea0714ad19n%40googlegroups.com.


Re: [racket-users] Regarding collections ...

2021-06-04 Thread Matthew Flatt
At Thu, 3 Jun 2021 20:26:59 -0700 (PDT), Don Green wrote:
> Using DrRacket in linux:
> When I run DrRacket from a terminal,
> 
> (current-library-collection-paths) returns the expected paths, 3 of them.
> 
> However, when I open one of my .ss files which is associated with drracket,
> 
> the file opens in drracket but (current-library-collection-paths) returns 
> only 2 of the 3 desired collection paths.

I think we're missing some context: Why do you expect 3 paths from
`(current-library-collection-paths)`?

A normal configuration would have just 2 paths, but it's possible to
have more due to command-line flags, environment variables, or
configuration in "config.rktd". Do you expect 3 because you have an
environment variable set, or something like that?


> Should I use info.rkt to specify the 3 desired collection paths?

An "info.rkt" file will not change the collection-paths parameter.
Normally, instead of adding new collection paths, new collections are
added through packages. Those packages are found through the
`current-library-collection-links` parameter, which points to files
that contain more paths.


Matthew

-- 
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/20210604072114.df%40sirmail.smtps.cs.utah.edu.