Re: [racket-users] Semaphore-count

2018-09-05 Thread George Neuner



On 9/4/2018 11:46 AM, George Neuner wrote:


AFAIK there is no way to simply 'check' if the semaphore is available.



I was wrong - there is a way using events: semaphore-peek-evt  can check 
whether a semaphore is ready without affecting its counter.


https://docs.racket-lang.org/reference/semaphore.html?q=semaphore-peek#%28def._%28%28quote._~23~25kernel%29._semaphore-peek-evt%29%29


George

--
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 tighten the contract on a function

2018-09-05 Thread David Storrs
On Wed, Sep 5, 2018 at 2:29 PM, Alexander McLin 
wrote:

> Something like the following program:
>
> #lang racket
>
> (module foomod racket
>   (provide foo)
>   (define/contract (foo arg) (-> string? #t) #t))
>
> (require 'foomod)
> (provide (contract-out (foo (-> non-empty-string? #t
>
>
I was looking to change the contract within the body of the requiring
module, but this would work for the other half of the solution.  Combine it
with Matthew Butterick's solution and it's a slam dunk.  Thanks for the
suggestion.

Dave


>
>
> On Tuesday, September 4, 2018 at 9:00:06 PM UTC-4, David K. Storrs wrote:
>>
>>
>>
>> On Tue, Sep 4, 2018 at 8:55 PM, Matthew Butterick 
>> wrote:
>>
>>
>>> On Sep 4, 2018, at 3:54 PM, David Storrs  wrote:
>>>
>>> Say I have this (possibly from a third-party module):
>>>
>>>   (define/contract (foo arg) (-> string? #t) #t)
>>>
>>> I want to ensure that the argument is always non-empty, so I tighten the
>>> contract.  I could do this:
>>>
>>>   (set! foo (contract (-> non-empty-string? #t) foo 'foo 'neg))
>>>
>>> Mutating the function is pretty ugly, but I'm not sure of a better way.
>>>
>>>
>>>
>>> Seems like this technique would break down with imported identifiers,
>>> for instance:
>>>
>>> ;
>>>
>>> #lang racket
>>>
>>> (module foomod racket
>>>   (provide foo)
>>>   (define/contract (foo arg) (-> string? #t) #t))
>>> (require 'foomod)
>>>
>>> (set! foo (contract (-> non-empty-string? #t) foo 'foo 'neg))
>>>
>>> (foo "") ; error: set!: cannot mutate module-required identifier in: foo
>>>
>>>
>>>
>>>
>> Ah, good point.  I'd realized that the tighter version would be visible
>> only in the current module, but I hadn't noticed that you actually can't
>> mutate imports.  Thanks.
>>
>>
>>> I am left with the following questions:
>>>
>>> 1) Is there a better way to do this?
>>>
>>>
>>> Why not the old prefix-and-wrap?
>>>
>>> ;
>>>
>>> #lang racket
>>>
>>> (module foomod racket
>>>   (provide foo)
>>>   (define/contract (foo arg) (-> string? #t) #t))
>>> (require (prefix-in lax: 'foomod))
>>>
>>> (define foo (contract (-> non-empty-string? #t) lax:foo 'foo 'neg))
>>>
>>> (foo "")
>>>
>>>
>> That works.
>>
>>
>>>
>>> 2) If I use this method in a module other than the one where foo was
>>> defined, my expectation is that I would affect it only in the current
>>> module but that other importers would not see the change.  Is this right?
>>>
>>>
>>> When you export this new `foo`, it carries the new contract, of course.
>>> (The old one remains but because your wrapper contract is tighter, any
>>> contract blame will happen in the new contract)
>>>
>>
>> Great, thanks.  This does exactly what I need.
>>
>> --
> 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] Racket and OS X Finder

2018-09-05 Thread Lehi Toskin
The application-file-handler is exactly what I needed. Thank you!

On Wednesday, September 5, 2018 at 11:24:01 AM UTC-7, Philip McGrath wrote:
>
> I have built toy application as a learning project that opens files in 
> Finder as a default app. I haven't worked on it in a while, so I may be 
> forgetting some things, but I'll report what I remember. It is a #lang 
> racket/gui program, built by `raco setup` in response to 
> `gracket-launcher-libraries` and `gracket-launcher-names` fields, so things 
> may be different if you're writing some other kind of program.
>
> I believe the hook for getting files passed from Finder at program startup 
> is `application-file-handler 
> `.
>  
> (It also works with Windows.) 
>
> There are also some auxiliary files you want to create to configure 
> certain things for `raco setup`'s implicit use of `build-aux-from-path 
> `
>  
> and `extract-aux-from-path 
> `.
>  
> I believe the key one is a ".filetypes" file. There is more documentation 
> on the format under `create-embedding-executable 
> `.
>  
> Mine is:
> Sapientia:freecell philip$ cat freecell.filetypes 
> ((("CFBuncleTypeName"
>"Freecell Game")
>   ("CFBundleTypeRole"
>"Viewer")
>   ("CFBundleTypeExtensions"
>(array "freecell"))
>   ("CFBundleTypeIconFile" "file-icon")
>  ))
> The "CFBundleTypeIconFile" entry directs `raco setup` to use 
> "file-icon.icns" as the icon for files opened by the application.
>
> Hope this helps!
>
> -Philip
>
> P.S.: I'll also mention that I recently started working on a Racket 
> library to write .icns icon files instead of shelling out to Apple's `sips` 
> to convert them from some other format. Pull requests are welcome! The code 
> is at https://github.com/LiberalArtist/icns and the docs are at 
> https://docs.racket-lang.org/icns/index.html
>
>
> On Wed, Sep 5, 2018 at 12:53 PM Lehi Toskin  > wrote:
>
>> I've been messing around with getting a test application to open files 
>> from Finder in OS X as a default app. The program appears to be operating 
>> properly, except for the part where it tells me information about the file 
>> I double-clicked. I modified the app a little so that it would print out 
>> any args that were passed to it into an editor-canvas, but all that showed 
>> up was an empty vector. Is there something weird with how Finder passes 
>> files to apps, like some URI schema that my test app doesn't recognize? Or 
>> is it something going on with how the `racket/cmdline` library operates 
>> that is incompatible with Finder?
>>
>> P.S. Running my test app from the terminal allows it to work properly, so 
>> I wonder what's the issue.
>>
>> -- 
>> 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 .
>> 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] How to tighten the contract on a function

2018-09-05 Thread Alexander McLin
Something like the following program:

#lang racket

(module foomod racket
  (provide foo)
  (define/contract (foo arg) (-> string? #t) #t))

(require 'foomod)
(provide (contract-out (foo (-> non-empty-string? #t




On Tuesday, September 4, 2018 at 9:00:06 PM UTC-4, David K. Storrs wrote:
>
>
>
> On Tue, Sep 4, 2018 at 8:55 PM, Matthew Butterick  > wrote:
>
>>
>> On Sep 4, 2018, at 3:54 PM, David Storrs > > wrote:
>>
>> Say I have this (possibly from a third-party module):
>>
>>   (define/contract (foo arg) (-> string? #t) #t)
>>
>> I want to ensure that the argument is always non-empty, so I tighten the 
>> contract.  I could do this:
>>
>>   (set! foo (contract (-> non-empty-string? #t) foo 'foo 'neg))
>>
>> Mutating the function is pretty ugly, but I'm not sure of a better way.  
>>
>>
>>
>> Seems like this technique would break down with imported identifiers, for 
>> instance:
>>
>> ;
>>
>> #lang racket
>>
>> (module foomod racket
>>   (provide foo)
>>   (define/contract (foo arg) (-> string? #t) #t))
>> (require 'foomod)
>>
>> (set! foo (contract (-> non-empty-string? #t) foo 'foo 'neg))
>>
>> (foo "") ; error: set!: cannot mutate module-required identifier in: foo
>>
>>
>>
>>
> Ah, good point.  I'd realized that the tighter version would be visible 
> only in the current module, but I hadn't noticed that you actually can't 
> mutate imports.  Thanks.
>
>
>> I am left with the following questions:
>>
>> 1) Is there a better way to do this?
>>
>>
>> Why not the old prefix-and-wrap?
>>
>> ;
>>
>> #lang racket
>>
>> (module foomod racket
>>   (provide foo)
>>   (define/contract (foo arg) (-> string? #t) #t))
>> (require (prefix-in lax: 'foomod))
>>
>> (define foo (contract (-> non-empty-string? #t) lax:foo 'foo 'neg))
>>
>> (foo "")
>>
>>
> That works.  
>
>
>>
>> 2) If I use this method in a module other than the one where foo was 
>> defined, my expectation is that I would affect it only in the current 
>> module but that other importers would not see the change.  Is this right?
>>
>>
>> When you export this new `foo`, it carries the new contract, of course. 
>> (The old one remains but because your wrapper contract is tighter, any 
>> contract blame will happen in the new contract)
>>
>
> Great, thanks.  This does exactly what I need. 
>
>

-- 
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] Racket and OS X Finder

2018-09-05 Thread Philip McGrath
I have built toy application as a learning project that opens files in
Finder as a default app. I haven't worked on it in a while, so I may be
forgetting some things, but I'll report what I remember. It is a #lang
racket/gui program, built by `raco setup` in response to
`gracket-launcher-libraries` and `gracket-launcher-names` fields, so things
may be different if you're writing some other kind of program.

I believe the hook for getting files passed from Finder at program startup
is `application-file-handler
`.
(It also works with Windows.)

There are also some auxiliary files you want to create to configure certain
things for `raco setup`'s implicit use of `build-aux-from-path
`
and `extract-aux-from-path
`.
I believe the key one is a ".filetypes" file. There is more documentation
on the format under `create-embedding-executable
`.
Mine is:
Sapientia:freecell philip$ cat freecell.filetypes
((("CFBuncleTypeName"
   "Freecell Game")
  ("CFBundleTypeRole"
   "Viewer")
  ("CFBundleTypeExtensions"
   (array "freecell"))
  ("CFBundleTypeIconFile" "file-icon")
 ))
The "CFBundleTypeIconFile" entry directs `raco setup` to use
"file-icon.icns" as the icon for files opened by the application.

Hope this helps!

-Philip

P.S.: I'll also mention that I recently started working on a Racket library
to write .icns icon files instead of shelling out to Apple's `sips` to
convert them from some other format. Pull requests are welcome! The code is
at https://github.com/LiberalArtist/icns and the docs are at
https://docs.racket-lang.org/icns/index.html


On Wed, Sep 5, 2018 at 12:53 PM Lehi Toskin  wrote:

> I've been messing around with getting a test application to open files
> from Finder in OS X as a default app. The program appears to be operating
> properly, except for the part where it tells me information about the file
> I double-clicked. I modified the app a little so that it would print out
> any args that were passed to it into an editor-canvas, but all that showed
> up was an empty vector. Is there something weird with how Finder passes
> files to apps, like some URI schema that my test app doesn't recognize? Or
> is it something going on with how the `racket/cmdline` library operates
> that is incompatible with Finder?
>
> P.S. Running my test app from the terminal allows it to work properly, so
> I wonder what's the issue.
>
> --
> 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] How to tighten the contract on a function

2018-09-05 Thread Alexander McLin
What about reproviding foo and using contract-out to wrap foo in a new 
contract?





On Tuesday, September 4, 2018 at 9:00:06 PM UTC-4, David K. Storrs wrote:
>
>
>
> On Tue, Sep 4, 2018 at 8:55 PM, Matthew Butterick  > wrote:
>
>>
>> On Sep 4, 2018, at 3:54 PM, David Storrs > > wrote:
>>
>> Say I have this (possibly from a third-party module):
>>
>>   (define/contract (foo arg) (-> string? #t) #t)
>>
>> I want to ensure that the argument is always non-empty, so I tighten the 
>> contract.  I could do this:
>>
>>   (set! foo (contract (-> non-empty-string? #t) foo 'foo 'neg))
>>
>> Mutating the function is pretty ugly, but I'm not sure of a better way.  
>>
>>
>>
>> Seems like this technique would break down with imported identifiers, for 
>> instance:
>>
>> ;
>>
>> #lang racket
>>
>> (module foomod racket
>>   (provide foo)
>>   (define/contract (foo arg) (-> string? #t) #t))
>> (require 'foomod)
>>
>> (set! foo (contract (-> non-empty-string? #t) foo 'foo 'neg))
>>
>> (foo "") ; error: set!: cannot mutate module-required identifier in: foo
>>
>>
>>
>>
> Ah, good point.  I'd realized that the tighter version would be visible 
> only in the current module, but I hadn't noticed that you actually can't 
> mutate imports.  Thanks.
>
>
>> I am left with the following questions:
>>
>> 1) Is there a better way to do this?
>>
>>
>> Why not the old prefix-and-wrap?
>>
>> ;
>>
>> #lang racket
>>
>> (module foomod racket
>>   (provide foo)
>>   (define/contract (foo arg) (-> string? #t) #t))
>> (require (prefix-in lax: 'foomod))
>>
>> (define foo (contract (-> non-empty-string? #t) lax:foo 'foo 'neg))
>>
>> (foo "")
>>
>>
> That works.  
>
>
>>
>> 2) If I use this method in a module other than the one where foo was 
>> defined, my expectation is that I would affect it only in the current 
>> module but that other importers would not see the change.  Is this right?
>>
>>
>> When you export this new `foo`, it carries the new contract, of course. 
>> (The old one remains but because your wrapper contract is tighter, any 
>> contract blame will happen in the new contract)
>>
>
> Great, thanks.  This does exactly what I need. 
>
>

-- 
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] Racket and OS X Finder

2018-09-05 Thread Lehi Toskin
I've been messing around with getting a test application to open files from 
Finder in OS X as a default app. The program appears to be operating 
properly, except for the part where it tells me information about the file 
I double-clicked. I modified the app a little so that it would print out 
any args that were passed to it into an editor-canvas, but all that showed 
up was an empty vector. Is there something weird with how Finder passes 
files to apps, like some URI schema that my test app doesn't recognize? Or 
is it something going on with how the `racket/cmdline` library operates 
that is incompatible with Finder?

P.S. Running my test app from the terminal allows it to work properly, so I 
wonder what's the issue.

-- 
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] Semaphore-count

2018-09-05 Thread Jos Koot
Hi Craig Allen
Would something like the following work?
Jos
 
(define (make-sema n)
 (define-syntax-rule (with-counter-sema expr ...)
  (begin
   (semaphore-wait counter-sema)
   (define result (begin expr ...))
   (semaphore-post counter-sema)
   result))
 (define counter-sema (make-semaphore 1))
 (define counter n)
 (define sema (make-semaphore n))
 (define (wait) (with-counter-sema (semaphore-wait sema) (set! n (sub1 n
 (define (post) (with-counter-sema (semaphore-post sema) (set! n (add1 n
 (define (count) (with-counter-sema n))
 (values wait post count))
 
(define-values (wait post count) (make-sema 3))
(count)
(wait)
(count)
(post)
(count)


  _  

From: racket-users@googlegroups.com [mailto:racket-users@googlegroups.com] On 
Behalf Of George Neuner
Sent: 04 September 2018 17:47
To: Craig Allen
Cc: racket users
Subject: Re: [racket-users] Semaphore-count



On 9/4/2018 8:31 AM, Craig Allen wrote:


I saw that function, but was scared off by its documentation:

Like semaphore-wait, but semaphore-try-wait? never blocks execution. If sema's 
internal counter is zero, semaphore-try-wait? returns
#f immediately without decrementing the counter. If sema's counter is positive, 
it is decremented and #t is returned.


I'm not sure why I'd want it to decrement the count, I really just want to see 
if my critical section is running. I will try it
though, thanks.




'wait' takes the semaphore when it is available.  'try-wait' takes the 
semaphore if possible, or fails immediately if it can't.  

AFAIK there is no way to simply 'check' if the semaphore is available.  In a 
multithread environment, such checking doesn't do much
good anyway ... a positive counter value doesn't mean you actually can take a 
semaphore - some other thread(s) may beat you to it.

The canon [language independent] methods of "optimistic" locking are either to 
spin until 'try' returns true, or to combine one or a
few initial probes using 'try' with a fall back to a normal wait if the probes 
fail.

George


-- 
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.