Contract bridge diagrams

59 views
Skip to first unread message

John Dell'Aquila

unread,
May 6, 2025, 1:00:44 PM5/6/25
to forum
I decided to learn about verb trains by implementing a modestly complex task as a single tacit verb. The exercise I chose was to convert a hand of cards into a bridge diagram, where each suit symbol is followed by a descending list of the ranks in that suit.

After a *lot* of reading and experimentation I settled on the below code and would like to know what experienced eyes make of it? Is there a simpler way to do this that I've missed? Is the code readable cold or would it be better done explicitly?

Thanks, John

NB. suits, ranks, cards (lowest to highest)
S=: <"0 @ u: 16b2660 + i._4  NB. |♣|♢|♡|♠|
R=: <;._1 ' 2 3 4 5 6 7 8 9 10 J Q K A'
C=: , |: R,&.>/S  NB. |2♣|3♣| ... |K♠|A♠|

NB. convert card numbers (0-51) to boxed strings
suits=: S {~ <. @ %&13
ranks=: R {~ 13&|
cards=: {&C

NB. bridge diagram (each suit followed by its descending ranks)
bridge=: (|.S)&((>@[) ;"0 1 (=/ suits) #"1 _ (] ranks))@([: \:~ ])"1

cards ]h=: 4 13 $ ?~52  NB. show hands as dealt
bridge h  NB. show hands as bridge diagram

Joseph Novakovich

unread,
May 6, 2025, 7:08:03 PM5/6/25
to fo...@jsoftware.com
Nice! There are some redundancies in the bridge verb; the following should be equivalent:

  bridge=: (|.S)&(>@[ ;"0 1 (=/ suits) # (] ranks))@\:~"1

Might just be personal preference, but instead of boxing all the cards separately and having blank boxes, you could instead display the tables as something like this:

  ┌─┬──────┐
  │♠│AQ865 │
  ├─┼──────┤
  │♡│KJ7   │
  ├─┼──────┤
  │♢│J9    │
  ├─┼──────┤
  │♣│AK6   │
  └─┴──────┘

All that needs to be changed is leaving S and R unboxed and also redefining C as:

  S=: u: 16b2660 + i._4  NB. '♣♢♡♠'
  R=: '23456789TJQKA'    NB. portable bridge notation has T for 10
  C=: , S <@,"0/ R       NB. |2♣|3♣| ... |K♠|A♠|

If instead you want to keep the boxing in the diagrams as you have it, you can avoid transpose by swapping S and R in C:

  C=: , S (,&.>)/ R

Joseph

To unsubscribe from this group and stop receiving emails from it, send an email to forum+un...@jsoftware.com.

John Dell'Aquila

unread,
May 7, 2025, 12:30:12 PM5/7/25
to forum, Joseph Novakovich
Thank you! I was experimenting with capped forks at some point and became blinded to the redundancy in the bridge verb. I chose boxed ranks because I wasn't aware of the T convention, which looks strange to my eyes, but makes the output so much more compact that it must be embraced.

I feel like I'm finally starting to climb the learning cliff. Now I must gush about J to everyone I meet. Well, just the nerds, it wouldn't exactly be cocktail conversation.

John

Kenneth Lettow

unread,
May 7, 2025, 12:42:19 PM5/7/25
to fo...@jsoftware.com

Saving a couple of characters for C.

C=: ,{S;R


(,{S;R) -: (, S (,&.>)/ R)

1


--
Kenneth Lettow
Director, Market Intelligence at Xometry
Where Big Ideas Are Built
Email kle...@thomasnet.com ­Website http://www.xometry.com/ ­

facebook

linkedin

twitter

instagram

youtube

 
__tpx__

John Dell'Aquila

unread,
May 7, 2025, 1:05:22 PM5/7/25
to forum, Kenneth Lettow
Thanks, I didn't know about catalogue. I actually wanted the rank before the suit so I'd throw in a transpose.
C=: ,|:{R;S

John

Henry Rich

unread,
May 7, 2025, 1:23:25 PM5/7/25
to fo...@jsoftware.com
I personally hate the T convention & use 10 with no problems.

Henry Rich

John Dell'Aquila

unread,
May 7, 2025, 4:34:17 PM5/7/25
to forum, Henry Rich
I'm with you aesthetically, but willing to compromise, because things are so much simpler when every rank is a single character.
I have to pick my battles until I learn enough J to do as I please.

John

Henry Rich

unread,
May 7, 2025, 4:37:24 PM5/7/25
to John Dell'Aquila, forum

It's easy. Just apply ;"1 to the boxed diagrams you had before.

Henry Rich

John Dell'Aquila

unread,
May 8, 2025, 12:01:43 PM5/8/25
to forum, Henry Rich, forum, John Dell'Aquila
I thought it would be something involved. I should have known J would have a verb for that!
bridge=: (|.S)&([:;"1 [;"0 1 (=/ suits) # (] ranks))@\:~"1

I like the string output that produces
♠Q1085
♡A7    
♢J10432
♣86    

Thanks,
John
Reply all
Reply to author
Forward
0 new messages