Re: [racket-users] Re: Functional object-oriented programming

2017-02-08 Thread Steve Byan's Lists
> On Feb 8, 2017, at 9:05 PM, Philip McGrath  wrote:

> Personally, I tend to end up defining helper functions to do functional 
> update (often with optional keyword arguments to address the 
> fields-that-stay-the-same issue). Generics in the sense of racket/generic can 
> be helpful for this if using structs. I may then implement the helper 
> function using struct-copy, but I can hide away its idiosyncrasies.

Thanks for the comments, Phillip. A helper method with keyword arguments might 
make my code more concise.

Best regards,
-Steve

--  
Steve Byan
steveb...@me.com
Littleton, MA

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Re: Functional object-oriented programming

2017-02-08 Thread Philip McGrath
Note, though, that struct-copy needs to be given the type of the resulting
structure statically, at compile time, and "the result of *struct-expr* can
be an instance of a sub-type of *id*, but the resulting copy is an
immediate instance of *id* (not the sub-type)." [1] If you have a complex
hierarchy of struct types (as might be the case if you're using structs in
an OOP-inspired sort of way), this can get a little annoying.

Personally, I tend to end up defining helper functions to do functional
update (often with optional keyword arguments to address the
fields-that-stay-the-same issue). Generics in the sense of racket/generic
can be helpful for this if using structs. I may then implement the helper
function using struct-copy, but I can hide away its idiosyncrasies.

[1]
http://docs.racket-lang.org/reference/struct-copy.html?q=struct-copy#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct-copy%29%29

On Wed, Feb 8, 2017 at 6:14 PM Alex Harsanyi  wrote:

> Structures support this style of updating:
>
> (struct foo (bar baz) #:transparent)
>
> (define one (foo "hello" 1))
>
> ;; Copy all fields from "one" and update baz to 2
> (define two (struct-copy foo one (baz 2)))
>
> They also have immutable fields by default.
>
> Best Regards,
> Alex.
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Re: Functional object-oriented programming

2017-02-08 Thread Steve Byan's Lists
Matthias, thanks for the confirmation that macros are the answer. Yes, mutation 
could be simpler. I'm learning more doing it functionally.

Alex, thanks for pointing out struct-copy. I hadn't read that part of the 
Racket Guide yet.

Would it be possible to write a macro that when invoked within a class 
definition would generate a "new-copy" method that behaves like struct-copy? I 
would guess that Racket has enough introspection to allow that.

Best regards,
-Steve

--  
Steve Byan
steveb...@me.com
Littleton, MA



-- 
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.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Re: Functional object-oriented programming

2017-02-08 Thread Alex Harsanyi
Structures support this style of updating:

(struct foo (bar baz) #:transparent)

(define one (foo "hello" 1))

;; Copy all fields from "one" and update baz to 2
(define two (struct-copy foo one (baz 2)))

They also have immutable fields by default.

Best Regards,
Alex.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Functional object-oriented programming

2017-02-08 Thread Matthias Felleisen

Yes. 

I have used this pattern extensively and locally defined macros is what you 
want. But, just in case it is not obvious, you can also mutate fields in Racket 
and perhaps that’s what you really want. Racket is not ideological about 
functional purity. 



> On Feb 8, 2017, at 3:54 PM, Steve Byan's Lists  
> wrote:
> 
> I'm just learning Racket's object system. I feel like I've missed something, 
> as it seems pretty verbose to functionally update objects.
> 
> The pattern I use when functionally updating object state is to invoke the 
> class constructor with a full set of arguments, some subset of which have 
> updated values. Something like this:
> 
> #lang racket
> 
> (require math/statistics)
> 
> (define foo%
>  (class object%
>(super-new)
>(init name)
>(init (stat empty-statistics))
>(field [my-name name]
>   [my-stat stat])
>(define/public (update-stat x)
>  (new this% [name my-name] [stat (update-statistics my-stat x)]
> 
> (define foo-stats
>  (for/fold
>   ([current-foo (new foo% [name "foo-object"])])
>   ([x (in-list (list 0.1 0.5 0.15 0.4 0.09))])
>(send current-foo update-stat x)))
> 
> 
> When the class has lots of fields, it is pretty verbose to provide all the 
> arguments to the constructor, even for fields that are not being updated. I 
> feel like I'm missing something that would reduce the amount of typing 
> involved. I suspect the "something" is macros, but I'm not there yet in my 
> journey through Racket-land. Is there some pre-fabbed construct that I've 
> overlooked?
> 
> Best regards,
> -Steve
> 
> --  
> Steve Byan
> steveb...@me.com
> Littleton, MA
> 
> 
> 
> -- 
> 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.
> For more options, visit https://groups.google.com/d/optout.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Fun with keyword-apply

2017-02-08 Thread Dan Liebgold
On Wednesday, February 8, 2017 at 2:08:49 PM UTC-8, Ryan Culpepper wrote:
> 
>(require (for-syntax syntax/parse/experimental/template))
>(define-syntax (test-syntax stx)
> 
>  (define-splicing-syntax-class spec
> as in your original version )
> 
>  (syntax-parse stx
>([_ s:spec ...]
> (template
>  (test (?@ s.key s.value) ...)
> 

That is pretty cool... I didn't think that was possible. It's a little 
concerning that its experimental, but I'm guessing it'll be incorporated into 
the syntax/parse lib (or lower down) at some point.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Fun with keyword-apply

2017-02-08 Thread Ryan Culpepper

On 02/08/2017 04:41 PM, Dan Liebgold wrote:

Hi all -

I have an odd syntax I'm trying to maintain backward compatibility with, but 
I'd like to take advantage of keyword parameters to accommodate the 
presence/absence/ordering of those parameters.

Here's an example of what I'm trying to do: http://pasterack.org/pastes/88615

Can you think of a better way to translate the syntax to function application?  
Note that I must do a little processing of the parameters in my syntax 
(represented here by the multiplications).


There are two problems in your macro. First, the arguments to 
`keyword-apply` are expressions, so you need to turn your sequences of 
keywords and keyword arguments into list-valued expressions. Something 
like this:


  '(s.key ...)  ;; quote okay because keys are literals, not exprs
  (list s.value ...)   ;; must use list, not quote

Second, `keyword-apply` also requires its arguments to be sorted; so you 
would have to do something like this:


  (define-syntax (test-syntax stx)

(define-splicing-syntax-class spec
  #:datum-literals (:a :b :c)
  (pattern (~seq :a n:number)
   #:attr key '#:a;; not syntax, just keyword
   #:attr value #'n)
  (pattern (~seq :b n:number)
   #:attr key '#:b;; likewise
   #:attr value #'(* 2 n))
  (pattern (~seq :c n:number)
   #:attr key '#:c;; likewise
   #:attr value #'(* 4 n))
  )

(syntax-parse stx
  ([_ s:spec ...]
   (define unsorted  ;; (Listof (cons Keyword Syntax))
 (map cons (attribute s.key) (attribute s.value)))
   (define sorted (sort unsorted keywordOr you can reuse Racket's `#%app` to do the keyword sorting for you. The 
`template` form and its `?@` splicing support makes this more convenient:


  (require (for-syntax syntax/parse/experimental/template))
  (define-syntax (test-syntax stx)

(define-splicing-syntax-class spec
   as in your original version )

(syntax-parse stx
  ([_ s:spec ...]
   (template
(test (?@ s.key s.value) ...)

Ryan

--
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.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Fun with keyword-apply

2017-02-08 Thread Dan Liebgold
Hi all -

I have an odd syntax I'm trying to maintain backward compatibility with, but 
I'd like to take advantage of keyword parameters to accommodate the 
presence/absence/ordering of those parameters.

Here's an example of what I'm trying to do: http://pasterack.org/pastes/88615

Can you think of a better way to translate the syntax to function application?  
Note that I must do a little processing of the parameters in my syntax 
(represented here by the multiplications).

Thanks,
Dan

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Functional object-oriented programming

2017-02-08 Thread Steve Byan's Lists
I'm just learning Racket's object system. I feel like I've missed something, as 
it seems pretty verbose to functionally update objects.

The pattern I use when functionally updating object state is to invoke the 
class constructor with a full set of arguments, some subset of which have 
updated values. Something like this:

#lang racket

(require math/statistics)

(define foo%
  (class object%
(super-new)
(init name)
(init (stat empty-statistics))
(field [my-name name]
   [my-stat stat])
(define/public (update-stat x)
  (new this% [name my-name] [stat (update-statistics my-stat x)]

(define foo-stats
  (for/fold
   ([current-foo (new foo% [name "foo-object"])])
   ([x (in-list (list 0.1 0.5 0.15 0.4 0.09))])
(send current-foo update-stat x)))


When the class has lots of fields, it is pretty verbose to provide all the 
arguments to the constructor, even for fields that are not being updated. I 
feel like I'm missing something that would reduce the amount of typing 
involved. I suspect the "something" is macros, but I'm not there yet in my 
journey through Racket-land. Is there some pre-fabbed construct that I've 
overlooked?

Best regards,
-Steve

--  
Steve Byan
steveb...@me.com
Littleton, MA



-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] read from stdin, except when run in DrRacket

2017-02-08 Thread Steve Byan's Lists

> On Feb 8, 2017, at 2:06 PM, Matthias Felleisen  wrote:
> 
> 
> I thought of giving this answer too, but if this is about testing let me 
> propose a slightly different approach: 

Thanks. By "testing" I meant flailing around in the REPL while I a) learn 
Racket and b) figure out the logic for my program, not exercising unit tests. 
But your approach is perfect for unit testing.

Best regards,
-Steve

--  
Steve Byan
steveb...@me.com
Littleton, MA



-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] read from stdin, except when run in DrRacket

2017-02-08 Thread Matthias Felleisen

I thought of giving this answer too, but if this is about testing let me 
propose a slightly different approach: 

#lang racket

;; assume main has two arguments
(define (main)
  (define args (current-command-line-arguments))
  (if (= (vector-length args) 0)
  (do-it)
  (with-input-from-file (vector-length args) do-it)))

(define (do-it)
  (read-line))

(module+ test
  (require rackunit)

  (define my-file (open-input-string "hello world\n line 2"))

  (check-equal? (parameterize ([current-input-port my-file]) (do-it)) "hello 
world"))
  

Also you can exercise main in DrRacket as if it were invoked via Language > 
Show Details > command line arguments . . . 



> On Feb 8, 2017, at 2:02 PM, Ben Greenman  wrote:
> 
> One idea: you can put the argument-parsing code in the "main" submodule, then 
> tell DrRacket not to run the main submodule.
> 
> 
> #lang racket
> 
> (define (run in-port)
>   "testing...")
> 
> (module+ main
>   (require racket/cmdline)
>   (command-line
> #:args args
> (run (if (null? args) (current-input-port) (open-input-file (car 
> args))
> 
> Then in DrRacket, click "Language -> Choose Language -> Show Details -> 
> Submodules to Run" and un-check "main".
> 
> 
> On Wed, Feb 8, 2017 at 1:51 PM, Steve Byan's Lists 
>  wrote:
> I'm working on a script that I eventually plan to invoke from the command 
> line. I'd like the script to either take a file name argument or, if no 
> arguments, read from stdin. However, while developing the script in DrRacket, 
> I'd like to not invoke the top-level function, and to instead define an input 
> port on a test file for convenience when exercising the code from the 
> DrRacket REPL.
> 
> Is there a way for a module to distinguish between running in DrRacket and 
> running from the racket or gracket command?
> 
> Best regards,
> -Steve
> 
> --
> Steve Byan
> steveb...@me.com
> Littleton, MA
> 
> 
> 
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
> 
> 
> -- 
> 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.
> For more options, visit https://groups.google.com/d/optout.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] read from stdin, except when run in DrRacket

2017-02-08 Thread Steve Byan's Lists

> On Feb 8, 2017, at 2:02 PM, Ben Greenman  wrote:
> 
> One idea: you can put the argument-parsing code in the "main" submodule, then 
> tell DrRacket not to run the main submodule.

[snip]

> Then in DrRacket, click "Language -> Choose Language -> Show Details -> 
> Submodules to Run" and un-check "main".

Thank you! That's the ticket.

Best regards,
-Steve

--  
Steve Byan
steveb...@me.com
Littleton, MA



-- 
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.
For more options, visit https://groups.google.com/d/optout.


[racket-users] read from stdin, except when run in DrRacket

2017-02-08 Thread Steve Byan's Lists
I'm working on a script that I eventually plan to invoke from the command line. 
I'd like the script to either take a file name argument or, if no arguments, 
read from stdin. However, while developing the script in DrRacket, I'd like to 
not invoke the top-level function, and to instead define an input port on a 
test file for convenience when exercising the code from the DrRacket REPL.

Is there a way for a module to distinguish between running in DrRacket and 
running from the racket or gracket command?

Best regards,
-Steve

--  
Steve Byan
steveb...@me.com
Littleton, MA



-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] IMAP with racket

2017-02-08 Thread Tim Hanson
Cool, thank you! I had no idea about parameterize. I will try that and continue 
my investigations.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] IMAP with racket

2017-02-08 Thread Matthew Flatt
At Tue, 7 Feb 2017 21:54:40 -0800 (PST), Tim Hanson wrote:
> https://docs.racket-lang.org/net/imap.html#%28def._%28%28lib._net%2Fimap..rkt%2
> 9._imap-port-number%29%29
> 
> Does this make imap-port-number a kind of global variable?

Normally, `imap-port-number` would be used with `parameterize` to
localize a setting:

 (parameterize ([imap-port-number 12345])
   (imap-connect ))

I agree that an optional keyword argument would be better in this case,
and it's just that the parameter predates keyword-argument support.
(It's a relatively old library.) Arguments like `#:tls?` were added
later.

A `#:port-number` argument that defaults to the parameter value would
work well.

At Wed, 8 Feb 2017 01:38:05 -0800 (PST), Tim Hanson wrote:
> P.s. For that matter couldn't folder also be optional, defaulting to "INBOX"?

That also sounds to me like a fine improvement.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[racket-users] How to debug segmentation fault in Racket application

2017-02-08 Thread Alex Harsanyi
Occasionally, my Racket application crashes with a segmentation fault (under 
Windows).  The crash is a NULL pointer reference.  Unfortunately, having no 
PDB's the stack trace only contains a list of addresses.

Unfortunately, this only happens occasionally, but under the same conditions.  
It also happened in several Racket versions, with the last crash was on Racket 
6.7

Does anyone have any advice on how to debug the crash?

I currently have saved the crash dump file and, if there are PDB's for the 
Windows 64bit Racket 6.7, I could get a good stack trace.  I can also share the 
crash dump file, if anyone is interested, but it is 700 Mb in size 
(uncompressed).

Thanks,
Alex.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] How to implement a dynamic plugin system?

2017-02-08 Thread Erich Rast
Thanks Matthias and Laurent for your advice!

I've taken a look at the source code in MrEd designer and understand
how dynamic-require works, but have decided against the plugin
structure at least for now, since plugin access to a shared database
creates other problems - it turned out at a closer look that my plugins
would not be as independent and modular than I thought.

Best,

Erich

MrEd On Tue, 7 Feb 2017 15:20:26 +
Laurent  wrote:

> There's also a plugin system in MrEdDesigner based on the directory
> structure to load various widgets at runtime:
> https://github.com/Metaxal/MrEd-Designer/blob/master/mred-designer/plugin.rkt
> https://github.com/Metaxal/MrEd-Designer/blob/master/mred-designer/mred-plugin.rkt
> https://github.com/Metaxal/MrEd-Designer/wiki/Developer%27s-Documentation
> 
> HTH,
> Laurent
> 
> On Tue, Feb 7, 2017 at 2:36 PM, Matthias Felleisen
>  wrote:
> 
> >
> > Erich, your needs sound very much like those of DrRacket’s.
> > You may wish to study how DrRacket loads tools (as in take
> > a look at the source) — Matthias
> >
> >
> >
> >
> >  
> > > On Feb 7, 2017, at 9:31 AM, Erich Rast  wrote:
> > >
> > > Thanks for the advice. So I should use dynamic-require, but how?
> > > When I tried to use it like "require" it didn't have the desired
> > > effect - nothing was imported. I have to admit I'm struggling a
> > > bit with all the different ways of load/eval/require/enter code
> > > that I've found in the docs so far.
> > >
> > > Here is a more concrete example of what I'd like to achieve:
> > >
> > > (require racket/gui/base
> > > gregor
> > > (prefix-in internat: "../core/internat.rkt")
> > > (prefix-in pref: "../core/prefs.rkt")
> > > (prefix-in db: "../core/dbaccess.rkt")
> > > (prefix-in dates: "../core/dates.rkt")
> > > (prefix-in metafonts: "../core/metafonts.rkt")
> > > (prefix-in threaded: "../core/threaded.rkt")
> > > (prefix-in console: "console.rkt")
> > > "../notetaker/notetaker-main.rkt")
> > >
> > > I'd like the last require to be dynamic, loaded at runtime from a
> > > relative directory and just-in-time compiled or compiled to disk
> > > on demand. "notetaker-main.rkt" needs to obtain all definitions
> > > from the previous imports (with the prefixes), and the current
> > > module needs to obtain notetaker-main.rkt's exports - in this
> > > case a 'notes-panel%, for instance. This also needs to work in a
> > > compiled application created with "Create
> > > Executable ...>Distribution" from the Racket menu.
> > >
> > > How would I go about that? Is it very complicated?
> > >
> > > If it's very complicated, maybe I should abandon the idea. But it
> > > would be neat, because the main application is really just a tab
> > > panel with many independent subcomponents as panel% classes.
> > >
> > > Best,
> > >
> > > Erich
> > >
> > >
> > >
> > >
> > > On Tue, 7 Feb 2017 20:05:03 +0800
> > > WarGrey Gyoudmon Ju  wrote:
> > >  
> > >> I think you can check the source of `raco` which uses the
> > >> `info.rkt` to search all subcommands(see `racket/getinfo`). In
> > >> this way, all plugins are just normal packages, you do not have
> > >> to reside them is a specific directory. Certainly, the plugin is
> > >> loaded via `dynamic-require`, if the plugin needs the imports of
> > >> main  
> > >
> > > --
> > > 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.  
> > > For more options, visit https://groups.google.com/d/optout.  
> >
> > --
> > 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.
> > For more options, visit https://groups.google.com/d/optout.
> >  
> 

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] IMAP with racket

2017-02-08 Thread Tim Hanson
P.s. For that matter couldn't folder also be optional, defaulting to "INBOX"?

-- 
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.
For more options, visit https://groups.google.com/d/optout.