Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python.': 0.04; 'operator': 0.04; 'flows': 0.07; 'indeed,': 0.07; '[0]': 0.09; 'compute': 0.09; 'necessary,': 0.09; 'readable': 0.09; 'restored': 0.09; 'value:': 0.09; 'library': 0.13; 'case.': 0.15; 'result,': 0.15; 'concat': 0.16; 'course:': 0.16; 'enough?': 0.16; 'remembered': 0.16; 'sequence.': 0.16; 'subject:programming': 0.16; 'examples': 0.16; 'wrote:': 0.18; 'example.': 0.18; 'programming': 0.20; 'have,': 0.21; 'trying': 0.21; 'received:209.85.210.174': 0.21; 'received:mail- iy0-f174.google.com': 0.21; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; '+0100,': 0.23; 'indentation': 0.23; 'stops': 0.23; '(my': 0.24; "python's": 0.24; 'code': 0.26; 'function': 0.27; 'skip:[ 10': 0.27; 'lists': 0.28; 'bit': 0.28; 'anyway.': 0.29; 'example': 0.29; 'handling': 0.30; 'oop': 0.30; 'recursion': 0.30; "didn't": 0.30; 'equivalent': 0.31; "i've": 0.32; 'thanks': 0.32; 'list': 0.32; 'sort': 0.33; 'there': 0.33; 'header:User-Agent:1': 0.33; 'named': 0.33; 'fri,': 0.34; 'rule': 0.34; 'anything': 0.34; 'frustrated': 0.34; 'to:addr:python-list': 0.35; "we're": 0.36; 'equal': 0.36; 'question': 0.36; 'two': 0.36; 'push': 0.37; 'but': 0.37; 'charset:us-ascii': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'replace': 0.38; 'resulting': 0.38; 'some': 0.38; 'doing': 0.38; 'should': 0.38; 'easier': 0.38; 'mailing': 0.39; 'ok,': 0.39; 'received:209': 0.39; 'client': 0.40; 'to:addr:python.org': 0.40; 'views': 0.40; 'back': 0.60; 'more': 0.61; 'achieve': 0.61; 'our': 0.63; 'believe': 0.65; 'pleased': 0.65; 'android': 0.66; 'piece': 0.66; 'share': 0.66; 'special': 0.66; 'restore': 0.68; 'become': 0.69; 'realized': 0.73; 'average': 0.80; 'pleasure': 0.80; '[1,2]': 0.84; 'blade': 0.84; 'chain:': 0.84; 'pain': 0.84; 'readability': 0.84; 'streams': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=gUggDUFovHWbxo1FWZGU69If1YxTYt6XaoPEP1JQ+WM=; b=N1N+dC5Xr14FzkWHulRC/j6L4EVQ1qyZ/ACJpuartRD5nTKA89lwjf1OL4AVoXqUE8 WV02DZHJZXa+XNbJrmxhmnvw+v2nQgf3w/dc2h0UQLzq/VTtrgRfpj0MHyrPVeNydnLv MqWg4qrfNuN5TKICgdniQnxk9sCpbZvob9ts9YlI4Yh8WaiIxX/eVyow83LL3y48WnwK qKT0jQ1hIYalNMIHvzXgKqJYuDPP9TJJ3tT3VBsOgeCSIJNt5JG/6UQTnOr+qgn5ESg1 0m51FvIikVcPgtpQ+CLoEjZGPNuVon6f2J4XKCZ80yvQf3f0Gasq9cW+PT8MWaKE/aaC lNmQ== Date: Sat, 24 Mar 2012 07:32:38 +0800 From: Ray Song To: python-list@python.org Subject: Re: Stream programming References: <4f6c9e17$0$1383$4fafbaef@reader2.news.tin.it> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4f6c9e17$0$1383$4fafbaef@reader2.news.tin.it> User-Agent: Mutt/1.5.21 (2010-09-15) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 86 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1332545570 news.xs4all.nl 6958 [2001:888:2000:d::a6]:47939 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:22103 On Fri, Mar 23, 2012 at 05:00:23PM +0100, Kiuhnm wrote: > I've been writing a little library for handling streams as an excuse for > doing a little OOP with Python. > > I don't share some of the views on readability expressed on this ng. > Indeed, I believe that a piece of code may very well start as complete > gibberish and become a pleasure to read after some additional > information is provided. > > I must say that imposed indentation is a pain when one is trying to > define some sort of DSL (domain specific language). Also, Python's > operator overloading is a bit limited, but that makes for a more > rewarding experience in my case. > > Here's an example of what you can write: > > numbers - push - avrg - 'med' - pop - filter(lt('med'), ge('med'))\ > - ['same', 'same'] - streams(cat) - 'same' > > Ok, we're at the "complete gibberish" phase. > > Time to give you the "additional information". > > I will use "<=>" to mean "is equivalent to". That's not part of the DSL. > A flow has one or more streams: > 1 stream: > [1,2,3] > 2 streams: > [1,3,5] | [2,4,6] > Two flows can be concatenated: > [1,2,3] + [4,5,6] <=> [1,2,3,4,5,6] > [0] + ([1,2] | [3,4]) + [10] <=> [0,1,2,10] | [0,3,4,10] > ([1,2] | [10,20]) + ([3,4] | [30,40]) <=> [1,2,3,4] | [10,20,30,40] > A flow can be transformed: > [1,2] - f <=> [f(1),f(2)] > ([1,2] | [3,4]) - f <=> [f(1,3),f(2,4)] > ([1,2] | [3,4]) - [f] <=> [f(1),f(2)] | [f(3),f(4)] > ([1,2] | [3,4]) - [f,g] <=> [f(1),f(2)] | [g(3),g(4)] > [1,2] - [f,g] <=> [f(1),f(2)] | [g(1),g(2)] > Some functions are special and almost any function can be made special: > [1,2,3,4,5] - filter(isprime) <=> [2,3,5] > [[],(1,2),[3,4,5]] - flatten <=> [1,2,3,4,5] > Note that 'filter' is not really necessary, thanks to 'flatten'. > Flows can be named, remembered and used > as a value: > [1,2,3,4,5] - 'flow' + val('flow') <=> [1,2,3,4,5]*2 > as a transformation chain: > [1,2,3] - skipfirst - 'again' | [4,5,6] - func('again') > <=> [2,3] | [5,6] > Recursion is also possible and stops when a function is applied to an > empty sequence. > Flows can be saved (push) and restored (pop) : > [1,2,3,4] - push - by(2) - 'double' - pop | val('double') > <=> [1,2,3,4] | [2,4,6,8] > There are easier ways to achieve the same result, of course: > [1,2,3,4] - [id, by(2)] > > Let's go back to our example. I didn't tell you anything but you should > be able to understand it anyway. > > numbers - push - avrg - 'med' - pop - filter(lt('med'), ge('med'))\ > - ['same', 'same'] - streams(cat) - 'same' > > It reads as > > "take a list of numbers - save it - compute the average and named it > 'med' - restore the flow - create two streams which have, respect., the > numbers less than 'med' and those greater or equal to 'med' - do the > /entire/ 'same' process on each one of the two streams - concat the > resulting streams - name all this /entire/ process 'same'. > Not readable enough? Replace 'same' with 'qsort'. > > Is that readable or am I going crazy? [note: that's a rhetorical > question whose answer is "That's very readable!"] > This sounds like a concatenative programming and i've found many cool examples from Haskell and Ruby. I was pleased with Python's rigid off-side rule at first but frustrated when I realized that this is a two-edged blade as the rule makes a DSL difficult. Sorry for former mail to you (my damn Android gmail client doesn't understand mailing lists well). -- Ray