Re: [racket-users] Help with vector-sort

2020-02-16 Thread greadey


On Saturday, 15 February 2020 13:03:44 UTC, Jens Axel Søgaard wrote:
>
> Den lør. 15. feb. 2020 kl. 13.44 skrev greadey  >:
>
>> I have written a programme to compute a bootstrapped mean etc.  I 
>> successfully wrote it using lists both untyped and in typed/racket.
>> I am interested in optimising the code and having seen that typed racket 
>> performs faster (for lists) I am interested in seeing if I get a 
>> performance increase using vectors in both typed and untyped code.
>> In order to compute the median value as well as certain percentiles it is 
>> necessary to sort the samples.  My specific problem is using vector-sort.  
>> I have got round it by using vector->list and list->vector functions and 
>> using the list sort function.
>> If I use vector-sort typed racket keeps telling me to use 
>> "require/typed".  I got a function definition to compile  (:sort-myvector : 
>> (Vectorof Flonum) (Flonum Flonum -> Boolean) -> (Vectorof Flonum))
>> I'm not sure if that is exactly right but it was something similar, 
>> however it did not work.  I did add a "require/typed racket/vector" to the 
>> expression.
>> So, how can I use vector-sort in typed racket code, and can anyone tell 
>> me if translating vectors to lists and back again actually introduces a 
>> performance hit? 
>>
>
> Here is an example of how to use `vector-sort` from a typed/racket program:
>
> #lang typed/racket
> (require/typed racket/vector
>   [vector-sort (-> (Vectorof Any) (-> Any Any Boolean) (Vectorof Any))])
>
> (define (compare x y)
>   (if (and (real? x) (real? y))
>   (< x y)
>   (error 'compare "expected two real numbers, got: ~a ~a" x y)))
>
> (vector-sort (vector 3 1 5 2) compare)
>  
> /Jens Axel
>
> Thanks Jens - Nice one!  I had to define vector-sort to accept and yield 
Real values in order to get it to work!  My understanding of typed 
programming is that if you (as you did) define a function to be 
polymorphic, it should be polymorphic.  However, copying your code exactly 
and running vector sort  on a vector of reals gives  the error;
Type Checker: type mismatch
  expected: (Vectorof Any)
  given: (Vectorof Real) in: orig

Maybe one day I'll understand this one :-)

-- 
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/da0772b1-02bc-4c12-9b8b-0a37e7c3107f%40googlegroups.com.


Re: [racket-users] Help with vector-sort

2020-02-15 Thread Jens Axel Søgaard
Den lør. 15. feb. 2020 kl. 13.44 skrev greadey :

> I have written a programme to compute a bootstrapped mean etc.  I
> successfully wrote it using lists both untyped and in typed/racket.
> I am interested in optimising the code and having seen that typed racket
> performs faster (for lists) I am interested in seeing if I get a
> performance increase using vectors in both typed and untyped code.
> In order to compute the median value as well as certain percentiles it is
> necessary to sort the samples.  My specific problem is using vector-sort.
> I have got round it by using vector->list and list->vector functions and
> using the list sort function.
> If I use vector-sort typed racket keeps telling me to use
> "require/typed".  I got a function definition to compile  (:sort-myvector :
> (Vectorof Flonum) (Flonum Flonum -> Boolean) -> (Vectorof Flonum))
> I'm not sure if that is exactly right but it was something similar,
> however it did not work.  I did add a "require/typed racket/vector" to the
> expression.
> So, how can I use vector-sort in typed racket code, and can anyone tell me
> if translating vectors to lists and back again actually introduces a
> performance hit?
>

Here is an example of how to use `vector-sort` from a typed/racket program:

#lang typed/racket
(require/typed racket/vector
  [vector-sort (-> (Vectorof Any) (-> Any Any Boolean) (Vectorof Any))])

(define (compare x y)
  (if (and (real? x) (real? y))
  (< x y)
  (error 'compare "expected two real numbers, got: ~a ~a" x y)))

(vector-sort (vector 3 1 5 2) compare)

/Jens Axel

-- 
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/CABefVgzOSTZJs%2BaZADFxe8oPJ%2BNx4ihvjHjJ19BcqhF2oKkayQ%40mail.gmail.com.