Re: [racket-users] Contracts in lambda?

2019-07-10 Thread Matthias Felleisen


[(contract (-> string? integer?) (λ (x) x) 'a 'b)
 "hello”]




> On Jul 10, 2019, at 10:11 PM, Kevin Forchione  wrote:
> 
> Hi guys,
> Is there a way to apply contracts to a lambda without associating it with an 
> identifier? What I want is to be able to pass lambda functions around without 
> having to bind them to a symbol. I could use raise-argument-error/ 
> raise-result-error but the contract system is more powerful and  I’m hoping 
> there’s a wrapperI of some sort that will do the trick. Something like:
> 
> (assert-contract [tag] (lambda argspec contract-spec body …)) ;; <= [ta] 
> would just be a symbol for the error message 
> 
> Kevin
> 
> -- 
> 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/B0725B4A-8D61-4F6C-AE31-9A7E067F82AE%40gmail.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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/90A0CD6C-1349-4BB7-B51A-9D83E6271CE9%40felleisen.org.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Contracts in lambda?

2019-07-10 Thread Kevin Forchione
Hi guys,
Is there a way to apply contracts to a lambda without associating it with an 
identifier? What I want is to be able to pass lambda functions around without 
having to bind them to a symbol. I could use raise-argument-error/ 
raise-result-error but the contract system is more powerful and  I’m hoping 
there’s a wrapperI of some sort that will do the trick. Something like:

(assert-contract [tag] (lambda argspec contract-spec body …)) ;; <= [ta] would 
just be a symbol for the error message 

Kevin

-- 
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/B0725B4A-8D61-4F6C-AE31-9A7E067F82AE%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Advice for porting Interactive Brokers API to Racket

2019-07-10 Thread evdubs
Hi,

I am currently investigating the effort required to port the Interactive 
Brokers Client API  
to Racket. In general, there is not much complexity in the client; it 
establishes an open socket, builds byte buffers to send to the server, and 
receives byte buffer responses. Some of the code declares classes that can 
be used by the client that can serialize to and deserialize from the byte 
buffers being sent.

There are some warts in the implementation. For example, there are sections 
like the following:

private static final int MIN_SERVER_VER_REAL_TIME_BARS = 34;
private static final int MIN_SERVER_VER_SCALE_ORDERS = 35;
private static final int MIN_SERVER_VER_SNAPSHOT_MKT_DATA = 35;
private static final int MIN_SERVER_VER_SSHORT_COMBO_LEGS = 35;
private static final int MIN_SERVER_VER_WHAT_IF_ORDERS = 36;

These values represent new feature releases that introduce things like 
receiving real time bars, submitting scale orders (a way to break up a 
large order into smaller orders that can scale as prices move favorably), 
receiving a snapshot of bids and asks for a financial instrument (market 
data), and submitting orders with conditions. These server version 
declarations are paired with code like:

public synchronized void placeOrder(int id, Contract contract, Order order) 
{
  ...
  if (m_serverVersion < MIN_SERVER_VER_SCALE_ORDERS) {
if (order.scaleInitLevelSize() != Integer.MAX_VALUE ||
order.scalePriceIncrement() != Double.MAX_VALUE) {
  error(id, EClientErrors.UPDATE_TWS,
"  It does not support Scale orders.");
  return;
}
  }
  ...
  if (m_serverVersion >= MIN_SERVER_VER_SCALE_ORDERS) {
if (m_serverVersion >= MIN_SERVER_VER_SCALE_ORDERS2) {
  buffer.sendMax(order.scaleInitLevelSize());
  buffer.sendMax(order.scaleSubsLevelSize());
}
else {
  buffer.send("");
  buffer.sendMax(order.scaleInitLevelSize());
}
buffer.sendMax(order.scalePriceIncrement());
  }
  ...
}

In the above, buffer is a byte buffer that is prepared to be sent along to 
the server. In this example, there are maybe 20 different buffers that can 
be built that just represent an order to send, and they all depend on the 
server version. It may have been a nicer design to have a small set of 
fields that every order shares and a map of extra fields for special 
orders, but we can't redesign the server-side of things.

I am wondering if there is a better way in Racket to port this code than to 
just have a "straight port" of code like:

(cond [(>= server-version 'scale-orders-version)
   (vector-append buffer (vector (order-scale-init-level-size order) 
(order-scale-subs-level-size order)))])

Is there a clearer way of representing capabilities of a server and have 
that be responsible for object serialization in Racket? Has anyone ported 
code to Racket with implementations like the above and found better ways of 
structuring the code in Racket?

Curiously,

Evan

-- 
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/e9507c6b-aa74-4605-a1ff-44dc1bef6d23%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Re: Thinking in scheme / racket

2019-07-10 Thread Bob Heffernan
On 19-07-10 02:46, Maciek Godek wrote:
> A while ago, I wrote a booklet which used almost the same problem to 
> introduce to, what you called nicely in the title of this thread, "thinking 
> in Scheme", so if you're interested, you may want to check out the first 
> chapter ("Introduction"):

Maciek,

Thank you for your reply.

I skimmed through your booklet some time ago.  (I too dislike the R
language, although some attempts have been made in recent years to make
it less awful).  I read through the introduction again this morning and
enjoyed it.

The reason I like racket (and scheme-like languages in general) is that
they encourage the style of programming you are advocating (I think)
where the program is expressive and can be read and appreciated by
humans.  In theory, my favourite style of programming is one that is
elegant and readable by humans.

My original email had to do with the problem of when this comes into
conflict with a prosaic computational task where my main aim is simply
to get the job done efficiently and my brain defaults to writing the
program in something like C.

For instance, another way of writing something like my original program
might be something like:

(define (f n) (- (expt 2 n) 3))
(define (good? n) (and (positive? n) (prime? n)))
(length (filter good? (map f (range 1 100

which is, I think, fairly expressive.  It is still, however, relatively
inefficient.  A python programmer might write something like

count = 0
for n in range(1, 1000):
if 2 ** n - 3 >= 0 and isprime(2 ** n - 3):
  count += 1

print(count)

and happily report that her program ran quite a bit faster than mine.

I'm not sure how I'd advocate for Racket (or scheme) in a situation like this.

Regards,
Bob

-- 
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/20190711013633.nm2jlznhzwibhsli%40bob-cit.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Capturing print?

2019-07-10 Thread Kevin Forchione


> On Jul 10, 2019, at 1:53 PM, bruno cuconato  wrote:
> 
> there's 
> https://docs.racket-lang.org/reference/port-lib.html#%28def._%28%28lib._racket%2Fport..rkt%29._with-output-to-string%29%29
>  
> 
>  and company.
> would that work for you?
> 
> -- bruno cuconato

Thanks! Yes, I think with a little tinkering I can get that to work for what I 
have in mind. Took me awhile to discover *how* that is supposed to work! 
Apparently the proc for with-output-to-string is a thunk:

(with-output-to-string (λ a
 (printf "a=~a~%" a)
 (define x 10)
 (define y 20)
 (printf "x=~a y=~a~%" x y)))

=> "a=()\nx=10 y=20\n”


Something a little macro can handle:

(define-syntax (output->string stx)
  (syntax-parse stx
[(_ body:expr ...) #'(with-output-to-string (thunk body ...))]))

(output->string
(define x 10)
  (define y 20)
  (printf "x=~a y=~a~%" x y))


=> "x=10 y=20\n"


Kevin

-- 
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/5D3552E4-F7E0-4223-8D99-A16C40E30FFE%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Capturing print?

2019-07-10 Thread bruno cuconato
there's
https://docs.racket-lang.org/reference/port-lib.html#%28def._%28%28lib._racket%2Fport..rkt%29._with-output-to-string%29%29
and company.
would that work for you?

-- bruno cuconato


On Wed, 10 Jul 2019 at 17:48, Kevin Forchione  wrote:

> Hi guys,
> Is there a wrapper or something like that that I can place around a print
> expression so that it captures it and returns a string value? Something
> like:
>
> [capture-print [printf “hello, world!”)) => “hello, world!”
>
> There are times when testing I’d like to capture the output and compare it
> with a check-equal? and suppress it from displaying in testing It could
> also be useful in other was possibly for delaying and manipulating printed
> output.
>
> Kevin
>
> --
> 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/CC768C18-C600-4136-8EF7-1A4AAC19BA25%40gmail.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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAPWro-izwc1mYkcL-uDKi-m2A2HWoa%3D5ZNL%2B9AJPp55yoMKytQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Capturing print?

2019-07-10 Thread Kevin Forchione
Hi guys,
Is there a wrapper or something like that that I can place around a print 
expression so that it captures it and returns a string value? Something like: 

[capture-print [printf “hello, world!”)) => “hello, world!”

There are times when testing I’d like to capture the output and compare it with 
a check-equal? and suppress it from displaying in testing It could also be 
useful in other was possibly for delaying and manipulating printed output. 

Kevin

-- 
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/CC768C18-C600-4136-8EF7-1A4AAC19BA25%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Seeking users to try file monitoring package

2019-07-10 Thread Ben Greenman
> I'm particularly curious if the (robust-watch) procedure provided by the
> package in particular behaves consistently across operating systems. I do
> not have a Mac and was hoping that a Mac+Racket user would be willing to try
> this out and report his or her experience.

(robust-watch) worked for me.

I'm on OSX El Capitan 10.11.1

Here's the terminal output:

$ racket main.rkt -m robusto ../a
Unrecognized method: #. Falling back to
robust watch.
Starting robust watch over paths:
-->  ../a

(robust add ../a/file.txt)
(robust add ../a/dirrr)
(robust change ../a/file.txt)
(robust remove ../a/file.txt)
(robust remove ../a)

-- 
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/CAFUu9R68BK3NKGpRBGriC%2BaByMcT99UhZg__RExLaXCF3WCaNw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Specializing functions accepting keywords

2019-07-10 Thread Jens Axel Søgaard
Consider the following fishy example wherein we try to
reuse a function with keyword arguments.


(define (fish #:name[name   #f]
  #:color   [color  #f]
  #:studio  [studio #f]
  #:eyes[eyes   2])
  (~a "The fish " name " is " color ", appears in a movie from " studio "
and has " eyes " eyes."))

Let's check see an example:

> (fish #:name "Nemo"   #:color "orange" #:studio "Disney")
"The fish Nemo is orange, appears in a movie from Disney and has 2 eyes."

A few examples later one quickly realizes that Disney has
a lot of fish in their movies:


(fish #:name "Nemo"   #:color "orange" #:studio "Disney")
(fish #:name "Dory"   #:color "blue"   #:studio "Disney")
(fish #:name "Marlin" #:color "orange" #:studio "Disney")
(fish #:name "Wanda"  #:color "gold"   #:studio "MGM")
(fish #:name "Blinky" #:color "orange" #:studio "Fox" #:eyes 3)


No worries, we can quickly define a  disney-fish  that simply
calls  fish  using the #:studio keyword and passes other keywords
along. The new  disney-fish must accept the same keywords as fish
(except for the studio one), so  procedure-reduce-keyword-arity
is needed to restrict the accepted keywords.


(define disney-fish
  (let ()
(define (make-disney-fish kws kw-args . rest)
  (keyword-apply/sort fish kws kw-args rest #:studio "Disney"))

(define-values (fish-required fish-allowed) (procedure-keywords fish))
(define fish-arity (procedure-arity fish)) ; number of by-position
arguments

(procedure-reduce-keyword-arity
   (make-keyword-procedure make-disney-fish)
   fish-arity
   (remove '#:studio fish-required)
   (remove '#:studio fish-allowed

Now we can write:

(disney-fish #:name "Nemo" #:color "orange")

The new  disney-fish  accepts only keywords accepted by fish,
so any error reporting works as expected.

But ... the above solution wasn't quick - it took more work than I
initially expected.
Am I missing something that makes reusing functions accepting
keyword arguments easier?

/Jens Axel


-- full example --


#lang racket

(require kw-utils/keyword-apply-sort)

(define (fish #:name[name   #f]
  #:color   [color  #f]
  #:studio  [studio #f]
  #:eyes[eyes   2])
  (~a "The fish " name " is " color ", appears in a movie from " studio "
and has " eyes " eyes."))

(fish #:name "Nemo"   #:color "orange" #:studio "Disney")
(fish #:name "Dory"   #:color "blue"   #:studio "Disney")
(fish #:name "Marlin" #:color "orange" #:studio "Disney")
(fish #:name "Blinky" #:color "orange" #:studio "Fox" #:eyes 3)

(define disney-fish
  (let ()
(define (make-disney-fish kws kw-args . rest)
  (keyword-apply/sort fish kws kw-args rest #:studio "Disney"))
(define-values (fish-required fish-allowed) (procedure-keywords fish))
(define fish-arity (procedure-arity fish)) ; number of by-position
arguments
(procedure-reduce-keyword-arity
 (make-keyword-procedure make-disney-fish)
 fish-arity
 (remove '#:studio fish-required)
 (remove '#:studio fish-allowed


(disney-fish #:name "Nemo" #:color "orange"
 #:studio "foo")

; Notes:
; > (disney-fish #:name "Nemo" #:color "orange" #:studio "Disney")
; correctly shows error (#:studio not expected)

-- 
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/CABefVgyXzWW1WJjC9SViyzsheswJD9Ek4LDfwqaNg_v4mT6y%3DQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Snip responds to scroll input

2019-07-10 Thread Kshitij Sachan
I want to make a snip that resizes based on vertical scroll input. I 
currently have the following snip code:

#lang racket

(require racket/gui
 racket/gui/base)

;; snip-class
(define test-snip-class
  (make-object
   (class snip-class%
 (inherit set-classname)
 
 (super-new)
 (set-classname "(lib test-snip)")
 ;; not sure if necessary
 (define/override (read f)
   (define w-b (box 0.0))
   (define h-b (box 0.0))
   (send f get w-b)
   (send f get h-b)
   (new scroll-snip% [w (unbox w-b)] [h (unbox h-b)]))
 )))

(define scroll-step-pixels 12)

;; snip stuff
(define scroll-snip%
  (class snip%
(inherit get-admin set-snipclass set-flags)
(init-field [w 250] [h 250])

(super-new)
(set-snipclass test-snip-class)
(send (get-the-snip-class-list) add test-snip-class)
(set-flags (list 'handles-events))

(define/override (get-extent dc x y width height descent space lspace 
rspace)
  (define (maybe-set-box! b v) (when b (set-box! b v)))
(maybe-set-box! width w)
(maybe-set-box! height h)
(maybe-set-box! descent 1.0)
(maybe-set-box! space 1.0)
(maybe-set-box! lspace 1.0)
(maybe-set-box! rspace 1.0))

(define/override (find-scroll-step y) (inexact->exact (round (/ y 
scroll-step-pixels
(define/override (get-num-scroll-steps) (quotient h scroll-step-pixels))
(define/override (get-scroll-step-offset step) (* step 
scroll-step-pixels))
  

(define/override (on-char dc x y editorx editory event)
  (cond
[(equal? (send event get-key-code) 'down)
 (define admin (get-admin))
 (set! w (+ w 20))
 (set! h (+ h 20))
 (when admin
   (send admin resized this #t))]
[(equal? (send event get-key-code) 'up)
 (define admin (get-admin))
 (set! w (- w 20))
 (set! h (- h 20))
 (when admin
   (send admin resized this #t))]
;; NOT WORKING!
[(equal? (send event get-key-code) 'wheel-up)
 (print "wheelup")
 (define admin (get-admin))
 (set! w (+ w 20))
 (set! h (+ h 20))
 (when admin
   (send admin resized this #t))]
[(equal? (send event get-key-code) 'wheel-down)
 (print "wheelup")
 (define admin (get-admin))
 (set! w (- w 20))
 (set! h (- h 20))
 (when admin
   (send admin resized this #t))]
[(equal? (send event get-key-code) 'escape)
 (print "wheelup")
 (define admin (get-admin))
 (set! w 150)
 (set! h 150)
 (when admin
   (send admin resized this #t))]))

(define/override (on-event dc x y editorx editory event)
  (cond
[(send event button-down?)
(define admin (get-admin))
(set! w (+ w 20))
(set! h (+ h 20))
(when admin
  (send admin resized this #t))]))
  
(define/override (draw dc x y left top right bottom dx dy draw-caret)
  (send dc set-brush "red" 'solid)
  (send dc set-pen "black" 3 'solid)
  (send dc draw-ellipse (+ x 1.0) (+ y 1.0) (- w 2) (- h 2)))

(define/override (copy)
  (new scroll-snip%))
))

I tried implementing this behavior in two different ways:
1. find-scroll-step, get-num-scroll-steps, and get-scroll-step-offset 
methods - I doubt this is the purpose these methods, but I couldn't find 
any examples online and the documentation wasn't entirely clear on what 
they do.
2. Overriding on-char - this seemed more promising because 'wheel-up and 
'wheel-down are listed as key codes for a key event, but my snip is still 
unresponsive to scrolling.

Worse, I suspect that it may not be possible to scroll within a snip in the 
interactions window because currently every time I scroll it just scrolls 
the interactions window itself (seems like the interactions window, rather 
than the snip, is in focus?). 

Please let me know how I can implement scrolling on my snip to resize.

-- 
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/7486b2c4-ce9b-4177-94ae-eafc867814e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Auto-generated AWS SDK

2019-07-10 Thread Greg Hendershott
It's definitely an interesting and smart approach, today.

I hope the following doesn't sound defensive; it's just context and
observations.

Keep in mind that my package dates back to 2012.

1. There were a half dozen AWS services. Of those few, S3 was the 90% use
   case.

2. There was no API spec for machine-generation. Because 1; why bother.


For most services, the only truly tricky and difficult part is
request-signing. Most services need one core function: You give it an
HTTP request, it gives you back the signed request (or makes the signed
request and gives you back the response). You could add many little
"wrapper" functions around this. But some users would be fine just
forming the request and using the core function to sign it. Especially
for the services that just consume and excrete blobs of JSON.


So, I can imagine at least two packages for each of the ever-growing
number of AWS services:

1. A tiny core request-signing library.

2. A wrapper library. Machine-generated. It uses 1.

Some people use 2. Others just use 1 directly.


As to who will plant the seed, harvest the wheat, grind the flour, and
bake the bread: My casual opinion is that most users of AWS are
eventually paying Amazon, and, many of the newer services are especially
clearly things that commercial entities will use. As a result, I think
that one or more companies using Racket with AWS to (try to) make money,
should do and/or fund any new work in this area.


p.s. Machine-generating wrappers doesn't mean there still won't be
non-trivial work, even if "just housekeeping". For N AWS services, there
are N*2 packages to maintain. Even more if one does the {lib doc test
meta} package split. CI scripts need to be updated for new versions of
Racket. And so on.

p.p.s. Although I wouldn't object if commercial entities using my
existing package wanted to recognize my work with a donation, I also
don't mind if they don't.

-- 
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/87muhmotga.fsf%40greghendershott.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Re: Thinking in scheme / racket

2019-07-10 Thread Maciek Godek

W dniu wtorek, 9 lipca 2019 14:09:04 UTC+2 użytkownik Bob Heffernan napisał:
>
> Dear all, 
>
> I recently wanted to count the number of primes in the sequences 2^n+3 
> and 2^n-3 (and a few more besides) where n is a positive integer. 
>
>
Hi!

A while ago, I wrote a booklet which used almost the same problem to 
introduce to, what you called nicely in the title of this thread, "thinking 
in Scheme", so if you're interested, you may want to check out the first 
chapter ("Introduction"):

https://github.com/panicz/pamphlet
 
HTH
Panicz

-- 
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/1240f18d-fcee-4daa-8497-7c66fcd22c0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.