[racket-users] ANN: with-cache

2016-11-22 Thread Ben Greenman
with-cache is a small package for saving the results of an expression to
the filesystem

#lang racket/base
(require pict with-cache)

(define (get-fish)
  (with-cache (cachefile "stdfish.rktd")
(λ () (standard-fish 100 50

(get-fish) ;; Builds a fish
(get-fish) ;; Reads a fish from "./compiled/with-cache/stdfish.rktd"



Run the above with `racket -W info@with-cache file.rkt` to see when cache
files get written and read.

By default, cached data is serialized, written in fasl format, and
invalidated if read by a different version of the with-cache library. You
can override all these options; the docs should explain how.

Docs (latest update is yet to sync on the package server):
http://docs.racket-lang.org/with-cache/index.html

Code:
https://github.com/bennn/with-cache

History:
I started writing this code while working on a Scribble document with lots
of picts in it. (Most of the picts were made by plot. Tweaking plot
parameters led to the *current-cache-keys* parameter.)
William Bowman encouraged me to put an early version of this code on the
package server.
Leif Andersen pointed out LOTS of places where the original code needed to
improve.

-- 
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] Contract on a parameter’s value as a function precondition?

2016-11-22 Thread Alexis King
I have a function that requires a parameter be set to a value satisfying
a particular contract, but I don’t see any direct way to specify that
constraint using the contract system. It seems possible to emulate using
#:pre from ->* and ->i, but this has two problems: it doesn’t produce
very good error messages, and it doesn’t let me provide an arbitrary
contract.

The latter issue seems a little trickier to implement, since I don’t
think there’s any specific support in Racket’s existing interposition
layer. Neither impersonate-procedure nor impersonate-procedure* provide
any control over the dynamic extent of the call, so it isn’t possible to
adjust the parameterization. It’s possible to create an extremely simple
contract that creates an entirely new procedure instead of a chaperone
or impersonator:

  (define (parameterization-> param-name param val/c)
(make-contract
 #:name `(parameterization-> ,param-name
 ,(contract-name val/c))
 #:projection
 (λ (blame)
   (let ([blame* (blame-add-context
  blame #:swap? #t
  (format "the value of (~a)" param-name))])
 (λ (val)
   (λ args
 (parameterize ([param (((contract-projection val/c) blame*)
(param))])
   (apply val args

…but this seems like it probably has some drawbacks. Is there a better
way to do this? And in any case, would this be something useful to add
to ->* or ->i?

Alexis

-- 
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: What is the Racket equivalent to 'return' for early exit?

2016-11-22 Thread Ronie Uliana
Em segunda-feira, 21 de novembro de 2016 16:08:40 UTC-2, Leif Andersen  
escreveu:
> Honestly, I personally like to use let/ec for this. I know it's still using 
> continuations, but it is much more lightweight, both syntactically and in 
> terms of run-time costs.
> 
> (define (do-something)
> 
>   (let/ec return
>     (unless (some-condition)
>       (return NO))
> 
>     (do-the-thing)))
> 
> Although honestly, with this pattern, I find that errors work better here, as 
> they return early, and you can decide how they get handled with a 
> with-handlers.
> 
> (define (do-something)
>   (unless (some-condition)
>     (error "NO"))
>   (do-the-thing))
> 
> (with-handlers ([exn:fail? (lambda (e) (displayln "I returned early"))])
>   (do-something))
> 
> But that is specifically because I prefer the workflow of catching errors, 
> rather than always having to manually check the output of the function 
> (I'm looking at you C...)
> 
> Hope that helps.
> 
> 
> 
> 
> 
> 
> 
> ~Leif Andersen
> 
> 
> On Mon, Nov 21, 2016 at 12:41 PM, David Storrs  wrote:
> 
> 
> Edit:  I know I could also use call/cc and invoke the continuation to escape, 
> but that still adds another layer of indentation for something that in the 
> normal case won't be called.
> 
> It's not a big deal, but I was wondering about it.
> 
> 
> 
> 
> 
> On Mon, Nov 21, 2016 at 9:37 AM, David Storrs  wrote:
> 
> 
> 
> 
> In Perl I would often write:
> 
> sub do_something {
>     return unless ( some necessary condition is met );
> 
>     ... do the thing ...
> 
> }
> 
> In Racket I could wrap the rest of the procedure in an (if), but that adds an 
> unnecessary level of indentation and feels clunky.  Is there a clean solution?
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> 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.

Just notice I replied the digest email instead of post a response. :p

So, let me try again and sorry for the mess...

I made this macro for exactly for the same reason (it's basically Leif's 
answer) :)

https://gist.github.com/ruliana/80d7bb46225a22a3682711ca8bd11a1d

@JohnClements, I guess you have a point :) but I think David is mostly talking 
about what we call "Guard Clauses" 
(http://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html), 
good or bad, they are a tool for simplifying code. But I agree, don't overdo it 
^_^

Macro copied below for convenience (btw, sorry for the code, Racket is not my 
main programming language... yet)


#lang racket
(require racket/stxparam)

(provide return
 define/guard)

(define-syntax-parameter return
(λ (stx)
  (raise-syntax-error (syntax-e stx) "can only used inside 
\"define/guard\"")))

(define-syntax-rule (define/guard (name args ...) body0 body ...)
(define (name args ...)
  (let/cc ret
(syntax-parameterize ([return (make-rename-transformer #'ret)])
  body0
  body ...

(define/guard (test2 a b)
  (when (> a b) (return 456))
  (displayln "hehehe")
  123)

-- 
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 backwards compatible to schemer books?

2016-11-22 Thread George Neuner

Hi Tim,

On 11/18/2016 2:20 PM, Tim Johnson wrote:

Hello racketeers :

I retired as a coder. Now I can write in whatever programming
language I wish to.

I'd like to learn racket. I intend to start by working off of the
Schemer books. I have the first three.

Is racket fully compatible to the code in the schemer books?
If not, is there a site that would detail any incompatibilities?

Thanks


I don't think there is any definitive list of incompatibilities.

The biggest is that Racket has immutable list pairs (cons cells). In the 
module language (#lang racket), set-car! and set-cdr! are not defined.  
There is a separate mutable pair type, mcons, that can be used instead.


The R_RS languages (e.g., #lang r5rs, #lang r6rs)  alias cons, set-car! 
and set-cdr! to the mutable mcons type.  You can use lists/pairs 
normally in R_RS code, but if you try to examine a list in the REPL you 
will see the difference: e.g.


#lang r5rs
(define p (list 'a 'b))

=> p
(mcons 'a (mcons 'b '()))
=>


If you're working from a Scheme book, you probably should start with 
R5RS (#lang r5rs).


But see:
https://docs.racket-lang.org/r5rs/index.html?q=r5rs
https://docs.racket-lang.org/reference/pairs.html?q=pairs
https://docs.racket-lang.org/reference/mpairs.html?q=pairs

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] Racket backwards compatible to schemer books?

2016-11-22 Thread Sam Caldwell
I think there are some minor incompatibilities. For example, the seasoned
schemer uses `letcc` whereas in racket you would write `let/cc`. I don't
know if these are catalogued somewhere.

- Sam Caldwell

On Fri, Nov 18, 2016 at 2:20 PM, Tim Johnson  wrote:

> Hello racketeers :
>
> I retired as a coder. Now I can write in whatever programming
> language I wish to.
>
> I'd like to learn racket. I intend to start by working off of the
> Schemer books. I have the first three.
>
> Is racket fully compatible to the code in the schemer books?
> If not, is there a site that would detail any incompatibilities?
>
> Thanks
> --
> Tim
> http://www.akwebsoft.com, http://www.tj49.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.
> 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] Testing - mails sent from desktop mail client not received

2016-11-22 Thread Tim Johnson
* Tim Johnson  [161118 13:02]:
> I am sending this from my browser, pointed at 
> https://groups.google.com/forum/#!newtopic/racket-users as a test.
> 
> Although I have been receiving emails sent to the Racket Users email list 
> (racket-users@googlegroups.com) as a subscriber, emails sent by me to the 
> same list via my desktop email client do not appear to be received or 
> distributed. 
> 
> This is a test.
> thanks
  This is a second test. I am replying from my topic opener from my
  mail client (mutt on ubuntu 14.04).

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


[racket-users] Racket backwards compatible to schemer books?

2016-11-22 Thread Tim Johnson
Hello racketeers :

I retired as a coder. Now I can write in whatever programming
language I wish to. 

I'd like to learn racket. I intend to start by working off of the
Schemer books. I have the first three.

Is racket fully compatible to the code in the schemer books?
If not, is there a site that would detail any incompatibilities?

Thanks
-- 
Tim 
http://www.akwebsoft.com, http://www.tj49.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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Digest for racket-users@googlegroups.com - 10 updates in 1 topic

2016-11-22 Thread Ronie Uliana
I made this macro for exactly for the same reason :)

https://gist.github.com/ruliana/80d7bb46225a22a3682711ca8bd11a1d

@JohnClements, I guess you have a point :) but I think David is mostly
talking about what we call "Guard Clauses" (
http://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html),
good or bad, they are a tool for simplifying code. But I agree, don't
overdo it ^_^

Macro copied below for convenience (btw, sorry for the code, Racket is not
my main programming language... yet)


#lang racket (require racket/stxparam)
(provide return
define/guard)
(define-syntax-parameter return
(λ (stx)
(raise-syntax-error (syntax-e stx) "can only used inside \"define/guard\""
)))
(define-syntax-rule (define/guard (name args ...) body0 body ...)
(define (name args ...)
(let/cc ret
(syntax-parameterize ([return (make-rename-transformer #'ret)])
body0
body ...
(define/guard (test2 a b)
(when (> a b) (return 456))
(displayln "hehehe")
123)

Em ter, 22 de nov de 2016 às 05:24, 
escreveu:

> racket-users@googlegroups.com
> 
>  Google
> Groups
> 
> 
> Topic digest
> View all topics
> 
>
>- What is the Racket equivalent to 'return' for early exit?
><#m_-7182901911078251811_group_thread_0> - 10 Updates
>
> What is the Racket equivalent to 'return' for early exit?
> 
> David Storrs : Nov 21 09:37AM -0800
>
> In Perl I would often write:
>
> sub do_something {
> return unless ( some necessary condition is met );
> ... do the thing ...
> }
>
> In Racket I could wrap the rest of the procedure in an (if), but that adds
> an unnecessary level of indentation and feels clunky. Is there a clean
> solution?
> David Storrs : Nov 21 09:41AM -0800
>
> Edit: I know I could also use call/cc and invoke the continuation to
> escape, but that still adds another layer of indentation for something that
> in the normal case won't be called.
>
> It's not a big deal, but I was wondering about it.
>
> On Mon, Nov 21, 2016 at 9:37 AM, David Storrs 
> wrote:
>
> George Neuner : Nov 21 01:04PM -0500
>
> On 11/21/2016 12:41 PM, David Storrs wrote:
>
> > In Racket I could wrap the rest of the procedure in an (if), but
> > that adds an unnecessary level of indentation and feels clunky.
> > Is there a clean solution?
>
> In Racket, IF requires both alternatives be specified. What's wrong
> with WHEN or UNLESS?
>
> (define (do-something)
> (unless (some necessary condition is met)
> ... do the thing(s) ...)
> )
>
> George
> Leif Andersen : Nov 21 01:07PM -0500
>
> Honestly, I personally like to use let/ec for this. I know it's still using
> continuations, but it is much more lightweight, both syntactically and in
> terms of run-time costs.
>
> (define (do-something)
> (let/ec return
> (unless (some-condition)
> (return NO))
>
> (do-the-thing)))
>
> Although honestly, with this pattern, I find that errors work better here,
> as they return early, and you can decide how they get handled with a
> with-handlers.
>
> (define (do-something)
> (unless (some-condition)
> (error "NO"))
> (do-the-thing))
>
> (with-handlers ([exn:fail? (lambda (e) (displayln "I returned early"))])
> (do-something))
>
> But that is specifically because I prefer the workflow of catching errors,
> rather than always having to manually check the output of the function
> (I'm looking at you C...)
>
> Hope that helps.
>
>
> ~Leif Andersen
>
> On Mon, Nov 21, 2016 at 12:41 PM, David Storrs 
> wrote:
>
> "John Clements" : Nov 21 01:30PM -0500
>
> > ... do the thing ...
> > }
>
> > In Racket I could wrap the rest of the procedure in an (if), but that
> adds an unnecessary level of indentation and feels clunky. Is there a clean
> solution?
>
> Playing the role of irritating ideologue, it’s … me!
>
> Yes, there’s let/ec, but my (limited) experience in industry suggests that
> reading functions that use ‘return’ liberally can be massively more
> difficult than one that uses ‘if’ or a related form. I would argue that the
> call/ec doesn’t actually make the code less clunky, it just hides the
> clunkiness, and makes the code harder to read, to boot. In the absence of
> return, you can generally easily deduce when control flow reaches a
> particular point (“we only get here if x is > 0 and the string is empty”).
> In the presence of return, this becomes “we only get here if x is > 0 and
> the string is empty and one of these 19