Calendar - for "REAL" programmers: Difference between revisions

J: remove "sneaky version" (wasn't 132 columns) and fix up text
(J: bugfix in "real" version -- and task requires format for 132 column display)
(J: remove "sneaky version" (wasn't 132 columns) and fix up text)
Line 318:
 
=={{header|J}}==
After some sneaky code with foreigns to obtain the upper and lowercase alphabets (because a. cannot be used), the code is straightforward. Write names in uppercase, convert to lowercase, and then use ~ to make the strings into verbs. Code taken from the calendar page.
 
'''Solution:'''
<lang j> 'UPPER LOWER'=: 3 (3!:4)"1 ]65 97 +/ <:+/\26$1 NB. UPPER AND LOWERCASE ALPHABETS
TL=:(LOWER {~ [: {.@I. 1,~UPPER&=) :: ]"0 NB. TO LOWERCASE
TU=:(UPPER {~ [: {.@I. 1,~LOWER&=) :: ]"0 NB. TO UPPERCASE
(TL 'REQUIRE')~ TL'DATES FORMAT' NB. J6
(TL 'REQUIRE')~ TL'DATES GENERAL/MISC/FORMAT' NB. J7
CALBODY=: (1 1 }. _1 _1 }. ":)@(-@(<.@%&22)@[ ]\ ((TL'CALENDAR')~)@])
CALTITLE=: (<: - 22&|)@[ ((TL'CENTER')~) '[INSERT SNOOPY HERE]' , '' ,:~ ":@]
FORMATCALENDAR=: [: TU CALTITLE,CALBODY</lang>
'''Example use''' (the first line <code>80 FORMATCALENDAR 1969</code> being the command that built this particular calendar):
<lang j> 80 FORMATCALENDAR 1969
[INSERT SNOOPY HERE]
1969
JAN │ FEB │ MAR
SU MO TU WE TH FR SA│ SU MO TU WE TH FR SA│ SU MO TU WE TH FR SA
1 2 3 4│ 1│ 1
5 6 7 8 9 10 11│ 2 3 4 5 6 7 8│ 2 3 4 5 6 7 8
12 13 14 15 16 17 18│ 9 10 11 12 13 14 15│ 9 10 11 12 13 14 15
19 20 21 22 23 24 25│ 16 17 18 19 20 21 22│ 16 17 18 19 20 21 22
26 27 28 29 30 31 │ 23 24 25 26 27 28 │ 23 24 25 26 27 28 29
│ │ 30 31
─────────────────────┼─────────────────────┼─────────────────────
APR │ MAY │ JUN
SU MO TU WE TH FR SA│ SU MO TU WE TH FR SA│ SU MO TU WE TH FR SA
1 2 3 4 5│ 1 2 3│ 1 2 3 4 5 6 7
6 7 8 9 10 11 12│ 4 5 6 7 8 9 10│ 8 9 10 11 12 13 14
13 14 15 16 17 18 19│ 11 12 13 14 15 16 17│ 15 16 17 18 19 20 21
20 21 22 23 24 25 26│ 18 19 20 21 22 23 24│ 22 23 24 25 26 27 28
27 28 29 30 │ 25 26 27 28 29 30 31│ 29 30
│ │
─────────────────────┼─────────────────────┼─────────────────────
JUL │ AUG │ SEP
SU MO TU WE TH FR SA│ SU MO TU WE TH FR SA│ SU MO TU WE TH FR SA
1 2 3 4 5│ 1 2│ 1 2 3 4 5 6
6 7 8 9 10 11 12│ 3 4 5 6 7 8 9│ 7 8 9 10 11 12 13
13 14 15 16 17 18 19│ 10 11 12 13 14 15 16│ 14 15 16 17 18 19 20
20 21 22 23 24 25 26│ 17 18 19 20 21 22 23│ 21 22 23 24 25 26 27
27 28 29 30 31 │ 24 25 26 27 28 29 30│ 28 29 30
│ 31 │
─────────────────────┼─────────────────────┼─────────────────────
OCT │ NOV │ DEC
SU MO TU WE TH FR SA│ SU MO TU WE TH FR SA│ SU MO TU WE TH FR SA
1 2 3 4│ 1│ 1 2 3 4 5 6
5 6 7 8 9 10 11│ 2 3 4 5 6 7 8│ 7 8 9 10 11 12 13
12 13 14 15 16 17 18│ 9 10 11 12 13 14 15│ 14 15 16 17 18 19 20
19 20 21 22 23 24 25│ 16 17 18 19 20 21 22│ 21 22 23 24 25 26 27
26 27 28 29 30 31 │ 23 24 25 26 27 28 29│ 28 29 30 31
│ 30 │ </lang>
 
Alternatively, we could express the entire algorithm using only uppercase letters.
 
For added "real"ness, we also only use 1 character names:
 
Line 385 ⟶ 331:
F=: 0 _1 }. 0 1 }. (2+[) E '[INSERT SNOOPY HERE]', ":@], D</lang>
 
Here's an example calendar (the first line <code>80132 F 1969</code> being the command that build this calendar):
 
<lang> 132 F 1969
6,951

edits