Calling J from Python

H

Hendrik van Rooyen

That is a start.

"Hell" is also what most example start with as in "Hello something"
Hell in northern countries is very cold.
Hell in middle east is very hot.
I do not know which is your Hell hot or cold.
Hell o veröld
It's also a village in Norway: http://en.wikipedia.org/wiki/Hell,_Norway
:D

I am under the impression that Loki had a daughter called Hel ...

- Hendrik
 
E

Eric_Dexter

I may have mistook the source code licence for the use licence.. I
will look into a little further to see what it can do.. Looks like
you are not allowed to redistribute k for profit. Some day I will
look up letters a random in the search engine to see what I come up
with.
 
G

Gosi

I may have mistook the source code licence for the use licence.. I
will look into a little further to see what it can do.. Looks like
you are not allowed to redistribute k for profit. Some day I will
look up letters a random in the search engine to see what I come up
with.

You can get older versions of the source code too for free.
The utility sources are also free.
 
T

Tina I

Hendrik said:
I am under the impression that Loki had a daughter called Hel ...

- Hendrik
Yes. And Hel was the queen of the underworld which was also called 'Hel'
(Which of course is 'hell', in modern Norwegian : "helvete")
 
D

Dennis Lee Bieber

I am under the impression that Loki had a daughter called Hel ...
One of his few "normal" offspring... After all, Loki also was
Sleipnir's* /mother/! And probably related to Fenrir (aka, the Fenris
Wolf) as well.


* Odin's eight-legged horse
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
A

Ant

Yes, but with Python you wouldn't have to spend a
couple of weeks sitting and thinking before starting
to type that line...

This is a good point often overlooked. You often get these threads on
c.l.python about "How can I do this in one line", usually with some
example of how it is done in only 13 characters in Perl. Yes you may
spend less time typing - but unless you are a true expert in (J, Perl,
other terse language) the time you spend actually working out how to
type it, and in debugging it far outweighs the time you'd spend on all
of that typing in a clean but more verbose language such as Python.
 
A

Alexander Schmolck

[restoring context]

(it's actually reasonably straightforward, if someone really cares I might
post a translation)
This is a good point often overlooked.

There is of course some truth in Greg's statement -- J code will likely have a
higher thought/character ratio (even after adjusting for differences in
average token-length) -- but I don't think that is in itself terribly
interesting.

What is in my opinion of interest is how much of the additional thought you
need to put in in order to achieve higher terseness is spent

a) on more pentrating insight on the problem as such, encouraged by the
mind-set and abstractions associated with a language (let's call this
expressiveness)

and how much of it is spent

b) on perl-golf style geek masturbation that focuses on recalling and
exploiting the language's various behavioral oddities that are only of
coincidental or low-level relevance to the problem (let's call this
golf-syndrome)

I'm not claiming that the boundaries between the two are always unambigious,
but I think the distinction is pretty important when discussing pros and cons
of differences in terseness between languages.

Apart from scaling better, one reason that a), expressiveness, is much more
interesting than b), golf-syndrome, is that it translates to some extent even
to writing code in other languages as it enriches the programmer's reservoir
of metaphors and abstractions. Typically this also has a beneficial effect
for coding even in languages that offer no direct support for these
abstractions (I bet a programmer with, say extensive python, J and prolog
experience and just a little bit of C background is in many cases likely to
come up with a superior C solutions to challenging problems than someone who's
got the same amount of experience in C only).

Therefore...
You often get these threads on c.l.python about "How can I do this in one
line", usually with some example of how it is done in only 13 characters in
Perl. Yes you may spend less time typing - but unless you are a true expert
in (J, Perl, other terse language) the time you spend actually working out
how to type it, and in debugging it far outweighs the time you'd spend on
all of that typing in a clean but more verbose language such as Python.

.... I also don't think your pairing of J and Perl is particularly helpful. As
long as no one can point me to some resonable examples demonstrating otherwise
I flattly deny that Perl is more concise than python in an *interesting* way,
i.e. by encouraging or enabling a)

(BTW "not interesting" != "not practically relevant"; harking back to my
previous posts, typing effort *does matter* in some contexts, such as
command-line one-liners; IMO the only thing perl is useful for.)

J, on the other hand, whilst also suffering somewhat from golf-syndrome, does
in my opinion also enable a)-style terseness. Let me give an example:

Before reading further, how would you code a program that gives the following
output ('skewed' sierpinski-triangle) in python? I'll give some remarkably
concise and IMO lucid J code and a python translation below.


*
**
* *
****
* *
** **
* * * *
********
* *
** **
* * * *
**** ****
* * * *
** ** ** **
* * * * * * * *
****************








SPOILERS AHEAD

































J solution
----------

I can think of two nice ways in J, 13 and 16 characters long respectively and
each expressing something essential and non-trival about the problem in a way
that would be more cumbersome in python.

Here's the first one:

(,,.~)^:4,'*' NB. due to Cliff Reiter, slightly adapted


Possible Python translation
---------------------------

Here's a python transiteration attempt:

# ^: , ~ ,.
print rep(hook(vertcat, self(horzcat)),4)('*')

or slightly more idiomatic:

def sierpinski(x):
return vertcat(x,horzcat(x,x))

print rep(sierpinsky,4)('*')

With:

def identity(x): return x
def rep(f,n): # f^:n
if n < 0: return lambda *args: rep(inverse(f),-n)(*args)
elif n == 0: return identity
else: return lambda *args: rep(f,n-1)(f(*args))
# horzcat and vertcat are only string-based special purpose mockups for this
# problem since python doesn't have arrays
def horzcat(a,b): # a,.b
return "\n".join(a_i+b_i for (a_i,b_i) in zip(a.split('\n'),
b.split('\n')))
def vertcat(a,b): # a,b
# fill "rows" of b up with spaces if a's rows are longer and vice versa
dif = len(a.split('\n')[0]) - len(b.split('\n')[0])
if dif < 0:
a = a.replace('\n', ' '*-dif + '\n') + ' '*-dif
elif dif > 0:
b = b.replace('\n', ' '*dif + '\n') + ' '*dif
return a + '\n' + b
def self(f): # f~
return lambda x: f(x,x)
def hook(f,g): # (f g)
return lambda x: f(x,g(x))
print rep(hook(vertcat, self(horzcat)),4)('*')

I find above J solution is quite neat conceptually because it directly
captures the self-similarity.

The other J solution is

' *'{~2|!~/~i.*:2^4

Can you figure out how it works? (Hint: k!n = choose(k,n), 2|x = x%2)

'as
 
M

Martin =?iso-8859-1?Q?L=FCthi?=

Alexander

Alexander Schmolck said:
I can think of two nice ways in J, 13 and 16 characters long respectively and
each expressing something essential and non-trival about the problem in a way
that would be more cumbersome in python.

Here's the first one:

(,,.~)^:4,'*' NB. due to Cliff Reiter, slightly adapted

Well, not as interesting as your solution, but definitively less mind-boggling
and much more flexible/extendible

====================================================
l = [True]
pr = {True: '*', False: ' '}

for k in range(15):
print ''.join([pr[x] for x in l])
l = [True] + [l[i+1]^l for i in range(len(l)-1)] + [True]
====================================================

more elegant solutions sought!

tnoo
 
B

bearophileHUGS

ant:
and in debugging it far outweighs the time you'd spend on all
of that typing in a clean but more verbose language such as Python.

Typing time counts a bit too. A language like Java is even more
verbose than Python, and that probably slows down the actual
programming, compared to less verbose languages. I think there is some
verbosity sweet spot between J and Java, and for me that's called
Python ;-)


Martin Lüthi:
more elegant solutions sought!

This is a bit simpler, but probably there are simpler solutions using
modular arithmetic:

l = [1]
for _ in range(15):
print ''.join(" *"[x] for x in l)
l = [1] + [l[i+1]^l for i in range(len(l)-1)] + [1]


Bye,
bearophile
 
G

George Sakkis

ant:
and in debugging it far outweighs the time you'd spend on all
of that typing in a clean but more verbose language such as Python.

Typing time counts a bit too. A language like Java is even more
verbose than Python, and that probably slows down the actual
programming, compared to less verbose languages. I think there is some
verbosity sweet spot between J and Java, and for me that's called
Python ;-)

Martin Lüthi:
more elegant solutions sought!

This is a bit simpler, but probably there are simpler solutions using
modular arithmetic:

l = [1]
for _ in range(15):
print ''.join(" *"[x] for x in l)
l = [1] + [l[i+1]^l for i in range(len(l)-1)] + [1]

Bye,
bearophile


Here's another one, adapted from the example (in Java) in Wikipedia's
entry (http://en.wikipedia.org/wiki/Sierpinski_triangle):

N=15
for x in xrange(N,0,-1):
print ''.join('* '[x&y!=0] for y in xrange(N+1-x))


George
 
H

Hendrik van Rooyen

Dennis Lee Bieber said:
One of his few "normal" offspring... After all, Loki also was
Sleipnir's* /mother/! And probably related to Fenrir (aka, the Fenris
Wolf) as well.


* Odin's eight-legged horse

We should both be more careful - using the past tense like that is
disrespectful, and there are no guarantees that the Norse gods are dead.

Vindictive lot, they were.. ....uuhhh are, I mean.

- Hendrik
 
G

greg

Alexander said:
how would you code a program that gives the following
output ('skewed' sierpinski-triangle) in python?


*
**
* *
****
* *
** **
* * * *
********
* *
** **
* * * *
**** ****
* * * *
** ** ** **
* * * * * * * *
****************

###############################################################

def print_sierpinski(order):
size = 4 * 2 ** order
buffer = [size * [" "] for i in range(size)]
put_sierpinski(buffer, size, 0, 0)
for line in buffer:
print "".join(line)

def put_sierpinski(buffer, size, x, y):
if size == 4:
put_triangle(buffer, size, x, y)
else:
size2 = size / 2
put_sierpinski(buffer, size2, x, y)
put_sierpinski(buffer, size2, x, y + size2)
put_sierpinski(buffer, size2, x + size2, y + size2)

def put_triangle(buffer, size, x, y):
for i in range(3):
buffer[y + i][x] = buffer[y + i][x + i] = "*"
for i in range(4):
buffer[y + 3][x + i] = "*"

print_sierpinski(2)

###############################################################

By making the recursion explicit, I think this brings out the
self-similarity better than any of the other solutions posted,
which are very clever but not what I would call "lucid".
 
N

Nick Craig-Wood

George Sakkis said:
This is a bit simpler, but probably there are simpler solutions using
modular arithmetic:

l = [1]
for _ in range(15):
print ''.join(" *"[x] for x in l)
l = [1] + [l[i+1]^l for i in range(len(l)-1)] + [1]

Bye,
bearophile


Here's another one, adapted from the example (in Java) in Wikipedia's
entry (http://en.wikipedia.org/wiki/Sierpinski_triangle):

N=15
for x in xrange(N,0,-1):
print ''.join('* '[x&y!=0] for y in xrange(N+1-x))


This is my solution after a few minutes thought.

It uses a different algorithm for generating the triangle. If python
could output binary numbers it would be more elegant...
.... print ("%X" % n).replace('0', ' ').replace('1', '*')
.... n = n ^ (n << 4)
....
*
**
* *
****
* *
** **
* * * *
********
* *
** **
* * * *
**** ****
* * * *
** ** ** **
* * * * * * * *
****************
 
G

gosinn

Den lördagen den 9:e mars 2013 kl. 19:34:09 UTC skrev Mark Lawrence:
Got a right slagging six years ago when first posted, what's changed to

make it worth evaluating?



--

Cheers.



Mark Lawrence

A lot has changed these last six years.

By the way Python 2.5 and Python 3.3 will not work with the example.

Python 2.7 does not work in Vista unchanged (neither does Python 3.3) but after change will run the demo and some examples.

J is now open source and has more platforms and guis.
 
G

gosinn

Den lördagen den 9:e mars 2013 kl. 19:34:09 UTC skrev Mark Lawrence:
Got a right slagging six years ago when first posted, what's changed to

make it worth evaluating?



--

Cheers.



Mark Lawrence

A lot has changed these last six years.

By the way Python 2.5 and Python 3.3 will not work with the example.

Python 2.7 does not work in Vista unchanged (neither does Python 3.3) but after change will run the demo and some examples.

J is now open source and has more platforms and guis.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top