Re: [racket-users] Calling a procedure from mysql

2016-03-28 Thread Ty Coghlan
On Monday, March 28, 2016 at 3:58:14 PM UTC-4, Ryan Culpepper wrote:
> On 03/24/2016 06:17 PM, Ty Coghlan wrote:
> > I have the following simple code:
> >
> > (require db)
> >
> > (define mdb (mysql-connect #:user user #:password password))
> > (query-exec mdb "use starwarsfinal")
> > (query mdb "CALL track_character(?)" "Chewbacca")
> > (disconnect mdb).
> >
> > Where track_character is a procedure that simply returns a simple
> > select statement. The query call to "Call track_character" fails with
> > the error "query: PROCEDURE starwarsfinal.track_character can't
> > return a result set in the given context SQLSTATE: 0A000", yet when I
> > run the select statement that the procedure runs as its own query, it
> > behaves as expected. I've noticed that this same error happens when
> > trying to call any procedure from mysql (functions work fine).
> > Looking this error up led me to find that it seems to be caused by
> > version errors, yet I have the most recent versions of both mysql and
> > racket. Any tips on how to figure this out?
> 
> The db library currently doesn't support MySQL CALL statements. You're 
> getting that error because the db library doesn't set the "can handle 
> multiple resultsets" flag when it connects to the server.
> 
> I can probably add support for CALL statements that return at most one 
> resultset pretty easily. I'll try to do that soon.
> 
> Adding support for multi-resultset CALL statements would be trickier; it 
> would require adding a new kind of structure that query can return, or 
> cramming multiple resultsets into the existing structures.
> 
> Ryan

That would fix the problems I was facing, thank you!
I may not have the best understanding of multi-sets, but couldn't a suitable 
value to return be either a racket set or a racket list of the structs that 
already represent a returned table?

-- 
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] Calling a procedure from mysql

2016-03-24 Thread Ty Coghlan
I have the following simple code: 

(require db)

(define mdb (mysql-connect #:user user #:password password))
(query-exec mdb "use starwarsfinal")
(query mdb "CALL track_character(?)" "Chewbacca")
(disconnect mdb).

Where track_character is a procedure that simply returns a simple select 
statement. The query call to "Call track_character" fails with the error 
"query: PROCEDURE starwarsfinal.track_character can't return a result set in 
the given context
  SQLSTATE: 0A000", yet when I run the select statement that the procedure runs 
as its own query, it behaves as expected. I've noticed that this same error 
happens when trying to call any procedure from mysql (functions work fine). 
Looking this error up led me to find that it seems to be caused by version 
errors, yet I have the most recent versions of both mysql and racket. Any tips 
on how to figure this out?

-- 
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: IO in racket is painful

2016-03-23 Thread Ty Coghlan
> I use racket for hackerrank and coding contests all the time, and I find it's 
> read syntax really useful. For instance, I would parse this into a list of 
> lists by doing 
> (for/list ([i (in-range (read))])
>   (map (lambda (x) (if (list? x) (cadr x) x)) (read))).
> 
> Then, to print out results, I normally do 
> (define (main n)
>   (unless (= 0 n)
> (begin (printf "~a ... ~a\n", args...)
>   (main (sub1 n.
> 
> Another really useful helper is 
> (define (read->list n)
>   (if (= 0 n) '()
> (cons (read) (read->list (sub1 n. 
> 
> You can customize these by doing for/vector, for/fold, etc., and there hasn't 
> been a hackerrank contest I've run into that I haven't been able to do with 
> some technique like this.

Correction, not "read syntax". My PL professor would fail me for that. I'd say 
that there are very few hackerrank and other contests input format that can't 
be parsed as an s-expression in some way (unless they do weird things with 
parentheses, in which case you have to use read-line/char/byte). 

Just note that a value with a comma in front of it is parsed as a list where 
the first item in the list is the unquote, and the second item is the value 
itself. Regardless, it's fairly easy to use the common list functions to 
extract the data you want.

-- 
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: IO in racket is painful

2016-03-23 Thread Ty Coghlan
On Wednesday, March 23, 2016 at 12:35:26 PM UTC-4, rom cgb wrote:
> Thanks all for the interesting replies. 
> 
> About using an external package, there also the case like on 
> www.hackerrank.com where you have to run the code in their own environment 
> (eg: http://i.imgur.com/iSSPLGy.png).

I use racket for hackerrank and coding contests all the time, and I find it's 
read syntax really useful. For instance, I would parse this into a list of 
lists by doing 
(for/list ([i (in-range (read))])
  (map (lambda (x) (if (list? x) (cadr x) x)) (read))).

Then, to print out results, I normally do 
(define (main n)
  (unless (= 0 n)
(begin (printf "~a ... ~a\n", args...)
  (main (sub1 n.

Another really useful helper is 
(define (read->list n)
  (if (= 0 n) '()
(cons (read) (read->list (sub1 n. 

You can customize these by doing for/vector, for/fold, etc., and there hasn't 
been a hackerrank contest I've run into that I haven't been able to do with 
some technique 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.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Re: Debug help with Unix Write

2016-02-12 Thread Ty Coghlan
Both of you were correct, I had to flush my output and newline terminate it. 
The final result looks like:

(define (broadcast source destination type message port)
  (let ([h (hash 'source source 'dest destination
 'type type 'message message)])
(fprintf port "~a\n" (jsexpr->string h))
(flush-output port))) 

I do have a couple of followup questions. Will flushing the output have issues 
when multiple bridges are active at once? The actual data packets are small 
enough that I doubt it'd overflow a buffer, if that helps. 
When I compile this program, I notice that it takes up 5MB of space. Is that 
because it's also compiling the c library that unix-socket wraps? Or is the 
JSON library that large?

Once again, thank you for your help!

-- 
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] Debug help with Unix Write

2016-02-12 Thread Ty Coghlan
As part of a course I'm in, we had to write a simple bridge simulation that 
could handle data messages to and from unix ports that have attached to them 
several hosts. In addition, these bridges have to implement a simple spanning 
tree algorithm, and send and receive BPDU messages. These messages are all 
formatted in JSON. My partner and I went about designing this in racket, 
changing the port protocol from SOCK_STREAM to SOCK_SEQPACKET (changing the 
binding of the variable from 1 to 5) in the unix library: 
https://github.com/racket/unix-socket. 

We ran into the problem where we were able to read messages from the hosts 
attached to the unix sockets, however, we could not read messages that we had 
sent out (which through the simulation get bounced back to the bridge so we can 
set up the spanning tree). This lead us to believe that there is a bug in how 
we write data to the unix socket, and we spent so long debugging it that we 
eventually just rewrote the program in python to make in before the deadline. 

All outgoing messages are written by a function called broadcast:

(define (broadcast source destination type message port)
  (write-json (hash 'source source 'destination destination
'type type 'message message)
  port))

It takes a string source, destination, message type, message, and finally an 
output port to write them on. Then it just wraps them up in a hash table and 
writes them to the port. We attempted to use display, print, regular write, and 
none of them alleviated the problem. We then looked at everything that passes 
data to broadcast, which includes the bpdu function

(define (bpdu bridge-id root-id cost-to-root lans)
  (for ([lan (hash-values lans)])
(broadcast bridge-id "" "bpdu"
   (jsexpr->string (hash 'id bridge-id 'root root-id 'cost 
cost-to-root))
   (cadr lan

bpdu takes in the current bridge-id, root-id, cost-to-root, and all lans (where 
lans are a three element list of lan id, out-port, and in-port), and simply 
formats them as a string before passing it as the message argument in broadcast 
to every port. This bpdu message is called from our main function, which sets 
up the unix socket connections and continuously loops, sending bpdus every 
500ms. The full (messy) file can be seen at 
https://github.com/cdris/bridges/blob/master/3700bridge.rkt.

Any help would be immensely appreciated. 

-- 
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] Embedding Rust in Racket

2015-11-09 Thread Ty Coghlan
It works correctly if I run it from the command line using the racket command, 
thanks! It also works if I raco exe it.

Whether or not to add lib to the name of the file seems to be operating system 
specific, or so I understand it. Windows seems to prefer leaving it as "embed". 
Any idea why DrRacket seems to redirect the output?

-- 
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] Embedding Rust in Racket

2015-11-09 Thread Ty Coghlan
I'm currently attempting to embed Rust into racket, following along with the 
tutorial at 
https://doc.rust-lang.org/stable/book/rust-inside-other-languages.html. For the 
most part I can get it to work when using functional programming in rust. I can 
call functions that return numbers/chars, however, I'm struggling to get side 
effects to work. For instance, when defining the "processs" function defined in 
the tutorial, I can not get the println!("Done!") (or the other print calls) to 
work in racket. I tried this both in drracket and with raco. The ffi call I 
used to attempt this is:

#lang racket/base
(require ffi/unsafe
 ffi/unsafe/define)

(define rust-lib (ffi-lib "target/release/embed"))
(define process (get-ffi-obj "process" rust-lib (_fun -> _int)))
(process)

Am I doing this correctly? Is this an issue I should take over to Rust's 
community, or are there certain steps I need to take in order for println to 
work?

Thanks!

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