Re: [racket-users] table lookup shorthands?

2017-08-06 Thread Alex Knauth

> On Aug 6, 2017, at 6:16 PM, Jordan Johnson  wrote:
> 
> Hi all,
> 
> I’m writing some music-related code for which I find myself using a lot of 
> lookup tables, following the pattern of making an alist or hash table and 
> then writing a function that consults the table. I wound up writing a macro 
> for this pattern; one example of its use is:
> 
> ;; key->fifths : PitchName -> Int[-7..7]
> ;; tells how many perfect fifths above C the given pitch is (and
> ;; therefore how many sharps or flats its key signature has)
> (define-lookup-function key->fifths
>  [C 0] [G 1] [D 2] [A 3] [E 4] [B 5]
>  [F# 6] [F♯ 6]
>  [C# 7] [C♯ 7]
>  [F -1]
>  [Bb -2] [Eb -3] [Ab -4] [Db -5] [Gb -6] [Cb -7]
>  [B♭ -2] [E♭ -3] [A♭ -4] [D♭ -5] [G♭ -6] [C♭ -7])
> ;; Ex:
> (key->fifths 'Gb) ;; -> -6
> 
> This seems like a simple and common enough pattern that there must be such a 
> facility in some Racket library out there already, but I haven’t found one. 
> Am I reinventing a wheel here?
> 
> Thanks,
> Jordan

What I've done for situations like these is define notes like C and D not as 
symbols, but as data structures that contain the numbers you need. So for 
instance I would define:

(struct pitch-class [chromatic-number])

(define C (pitch-class 0))
(define C#/D♭ (pitch-class 1))
(define D (pitch-class 2))
(define D#/E♭ (pitch-class 3))
(define E (pitch-class 4))
(define F (pitch-class 5))
etc.

When the notes are defined like this, the number of fifths can be a 
mathematical function instead of a lookup, if you can find the function that 
maps chromatic numbers to fifths.
  

-- 
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] table lookup shorthands?

2017-08-06 Thread Jordan Johnson
Hi all,

I’m writing some music-related code for which I find myself using a lot of 
lookup tables, following the pattern of making an alist or hash table and then 
writing a function that consults the table. I wound up writing a macro for this 
pattern; one example of its use is:

;; key->fifths : PitchName -> Int[-7..7]
;; tells how many perfect fifths above C the given pitch is (and
;; therefore how many sharps or flats its key signature has)
(define-lookup-function key->fifths
  [C 0] [G 1] [D 2] [A 3] [E 4] [B 5]
  [F# 6] [F♯ 6]
  [C# 7] [C♯ 7]
  [F -1]
  [Bb -2] [Eb -3] [Ab -4] [Db -5] [Gb -6] [Cb -7]
  [B♭ -2] [E♭ -3] [A♭ -4] [D♭ -5] [G♭ -6] [C♭ -7])
;; Ex:
(key->fifths 'Gb) ;; -> -6

This seems like a simple and common enough pattern that there must be such a 
facility in some Racket library out there already, but I haven’t found one. Am 
I reinventing a wheel here?

Thanks,
Jordan

-- 
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] Anyone using MongoDB 3.2.15 with DrRacket 6.9?

2017-08-06 Thread Cecil McGregor
I've run the Quickstart and find some problems when
I start adding minor forms.

The QuickStart is at https://docs.racket-lang.org/mongodb/Quickstart.html

Here is the QuickStart code:

#lang racket
(require db/mongodb)

(define m (create-mongo))
(define d (make-mongo-db m "awesome-dot-com"))
(current-mongo-db d)
(define-mongo-struct post "posts"
  ([title #:required]
   [body #:required]
   [tags #:set-add #:pull]
   [comments #:push #:pull]
   [views #:inc]))
 
(define p
  (make-post #:title "Welcome to my blog"
 #:body "This is my first entry, yay!"))
(set-add-post-tags! p 'awesome)
(inc-post-views! p)
 
(set-post-comments! p (list "Can't wait!" "Another blog?"))
(post-comments p)


The output is as expected as well as the db validity:
(This was performed after  running db.dropDatabase() )
Welcome to DrRacket, version 6.9 [3m].
Language: racket, with debugging.
'#("Can't wait!" "Another blog?")
> (mongo-db? d)
#t
> (mongo-db-collections d)
'()<== Should show "posts" as a collection
> 
> (mongo-db-valid-collection? d "posts")
#t <== Internally it know "posts" exists

The mongo-db-collection should definitely show "posts"
as a collection. Running the mongo shell shows post:

use awesome-dot-com
switched to db awesome-dot-com
awesome-dot-com> show collections
posts<=== As expected

A few other calls don't quite seem to work either.

My question:

This code is old and written for Mongo 1.3. 
Does it work with the current 3.2.15 version?
(Apparently not. Am I wasting my time or should I be
prepared to  start hacking an upgrade?)

Is anyone using this 2011 code?

Does better documentation exist anywhere? (I can't find it
and I confess the current docs leave much to be desired.)

What are other people using for a NoSQL racket experience?

-- 
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: Need help porting a library to Typed Racket

2017-08-06 Thread Ray Racine
And back from the beach ...

So I did observe the opaque error when attempting to run:
 raco test test/unpack/extension.rkt

"A" way to clear that error is specify for TR what the procedure does
actually return as opposed to punting to Any.  unpack really doesn't return
Any-thing.  It returns a fixed, enumerable set of types.  Worth leveraging
the knowledge.

Type Racket inference was strong enough generously report errors until the
following was realized.:

(define-type MsgPack-Type (U Integer HashTableTop VectorTop String Real
Bytes EOF Void Boolean ext))

(: unpack (-> Input-Port MsgPack-Type))
(define (unpack ...) ...)

as opposed to (: unpack (-> Input-Port Any))

With that change:
$ raco test test/unpack/extension.rkt
raco test: (submod "test/unpack/extension.rkt" test)
OK, passed 100 tests.
OK, passed 100 tests.
OK, passed 100 tests.
OK, passed 100 tests.
OK, passed 100 tests.
OK, passed 100 tests.
OK, passed 100 tests.
7 tests passed

Note the EOF value is still being returned by the `unpack` procedure.  You
may or may not handle that case internal to the procedure.  i.e. (error
'Unrecoverable-EOF-Error ... ) or whatever.

Which is another point you raised which is that Typed Racket makes you
explicitly deal with fact that many IO routines can and will return EOFs.
Think of these as common bugs lurking in Untyped Racket code and that Typed
Racket is requiring to be treated with come error flow path that addresses
these error situations.

In IO-land stuff happens ... so given an unexpected, unrecoverable error
such as a socket has gone bad or similar and you get a EOF in many cases
raising an exception does make sense.  Sometimes something like an Either
or Option type style is more appropriate.  Sometimes it is just personal
preference.   IO is just landmines everywhere ...


Final thought.  The use of upper cased types is fairly routine in Typed
Racket.

Might consider.

 ;; Ext vs ext as struct's type and ctor name
(struct Ext ([type : Integer] [data : Bytes]))

giving
(define-type MsgPack-Type (U Integer HashTableTop VectorTop String Real
Bytes EOF Void Boolean Ext))





On Sun, Aug 6, 2017 at 4:00 PM, Ray Racine  wrote:

> Very much hurried and will look at it later ...
>
> 1. Don't use (file "xyz.rkt") just change it all to "xyz.rkt".  i.e. drop
> the (file ).
> 2. The dir MsgPack.rkt uses invalid chars (assume the '.') so change it to
> say "msgpack/"
> 3. See 1.7.3 Linking and Developing New Packages in Racket Docs.
>  Run '$ raco pkg install'  which will make your collection visible,
> including for tests.
>  e.g.
>  % raco test test/pack/binary.rkt
>  raco test: (submod "test/pack/binary.rkt" test)
> OK, passed 100 tests.
> OK, passed 100 tests.
> 2 tests passed
> 4.  With regard to the ext #:transparent and the
>   unpack: broke its own contract
>   any-wrap/c: Unable to protect opaque value passed as `Any`
>  value: #
>  I have hit this landmine before but as I recall usually on a
> (require/typed [#:opaque ...]) situation.
>  Try googling the error.  I recall some suggestion on a
> define-refined-subtype of the opaque structure or some such to allow the
> contract to wrap.   I'll definitely look at it tonight if someone else
> doesn't offer up a solution.
>
>
>
> On Sun, Aug 6, 2017 at 12:35 PM,  wrote:
>
>> *bump* I hope bumping is not frowned upon here. I just need to sort this
>> one last issue 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.
>>
>
>

-- 
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: Need help porting a library to Typed Racket

2017-08-06 Thread Ray Racine
Very much hurried and will look at it later ...

1. Don't use (file "xyz.rkt") just change it all to "xyz.rkt".  i.e. drop
the (file ).
2. The dir MsgPack.rkt uses invalid chars (assume the '.') so change it to
say "msgpack/"
3. See 1.7.3 Linking and Developing New Packages in Racket Docs.
 Run '$ raco pkg install'  which will make your collection visible,
including for tests.
 e.g.
 % raco test test/pack/binary.rkt
 raco test: (submod "test/pack/binary.rkt" test)
OK, passed 100 tests.
OK, passed 100 tests.
2 tests passed
4.  With regard to the ext #:transparent and the
  unpack: broke its own contract
  any-wrap/c: Unable to protect opaque value passed as `Any`
 value: #
 I have hit this landmine before but as I recall usually on a
(require/typed [#:opaque ...]) situation.
 Try googling the error.  I recall some suggestion on a
define-refined-subtype of the opaque structure or some such to allow the
contract to wrap.   I'll definitely look at it tonight if someone else
doesn't offer up a solution.



On Sun, Aug 6, 2017 at 12:35 PM,  wrote:

> *bump* I hope bumping is not frowned upon here. I just need to sort this
> one last issue 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.
>

-- 
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: Need help porting a library to Typed Racket

2017-08-06 Thread hiphish
*bump* I hope bumping is not frowned upon here. I just need to sort this one 
last issue 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.