User:B Jonas

From J Wiki
Jump to navigation Jump to search

For quick finding: Phrases/Sets.

Some useful snippets I always need. Firstly the combinations verb from the factorial dictionary page changed slightly, secondly a simple variations verb

   comb =: 4 :'; (i.@# ,.&.> ,&.>/\.@:(>:&.>))^:x i.@(,&0)&.> 0=i.<:x-y'
   2 comb 5
0 1
0 2
0 3
0 4
1 2
1 3
1 4
2 3
2 4
3 4
   var =: [{."1 i.@]A.~!@-~*[:i.!@]%!@-~
   2 var 4
0 1
0 2
0 3
1 0
1 2
1 3
2 0
2 1
2 3
3 0
3 1
3 2

Then cross product of two 3d real vectors.

   cross=: [: : ([: -/ .*"2 (=i.3),"1 _ ,:)"1

Patchy pattern script golfed down to one J line

   ' ,'';'{~_2#.&.|:\([:-.3<:[:+/_1 0 1|."1"{[:+/_1 0 1|."{])^:40]0=?3$~50 80

Try it.


Equivalent shorter expression. -- Roger Hui <<DateTime(2007-10-13T22:01:25Z)>>

   ' ,'';'{~_2#.@|:\(3>([:+/_1 0 1|."{|:)^:2)^:40]0=?50 80$3

Ascii-art mandelbrot fractal, usable in the irc bot.

   ] agr =: [:{&' .'':' _2:#.&.|:\ 0&=
   ] mandeltop=: agr 2<| (+*:)^:10~ (0.1*i._10)j.~/(0.5-0.1*i.25)
   ] mandelbot=: agr 2<| (+*:)^:10~ (0.1*>:i.10)j.~/(0.5-0.1*i.25)
   ] mandeltop
   ] mandelbot

Another cellular automaton. Runs until interrupted. Set sleepamt to the number of seconds to wait between lines.

   sleep=: 'libc.so.6 select > i i x x x *x' (15!:0) 0;0;0;0; [:<._ 1e6#:*&1e6
   sleepamt=: %40
   0[ (([:sleep]&sleepamt)] [:1!:2&2&.({&' #') 1 3 5 6 e.~ 3 #.\ 0 0&,)^:_]79$1

Some approximations for pi.

The first one uses a slowly converging series and is not scalable for higher precision.

   4%:90*+/_4^~>:i.99

The second one is more scalable, but still not a very efficient algorithm, and also trades some clarity for efficiency. I show two variants.

   2*(+[:(%~-.)/[:-/_2]\*/\@:%&(>:i.22))^:6]1


   3*(+[:(%~0.5&-)/[:-/_2]\*/\@:%&(>:i.18))^:5]1

The third one is quite efficient.

   (($&0 12 0 _12%i.)28)p.2-%:3


   12*-/(2-%:3)(^%])1+2*i.14

Ways to generate a list of all alphanumeric ascii characters.

   a.#~2|i.&0"1 a./:@,"0 _'/9@Z`z'
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
   a.#~~:/"1(i.256)</a.i.'0:A[a{'
   a.{~I.2|I.48 10 7 26 6 26
   u:I._2<nc'a'&,&.>a.              NB. by [[User:Raul Miller|Raul Miller]]
   16}.u:,32 65 97+/i.26            NB. by [[User:Raul Miller|Raul Miller]]
   AlphaNum_j_                      NB. by [[User:Ric Sherlock|Ric Sherlock]]
   a.#~2|'0:A[a{'i.&6@/:@,"#:a.
   a.#~2|a.i.&0@/:@,"{'/9@Z`z'
   a.#~~:/\a.e.'0:A[a{'
   a.#~2|i.&0"1 a./:@,"0 _'/9@Z`z'  NB. Linda Alvord
   a.#~2|'0:A[a{'i.&6@/:@,"#:a.     NB. Linda Alvord
   42}.,4{._32(26&{.)\a.-.':'       NB. me using Linda Alvord's idea
   a.#~2|'/9@Z`z'I.a.

freenode/#jsoftware 2013-05-30
172207 < mike_____> given a string w, check if it's of the form w = xuuy for
                    some strings x,u, and y
...
172742 < b_jonas> ) ]t=: 'ABCDE'{~ 5|+/\1+?70$4
172742 < jconn> b_jonas:
EADAEBDACBEDECDACAEDBDBCABDABEDBEAEDCEBABCABCECADADADBACDCECECEABDADBC
...
174144 < b_jonas> ) l #~ l <: t ([: >./ [: (*>:)/\. =)"1 (l=.>:i.<.-:#t)|.!.'
                  '"0 _ t
174145 < jconn> b_jonas: 2 3
174207 < b_jonas> mike: ^^ shows there are repeated substrings of length 2 and 3
174209 < b_jonas> I hope
...
174520 < b_jonas> of course, you could just try a solution by taknig substrings
...
174801 < b_jonas> ) l #~ (l=.>:i.<.-:#t) (4 :'1 e. x (}.=-@[}.]) x <\ y'"0 _) t
174802 < jconn> b_jonas: 2 3
174821 < b_jonas> mike_____: ^ comparing substrings, but that would take O(n^3)
                  time of course

It annoys me that j601 has the profile changed in such a way, that under linux jconsole, the box drawing characters are set up to something that (I assume) work on utf8 terminals, but mess up my 8859-2 terminal. In case anyone else is frustrated by this too, here's the way I reverted it to the previous settings, that is, the ascii box drawing characters. Depending on your interpreter version, in the file j601/system/extras/util/boot.ijs, find this statement: boxdraw_j_ 1 2 0 {~ ('Darwin';'Win') i. <UNAME and comment it out with NB.; or in the file j701/system/util/configure.ijs, find this statement: 9!:7 BoxForm { Boxes and comment it out with an NB..