The Artima Developer Community
Sponsored Link

Python Buzz Forum
Sugar Pie

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Thomas Guest

Posts: 236
Nickname: tag
Registered: Nov, 2004

Thomas Guest likes dynamic languages.
Sugar Pie Posted: Mar 6, 2008 1:03 PM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Thomas Guest.
Original Post: Sugar Pie
Feed Title: Word Aligned: Category Python
Feed URL: http://feeds.feedburner.com/WordAlignedCategoryPython
Feed Description: Dynamic languages in general. Python in particular. The adventures of a space sensitive programmer.
Latest Python Buzz Posts
Latest Python Buzz Posts by Thomas Guest
Latest Posts From Word Aligned: Category Python

Advertisement

Question: in the code snippet bleow, what does the result stream, rs, approximate?

from itertools import count, ifilter, islice, izip
from random import random as xy
    
pt = lambda: (xy(), xy())
on = ifilter(lambda n: abs(complex(*pt())) < 1., count(1))
rs = (4. * j / i for i, j in izip(on, count(1)))

The code isn’t wilfully obscure but I’ll admit it’s unusual. Although written in a functional style, the source of the stream, pt, is utterly impure, generating a sequence of random results: it sprinkles points in a unit square. Despite this random input the results stream always tends to the same value. Well, in theory it should!

Incidentally, applying abs() to a complex number returns the modulus of that number: it’s easier than math.sqrt(x * x + y * y). And I’m counting from 1, not 0, to avoid dividing by zero.

>>> abs(3+4j)
5.0
>>> pt = 5, 12
>>> abs(complex(*pt))
13.0

Here’s a picture of a round pie on a square baking tray being dusted with sugar.

Dusting a pie

Read: Sugar Pie

Topic: Software needs Metaphors too! Previous Topic   Next Topic Topic: Log analysis of my website

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use