<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>s-expressions &#187; lambda</title>
	<atom:link href="http://s-expressions.com/tag/lambda/feed/" rel="self" type="application/rss+xml" />
	<link>http://s-expressions.com</link>
	<description>Amit Rathore blogs about software development</description>
	<lastBuildDate>Tue, 17 Jan 2012 17:58:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='s-expressions.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>s-expressions &#187; lambda</title>
		<link>http://s-expressions.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://s-expressions.com/osd.xml" title="s-expressions" />
	<atom:link rel='hub' href='http://s-expressions.com/?pushpress=hub'/>
		<item>
		<title>Clojure: understanding dynamic vars and laziness</title>
		<link>http://s-expressions.com/2009/02/27/clojure-understanding-dynamic-vars-and-laziness/</link>
		<comments>http://s-expressions.com/2009/02/27/clojure-understanding-dynamic-vars-and-laziness/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 20:04:43 +0000</pubDate>
		<dc:creator>Amit Rathore</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[learning]]></category>

		<guid isPermaLink="false">http://sexp.wordpress.com/?p=140</guid>
		<description><![CDATA[I spent a frustrating day figuring out why something wasn&#8217;t working as expected in a particular situation, even thought it was working just fine elsewhere. It turned out the problem was my flawed understanding of how Clojure&#8217;s dynamic vars behaved with respect to laziness. Thanks to Chouser and Chousuke on the #clojure channel in IRC, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=s-expressions.com&amp;blog=4254185&amp;post=140&amp;subd=sexp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I spent a frustrating day figuring out why something wasn&#8217;t working as expected in a particular situation, even thought it was working just fine elsewhere. It turned out the problem was my flawed understanding of how Clojure&#8217;s dynamic vars behaved with respect to laziness. Thanks to Chouser and Chousuke on the #clojure channel in IRC, I think I have a slightly better grasp of the thing.</p>
<p>Update: Here&#8217;s a summary for those who don&#8217;t want to read the whole thing. What was happening is this &#8211; my assumption was that dynamic vars get captured in lazy objects &#8211; just like locals get captured in closures. <strong>They don&#8217;t</strong> &#8211; so if you later rebind the vars, when the lazy objects are realized, the vars will evaluate according to the latest binding. To baaad results <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Here&#8217;s the chat transcript if you want to read the whole thing &#8211; it might help:</p>
<p>[10:24am] <span style="color:red;">erohtar:</span> chouser: u there?</p>
<p>[10:24am] <span style="color:blue;">Chouser:</span> yep</p>
<p>[10:24am] <span style="color:red;">erohtar:</span> chouser: so i was thinking about our conversation yesterday about vars</p>
<p>[10:24am] <span style="color:red;">erohtar:</span> chouser: so vars in clojure arent the same as dynamically scoped variables in common lisp?</p>
<p>[10:25am] <span style="color:blue;">Chouser:</span> I don&#8217;t know CL well enough to say for sure, but my understanding is that they&#8217;re pretty much equivalent.</p>
<p>[10:26am] <span style="color:blue;">Chouser:</span> a possible difference being how threads interact with them &#8212; not sure what CL does there.</p>
<p>[10:26am] <span style="color:red;">erohtar:</span> chouser: the thing is&#8230; in common lisp, if i bing var-a to x, any thing down the call stack will see x&#8230;</p>
<p>[10:27am] <span style="color:red;">erohtar:</span> chouser: in clojure, it doesnt seem to behave like a closure&#8230; (laziness, during realization) is that correct? if var-a was bound to y elsewhere, realization of the old lazy stuff would now see y</p>
<p>[10:28am] <span style="color:red;">erohtar:</span> chouser: is that right?</p>
<p>[10:28am] <span style="color:blue;">Chouser:</span> CL doesn&#8217;t do lazy seqs, right?  so lets ignore that for now</p>
<p>[10:29am] <span style="color:red;">erohtar:</span> chouser: ok&#8230;</p>
<p>[10:29am] <span style="color:blue;">Chouser:</span> let&#8217;s just look at how a closure works.</p>
<p>[10:30am] <span style="color:red;">erohtar:</span> chouser: i think i understand how a closure works, but when a closure is going to be evaluated lazily&#8230; what happens to a var that has since been rebound?</p>
<p>[10:31am] <span style="color:blue;">Chouser:</span> (def var-a 2), (let [add (binding [var-a 5] (fn [n] (+ var-a n)))] (add 3))</p>
<p>[10:31am] <span style="color:blue;">Chouser:</span> ok, here&#8217;s a little example that I would expect to work the same if translated to CL</p>
<p>[10:31am] <span style="color:blue;">Chouser:</span> var-a has a root binding of 2.</p>
<p>[10:31am] <span style="color:red;">erohtar:</span> chouser: that should eval to 8, yes?</p>
<p>[10:31am] <span style="color:blue;">Chouser:</span> no</p>
<p>[10:32am] <span style="color:red;">erohtar:</span> chouser: hmmm</p>
<p>[10:32am] <span style="color:blue;">Chouser:</span> within the dynamic scope of the binding, a closure is created that refers to var-a</p>
<p>[10:32am] <span style="color:red;">erohtar:</span> chouser: ok&#8230; and that is 5?</p>
<p>[10:33am] <span style="color:green;">Chousuke:</span> nah, it&#8217;s var-a</p>
<p>[10:33am] <span style="color:red;">erohtar:</span> oh</p>
<p>[10:33am] <span style="color:blue;">Chouser:</span> at the moment the closure is created, var-a has a thread-local binding of 5, but that doesn&#8217;t really matter because nothing is asking for var-a&#8217;s *value*, just grabbing var-a itself.</p>
<p>[10:33am] <span style="color:red;">erohtar:</span> i see&#8230; ok&#8230;</p>
<p>[10:34am] <span style="color:blue;">Chouser:</span> so that closure is returned to outside the binding, and named &#8216;add&#8217;</p>
<p>[10:34am] <span style="color:green;">Chousuke:</span> I guess it&#8217;s a bit surprising though.</p>
<p>[10:34am] <span style="color:blue;">Chouser:</span> and what does the closure have bundled inside?  it knows it wants one arg n, and it has references to the vars var-a and +</p>
<p>[10:35am] <span style="color:red;">erohtar:</span> and it dynamically uses the value 2, for var-a when it needs it?</p>
<p>[10:35am] <span style="color:blue;">Chouser:</span> right</p>
<p>[10:35am] <span style="color:red;">erohtar:</span> arent closures supposed to capture their environment?</p>
<p>[10:35am] <span style="color:blue;">Chouser:</span> their lexical environment, yes.</p>
<p>[10:35am] <span style="color:red;">erohtar:</span> not the values?</p>
<p>[10:36am] <span style="color:blue;">Chouser:</span> which is why this works: (def var-a 2), (let [m 7, add (binding [var-a 5] (fn [] (+ var-a m)))] (add))</p>
<p>[10:37am] <span style="color:blue;">Chouser:</span> hm, not that&#8217;s not quite right, since m is still available when &#8216;add&#8217; is called.  But anyway, it wouldn&#8217;t have to be.</p>
<p>[10:38am] <span style="color:red;">erohtar:</span> thinking about this</p>
<p>[10:38am] <span style="color:blue;">Chouser:</span> if you think about how dynamic vars are used in clojure.core, you&#8217;ll see it must be this way.</p>
<p>[10:38am] <span style="color:blue;">Chouser:</span> let&#8217;s look at *in*</p>
<p>[10:38am] <span style="color:blue;">Chouser:</span> er, *out*</p>
<p>[10:38am] <span style="color:green;">Chousuke:</span> ,(let [x 1 add (fn [] (+ 4 x))] [(binding [x 2] (add)) (binding [+ -] (add))])</p>
<p>[10:38am] clojurebot: java.lang.Exception: Unable to resolve var: x in this context</p>
<p>[10:38am] <span style="color:green;">Chousuke:</span> hmm</p>
<p>[10:39am] <span style="color:red;">erohtar:</span> ok &#8211; talking about *out* &#8211; is that the std-out ?</p>
<p>[10:39am] <span style="color:blue;">Chouser:</span> somewhere there&#8217;s something like (def *out* System/out)</p>
<p>[10:39am] <span style="color:blue;">Chouser:</span> right</p>
<p>[10:39am] <span style="color:green;">Chousuke:</span> ,(let [x 1] (let [add (fn [] (+ 4 x))] [(binding [x 2] (add)) (binding [+ -] (add))]))</p>
<p>[10:39am] <span style="color:red;">erohtar:</span> right</p>
<p>[10:39am] clojurebot: java.lang.Exception: Unable to resolve var: x in this context</p>
<p>[10:39am] <span style="color:blue;">Chouser:</span> that&#8217;s the root binding of *out*</p>
<p>[10:39am] <span style="color:red;">erohtar:</span> ok, with u so far</p>
<p>[10:39am] <span style="color:green;">Chousuke:</span> hmm, I guess binding let-bound variables does not work</p>
<p>[10:40am] <span style="color:blue;">Chouser:</span> then you&#8217;ve got functions like prn that use *out*</p>
<p>[10:40am] <span style="color:red;">erohtar:</span> yes&#8230; i think they need to be vars</p>
<p>[10:40am] <span style="color:red;">erohtar:</span> right</p>
<p>[10:40am] kotarak: let-bound locals are not Vars</p>
<p>[10:40am] <span style="color:green;">Chousuke:</span> hmm</p>
<p>[10:40am] <span style="color:green;">Chousuke:</span> ,(let [x 1] #&#8217;x)</p>
<p>[10:40am] clojurebot: java.lang.Exception: Unable to resolve var: x in this context</p>
<p>[10:41am] <span style="color:blue;">Chouser:</span> we can pretend prn is defined something like (defn prn [&amp; args] (.write *out* args))</p>
<p>[10:41am] <span style="color:red;">erohtar:</span> when u create a lazy something, that uses prn, what happens?</p>
<p>[10:41am] <span style="color:green;">Chousuke:</span> indeed.</p>
<p>[10:41am] <span style="color:blue;">Chouser:</span> now, if prn took the value of the *out* when it was defined, there would be no way to get prn to write to any other stream &#8212; it would always use the root binding, since that&#8217;s what was in play when prn was defined.</p>
<p>[10:42am] <span style="color:blue;">Chouser:</span> but that&#8217;s not how it works.</p>
<p>[10:42am] <span style="color:blue;">Chouser:</span> prn has a reference to the Var *out*, not its value.</p>
<p>[10:42am] <span style="color:red;">erohtar:</span> so something like  &#8211; (map #(prn %)<br />
[1 2 3])</p>
<p>[10:42am] <span style="color:red;">erohtar:</span> chouser: i completely agree</p>
<p>[10:42am] <span style="color:blue;">Chouser:</span> so instead, prn will use the dynamic value of *out* when you call it.</p>
<p>[10:42am] <span style="color:red;">erohtar:</span> chouser: hmmm</p>
<p>[10:42am] <span style="color:blue;">Chouser:</span> ,(with-out-str (prn 5 10))</p>
<p>[10:42am] clojurebot: &#8220;5 10\n&#8221;</p>
<p>[10:43am] <span style="color:blue;">Chouser:</span> got it?</p>
<p>[10:43am] <span style="color:red;">erohtar:</span> i do</p>
<p>[10:43am] <span style="color:red;">erohtar:</span> im thinking about my confusion &#8211; and trying to figure out the question to ask next</p>
<p>[10:44am] <span style="color:red;">erohtar:</span> so lets take my example -</p>
<p>[10:44am] <span style="color:red;">erohtar:</span> (map #(prn %)<br />
[1 2 3])</p>
<p>[10:45am] <span style="color:red;">erohtar:</span> now, somewhere else, i rebind *out* to str&#8230; and in there, if i use the above object, will it also use the new binding?</p>
<p>[10:45am] <span style="color:red;">erohtar:</span> it will&#8230;</p>
<p>[10:45am] <span style="color:red;">erohtar:</span> i think im getting it now</p>
<p>[10:46am] <span style="color:red;">erohtar:</span> ok &#8211; final thought -</p>
<p>[10:46am] <span style="color:blue;">Chouser:</span> (def q (map #(prn %)<br />
[1 2 3])), (with-out-str (doall q))</p>
<p>[10:47am] <span style="color:blue;">Chouser:</span> so yes, even though you created the lazy seq and closure outside the with-out-str, since its not computed or realized until the doall which is within the with-out-str, all those prn&#8217;s will use the binding provided by with-out-str</p>
<p>[10:47am] <span style="color:red;">erohtar:</span> got it&#8230;</p>
<p>[10:48am] <span style="color:red;">erohtar:</span> so my final thought &#8211; what if instead of *out* we&#8217;re talking about a connection to a database&#8230; bad use of vars, right?</p>
<p>[10:48am] <span style="color:blue;">Chouser:</span> now lazy seqs can be a bit tricky because they cache their results and can effectively contain multiple closures.</p>
<p>[10:49am] <span style="color:blue;">Chouser:</span> hmm&#8230;.</p>
<p>[10:49am] <span style="color:red;">erohtar:</span> if i read from a db a list of objects using a var that holds database config&#8230;. and then i rebind the config to write into another database</p>
<p>[10:49am] <span style="color:blue;">Chouser:</span> if you&#8217;re going to open a connection to a database, do a batch of work in a single thread on it, and then close the connection &#8212; then that would be a fine use.</p>
<p>[10:50am] <span style="color:red;">erohtar:</span> since the initial read may be lazy&#8230; when it realizes&#8230; it will &#8216;read from the wrong place&#8217;&#8221;?</p>
<p>[10:50am] <span style="color:blue;">Chouser:</span> right</p>
<p>[10:50am] <span style="color:blue;">Chouser:</span> basically you want to be very careful about passing closures (and therefore lazy seqs) past &#8216;binding&#8217; boundaries.</p>
<p>[10:50am] <span style="color:red;">erohtar:</span> chouser: on that thought &#8211; what i did was, i read from a db, and to write into the other, i created agents, binding the new db config in each</p>
<p>[10:51am] <span style="color:red;">erohtar:</span> chouser: it didnt work&#8230;</p>
<p>[10:51am] <span style="color:red;">erohtar:</span> chouser: ok&#8230; im beginning to understand &#8230;</p>
<p>[10:51am] <span style="color:red;">erohtar:</span> i need to rethink what im doing</p>
<p>[10:52am] <span style="color:red;">erohtar:</span> and avoid using vars for some of this stuff, since im doing a lot of agent related stuff</p>
<p>[10:52am] <span style="color:blue;">Chouser:</span> yes, I&#8217;d recommend trying to have the behavior of as much of your code as possible depend only on the args passed directly in.</p>
<p>[10:53am] <span style="color:red;">erohtar:</span> chouser: this whole mess started when i wanted to avoid passing db-config around everywhere</p>
<p>[10:53am] <span style="color:blue;">Chouser:</span> this is better for testing, reasoning about behavior, easier to work with threads, closures, laziness, etc.</p>
<p>[10:53am] <span style="color:blue;">Chouser:</span> heh, yeah.</p>
<p>[10:53am] <span style="color:red;">erohtar:</span> yup &#8211; i hear you</p>
<p>[10:53am] <span style="color:blue;">Chouser:</span> well, another thing that may be helpful &#8212; just guessing since I haven&#8217;t seen your code&#8230;</p>
<p>[10:53am] <span style="color:red;">erohtar:</span> thanks a ton for your time &#8211; ur incredibly helpful</p>
<p>[10:54am] <span style="color:red;">erohtar:</span> ok?</p>
<p>[10:54am] <span style="color:blue;">Chouser:</span> would be to try to keep as much of your code purely functional as possible.</p>
<p>[10:54am] <span style="color:red;">erohtar:</span> yes &#8211; i understand</p>
<p>[10:54am] <span style="color:blue;">Chouser:</span> so avoid writing a function that takes some args, does some computation, and then writes to a db.  Instead, have a fn that takes args, does computation, returns result.</p>
<p>[10:55am] <span style="color:blue;">Chouser:</span> then perhaps you don&#8217;t have to pass in db config at all &#8212; whoever calls this fn can to the db write itself.</p>
<p>[10:55am] <span style="color:red;">erohtar:</span> i think it mostly is&#8230; except for this stuff&#8230; where i ended up inadvertently depending on &#8220;global&#8221; db-config</p>
<p>[10:55am] <span style="color:red;">erohtar:</span> well, this is a persistence layer</p>
<p>[10:56am] <span style="color:red;">erohtar:</span> i take an object in, break it up etc., and put it into hbase</p>
<p>[10:56am] <span style="color:red;">erohtar:</span> the code is open-sourece&#8230; would u like to see?</p>
<p>[10:56am] <span style="color:blue;">Chouser:</span> hm, sure.  I may need to ramp up on hbase soon.</p>
<p>[10:56am] <span style="color:red;">erohtar:</span> http://github.com/amitrathore/capjure/blob/b939a7038ebb4aed0d068d6e86291cc881ecf72b/src/org/rathore/amit/capjure.clj</p>
<p>[10:57am] <span style="color:red;">erohtar:</span> its kind a messy &#8211; learning clojure and hbase while doing this&#8230;</p>
<p>[10:57am] <span style="color:blue;">Chouser:</span></p>
<p>[10:57am] <span style="color:green;">Chousuke:</span> <span style="color:red;">erohtar:</span> first thought: use some newlines.</p>
<p>[10:58am] danlarkin: Noooooooooooo -jure</p>
<p>[10:58am] <span style="color:red;">erohtar:</span> danlarkin: haha &#8211; u&#8217;ve told me that already</p>
<p>[10:58am] <span style="color:blue;">Chouser:</span> danlarkin: hey, at least he&#8217;s got a name</p>
<p>[10:58am] <span style="color:green;">Chousuke:</span> <span style="color:red;">erohtar:</span> one newline after each def/defn would make it a lot nicer</p>
<p>[10:58am] danlarkin: erohtar, <span style="color:blue;">Chouser:</span></p>
<p>[10:58am] <span style="color:red;">erohtar:</span> ok &#8211; more newlines, check</p>
<p>[10:59am] <span style="color:green;">Chousuke:</span> well, the def group is fine I guess.</p>
<p>[10:59am] <span style="color:green;">Chousuke:</span> but (defn foo) (defn bar) without an empty line in the middle is a bit weird</p>
<p>[11:00am] <span style="color:red;">erohtar:</span> ok &#8211; those were sort of just quick utility functions&#8230; but i&#8217;ll put newlines</p>
<p>[11:00am] <span style="color:green;">Chousuke:</span> newlines never hurt anyone</p>
<p>[11:00am] <span style="color:blue;">Chouser:</span> <span style="color:red;">erohtar:</span> this is what you get for posting code.  unsolicited advice.</p>
<p>[11:01am] <span style="color:red;">erohtar:</span> haha</p>
<p>[11:01am] <span style="color:red;">erohtar:</span> its fine &#8211; maybe i&#8217;ll learn something too</p>
<p>[11:01am] <span style="color:blue;">Chouser:</span> but while we&#8217;re at it &#8212; I much prefer to have the code arranged so that &#8216;declare&#8217; is only needed in mutually-recusive situations.</p>
<p>[11:01am] <span style="color:blue;">Chouser:</span> though others may disagree with me</p>
<p>[11:02am] <span style="color:red;">erohtar:</span> right &#8211; i struggled with that one &#8211; cause i dont like to have to order the functions in &#8220;reverse&#8221;</p>
<p>[11:02am] • Chouser nods</p>
<p>[11:02am] <span style="color:red;">erohtar:</span> see &#8211; capjure-insert is the only one that most people will call</p>
<p>[11:03am] <span style="color:green;">Chousuke:</span> <span style="color:red;">erohtar:</span> you could add more newlines and some kind of &#8220;headlines&#8221; for sections of code</p>
<p>[11:03am] <span style="color:red;">erohtar:</span> u bind the config stuff &#8211; *hbase-master* and *primary-keys-config* &#8211; and then u call capjure-insert with the params</p>
<p>[11:03am] <span style="color:green;">Chousuke:</span> so people can easily skip the helper function blocks</p>
<p>[11:03am] <span style="color:red;">erohtar:</span> most of the remaining functions are functional</p>
<p>[11:03am] <span style="color:red;">erohtar:</span> only the ones dealing with hbase have (obvious) side-effects</p>
<p>[11:04am] <span style="color:red;">erohtar:</span> and it works fine&#8230; and its in production too&#8230; ran into issues trying to use TWO hbase configs (to move data between them)</p>
<p>[11:04am] <span style="color:green;">Chousuke:</span> that&#8217;s a lot of functions for one namespace too, though</p>
<p>[11:05am] <span style="color:blue;">Chouser:</span> I really don&#8217;t think more namespaces would be better.</p>
<p>[11:05am] <span style="color:green;">Chousuke:</span> if possible maybe it&#8217;d make sense to put the &#8220;side-effecting&#8221; functions (that deal with hbase) in a separate file or namespace?</p>
<p>[11:05am] <span style="color:red;">erohtar:</span> cause lazy lists of data from one hbase, seem to be getting messed up when i rebind the config to start writing into the other hbase</p>
<p>[11:06am] <span style="color:red;">erohtar:</span> do u know what i mean? (trying to get discussion back to lazy closures and bindings  )</p>
<p>[11:06am] <span style="color:green;">Chousuke:</span> ah</p>
<p>[11:06am] <span style="color:green;">Chousuke:</span> right, I can see why that happens</p>
<p>[11:06am] <span style="color:green;">Chousuke:</span> the laziness gets evaluated in the context of the new hbase.</p>
<p>[11:06am] <span style="color:red;">erohtar:</span> well, im only starting to see it  thanks to u guys&#8230;</p>
<p>[11:06am] <span style="color:red;">erohtar:</span> yea</p>
<p>[11:06am] <span style="color:red;">erohtar:</span> drove me crazy</p>
<p>[11:06am] <span style="color:red;">erohtar:</span> and i did all this only so i wouldnt need to pass the db config around all the functions</p>
<p>[11:06am] <span style="color:red;">erohtar:</span> like u said, there are a lot of them&#8230;</p>
<p>[11:07am] <span style="color:green;">Chousuke:</span> which means you will have to move away from using global vars for config and instead create something you explicitly pass around to the functions.</p>
<p>[11:07am] <span style="color:red;">erohtar:</span> so now what&#8230; ? pass the config around everywhere?</p>
<p>[11:08am] <span style="color:green;">Chousuke:</span> maybe only to a few core functions.</p>
<p>[11:08am] <span style="color:red;">erohtar:</span> yea</p>
<p>[11:08am] <span style="color:green;">Chousuke:</span> which then would bind the dynamic vars.</p>
<p>[11:09am] <span style="color:red;">erohtar:</span> well, if i pass it in, i dont need dynamic vars, rite</p>
<p>[11:09am] <span style="color:blue;">Chouser:</span> or don&#8217;t use lazy seqs</p>
<p>[11:09am] <span style="color:blue;">Chouser:</span> use doall or stuff them in a vector first</p>
<p>[11:09am] <span style="color:red;">erohtar:</span> hmmm</p>
<p>[11:09am] cp2 is now known as c|p.</p>
<p>[11:09am] <span style="color:red;">erohtar:</span> that might be one solution</p>
<p>[11:09am] <span style="color:green;">Chousuke:</span> right, if they are side-effecting that might be better.</p>
<p>[11:09am] <span style="color:green;">Chousuke:</span> though maybe not memory-efficient</p>
<p>[11:10am] <span style="color:red;">erohtar:</span> yea</p>
<p>[11:10am] <span style="color:red;">erohtar:</span> i think i will get rid of the dynamic vars</p>
<p>[11:10am] <span style="color:green;">Chousuke:</span> it&#8217;s a shame though</p>
<p>[11:10am] <span style="color:red;">erohtar:</span> what is the pattern for this kind of &#8216;static&#8217; config being passed around?</p>
<p>[11:10am] <span style="color:green;">Chousuke:</span> they make the code much easier to write</p>
<p>[11:10am] <span style="color:green;">Chousuke:</span> I wonder if there&#8217;s a better solution</p>
<p>[11:11am] <span style="color:green;">Chousuke:</span> some way to &#8220;bind&#8221; the current config to when the lazy-seq is first created</p>
<p>[11:11am] <span style="color:red;">erohtar:</span> in typical OO, u can create an instance variable to hold the config &#8211; i wonder if in clojure u can create something like that&#8230; and since its immutable, it should be fine, rite?</p>
<p>[11:11am] <span style="color:blue;">Chouser:</span> yes</p>
<p>[11:11am] <span style="color:green;">Chousuke:</span> maybe do (let [config *global*] (return-some-lazy-seq-qith config))?</p>
<p>[11:11am] <span style="color:red;">erohtar:</span> but there is no way to do that, rite?</p>
<p>[11:11am] <span style="color:green;">Chousuke:</span> would that fix the value?</p>
<p>[11:12am] <span style="color:red;">erohtar:</span> yea</p>
<p>[11:12am] <span style="color:blue;">Chouser:</span> stuff all your config into a map, and pass that single thing into functions that need to use it</p>
<p>[11:12am] <span style="color:red;">erohtar:</span> i thought of doing something like that&#8230; but other functions couldnt access those values&#8230;</p>
<p>[11:12am] <span style="color:red;">erohtar:</span> i thought of simulating an &#8216;immutable object oriented system&#8217; -</p>
<p>[11:12am] brianh joined the chat room.</p>
<p>[11:12am] <span style="color:red;">erohtar:</span> like how u do in javascript&#8230; but immutable</p>
<p>[11:13am] <span style="color:green;">Chousuke:</span> I did in clojurebot so that I pass a map of config stuff around with every function</p>
<p>[11:13am] <span style="color:red;">erohtar:</span> return a closure with the vars bound with a let&#8230; and then closures for all the other functions also</p>
<p>[11:13am] <span style="color:green;">Chousuke:</span> it&#8217;s a bit tedious though</p>
<p>[11:13am] <span style="color:red;">erohtar:</span> i dunno if im being clear</p>
<p>[11:13am] <span style="color:red;">erohtar:</span> very tedious</p>
<p>[11:14am] <span style="color:red;">erohtar:</span> macros can help</p>
<p>[11:14am] <span style="color:red;">erohtar:</span> essentially what id be doing is creating an immutable OO system</p>
<p>[11:14am] <span style="color:red;">erohtar:</span> something like that</p>
<p>[11:14am] <span style="color:red;">erohtar:</span> does that make sense?</p>
<p>[11:14am] <span style="color:green;">Chousuke:</span> <span style="color:red;">erohtar:</span> but your problem is only with lazy seqs right?</p>
<p>[11:14am] <span style="color:red;">erohtar:</span> lazy seqs and dynamic vars</p>
<p>[11:14am] <span style="color:green;">Chousuke:</span> <span style="color:red;">erohtar:</span> don&#8217;t you only need to fixate the config when returning a seq</p>
<p>[11:14am] <span style="color:red;">erohtar:</span> the interplay</p>
<p>[11:14am] <span style="color:red;">erohtar:</span> yes</p>
<p>[11:15am] <span style="color:blue;">Chouser:</span> <span style="color:red;">erohtar:</span> clojure.zip does something like that</p>
<p>[11:15am] <span style="color:red;">erohtar:</span> chouser: like what?</p>
<p>[11:15am] <span style="color:blue;">Chouser:</span> each node is a object with a several functions attached as metadata</p>
<p>[11:16am] <span style="color:red;">erohtar:</span> chouser: ah, i see</p>
<p>[11:16am] <span style="color:red;">erohtar:</span> got it</p>
<p>[11:16am] <span style="color:blue;">Chouser:</span> most of the api fns are like (children my-node), where children calls fns from my-node&#8217;s metadata, passing in my-node</p>
<p>[11:16am] <span style="color:red;">erohtar:</span> i see -</p>
<p>[11:17am] <span style="color:red;">erohtar:</span> thats rather cool &#8211; so u can basically have my-node equivalent contain the config data</p>
<p>[11:17am] <span style="color:blue;">Chouser:</span> sure</p>
<p>[11:18am] <span style="color:red;">erohtar:</span> alright folks, thanks a lot for this conversation &#8211; im going to experiment and see how to get this done with the least amount of work</p>
<p>[11:18am] <span style="color:blue;">Chouser:</span> the only reason zip nodes have fns in metadata is so it can be polymorphic.</p>
<p>[11:18am] <span style="color:red;">erohtar:</span> yes- makes sense</p>
<p>[11:18am] <span style="color:red;">erohtar:</span> (then everyone can tell me how my code sucks  )</p>
<p>[11:18am] <span style="color:blue;">Chouser:</span> if you&#8217;re going to only have one kind of db config (seems likely) then you don&#8217;t need the metadata piece.</p>
<p>[11:19am] <span style="color:red;">erohtar:</span> yes &#8211; just need to take care of the fact that the config will be rebound&#8230; so just closures that capture the config, and the functions that use it</p>
<p>[11:19am] <span style="color:red;">erohtar:</span> something like that</p>
<p>P.S. <span style="color:red;">erohtar</span> is my handle on IRC&#8230; it&#8217;s the reverse of my last name. In case you were wondering.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sexp.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sexp.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sexp.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sexp.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sexp.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sexp.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sexp.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sexp.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sexp.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sexp.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sexp.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sexp.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sexp.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sexp.wordpress.com/140/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=s-expressions.com&amp;blog=4254185&amp;post=140&amp;subd=sexp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://s-expressions.com/2009/02/27/clojure-understanding-dynamic-vars-and-laziness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/69c7419d10661aa2ab01de450a0127b2?s=96&#38;d=monsterid" medium="image">
			<media:title type="html">Amit Rathore</media:title>
		</media:content>
	</item>
		<item>
		<title>More on Lisp syntax, and language extensions</title>
		<link>http://s-expressions.com/2007/02/16/more-on-lisp-syntax-and-language-extensions/</link>
		<comments>http://s-expressions.com/2007/02/16/more-on-lisp-syntax-and-language-extensions/#comments</comments>
		<pubDate>Sat, 17 Feb 2007 00:22:30 +0000</pubDate>
		<dc:creator>Amit Rathore</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[meta]]></category>

		<guid isPermaLink="false">http://epistemologic.wordpress.com/2007/02/16/more-on-lisp-syntax-and-language-extensions/</guid>
		<description><![CDATA[Following my recent post on the topic, I thought of one more thing that the syntax of Lisp allows you to do. Being homoiconic, and the fact that code manipulation is so simple (it&#8217;s all lists), layering on &#8220;language extensions&#8221; becomes possible. For example, if Betty Programmer realizes that OO is a great way to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=s-expressions.com&amp;blog=4254185&amp;post=40&amp;subd=sexp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Following my <a href="http://epistemologic.com/2007/02/14/lisp-syntax-and-when-code-is-data/">recent post</a> on the topic, I thought of one more thing that the syntax of Lisp allows you to do. Being homoiconic, and the fact that code manipulation is so simple (it&#8217;s all lists), layering on &#8220;language extensions&#8221; becomes possible. For example, if Betty Programmer realizes that OO is a great way to design and write code but that Lisp by itself doesn&#8217;t provide an OO facility (there are no &#8220;class&#8221; constructs, no inheritance etc.) &#8211; she doesn&#8217;t need to despair.</p>
<p>She can write code to add an OOP system to the language. Yes, this means Lisp really blurs the distinction between the language designer and the programmer. In other words, while it&#8217;s fairly obvious that Lisp is very well suited to writing DSLs, it is also possible to fundamentally extend the language as well &#8211; like adding an OO system, or pattern-matching, or logic-programming (ala Prolog).</p>
<p>Now, obviously, I&#8217;m not proficient enough yet to do anything of this sort. But, as I said before, it is my intention to learn <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Lisp. A language where being meta is something worth thinking about.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sexp.wordpress.com/40/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sexp.wordpress.com/40/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sexp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sexp.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sexp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sexp.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sexp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sexp.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sexp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sexp.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sexp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sexp.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sexp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sexp.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sexp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sexp.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=s-expressions.com&amp;blog=4254185&amp;post=40&amp;subd=sexp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://s-expressions.com/2007/02/16/more-on-lisp-syntax-and-language-extensions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/69c7419d10661aa2ab01de450a0127b2?s=96&#38;d=monsterid" medium="image">
			<media:title type="html">Amit Rathore</media:title>
		</media:content>
	</item>
		<item>
		<title>Lisp syntax, and when code is data</title>
		<link>http://s-expressions.com/2007/02/14/lisp-syntax-and-when-code-is-data/</link>
		<comments>http://s-expressions.com/2007/02/14/lisp-syntax-and-when-code-is-data/#comments</comments>
		<pubDate>Wed, 14 Feb 2007 22:55:32 +0000</pubDate>
		<dc:creator>Amit Rathore</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[meta]]></category>

		<guid isPermaLink="false">http://epistemologic.com/2007/02/14/lisp-syntax-and-when-code-is-data/</guid>
		<description><![CDATA[Like I said earlier, my friend Ravi introduced me to Lisp several years ago, but it has taken me many years to really want to learn it well enough. I&#8217;ll write about my reasons in another post. In any event, at the beginning of this year, I started to pick it up again, promising myself [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=s-expressions.com&amp;blog=4254185&amp;post=38&amp;subd=sexp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Like I said earlier, my friend <a href="http://ravimohan.blogspot.com">Ravi</a> introduced me to Lisp several years ago, but it has taken me many years to really <em>want</em> to learn it well enough. I&#8217;ll write about my reasons in another post. In any event, at the beginning of this year, I started to pick it up again, promising myself that I&#8217;d be serious. This time. So far so good.</p>
<p>I think I&#8217;ve started to grok one of the core ideas of Lisp. I had always read that the syntax of Lisp was one of its strengths. And I had always struggled with that idea, knowing it was important, yet was quite unable to really put my finger on it. I think I&#8217;m closer to it today.</p>
<p>If you had to create a programming language to write programs that wrote programs (as in, say DSLs) &#8211; what design choices would you make?</p>
<p>For one thing, you&#8217;d have to be able to generate and manipulate (walk parse-trees, compare and transform nodes etc.) code as though it were just another data-structure. Right? OK, so the code that was being generated would look like and behave like data.</p>
<p>You would then create an EVAL function that could run the generated code. Maybe your generated code would in turn produce generated code, so to keep things easy and simple, your language syntax would be the same as that of the generated language. In other words, you&#8217;d end up with a homoiconic programming language. Finally, you would bootstrap your language processor and arrive at your final metacircular evaluator.</p>
<p>To recap, this language would have syntax that looked and behaved like data and because of it could generate and manipulate that data, which itself could be code. What would this data structure look like? One obvious choice for this is a tree (because of parse-trees). If you think about it, XML is just like a tree. But it&#8217;s kludgey. What we want is something like XML but without all the cruft. For example -</p>
<pre>
<code>
&lt;program&gt;
	&lt;function&nbsp;name=\&quot;add_to_stock\&quot;&gt;
		&lt;param&nbsp;name=\&quot;counter\&quot;&nbsp;/&gt;
			&lt;call_function&nbsp;name=\&quot;increment\&quot;&gt;
				&lt;argument&nbsp;value=\&quot;counter\&quot;/&gt;
			&lt;/call_function&gt;
	&lt;/function&gt;

	&lt;function&nbsp;name=\&quot;remove_from_stock\&quot;&gt;
		&lt;param&nbsp;name=\&quot;item\&quot;/&gt;
			&lt;call_function&nbsp;&nbsp;name=\&quot;decrement_from_stock_file\&quot;&gt;
				&lt;argument&nbsp;value=\&quot;item\&quot;/&gt;
			&lt;/call_function&gt;
	&lt;/funtion&gt;
&lt;/program&gt;
</code>
</pre>
<p>The syntax is truly disgusting, but useful &#8211; especially if you need to programmatically generate it. Let&#8217;s now try to make it easier for humans, too. I&#8217;m going to remove the &#8216;program&#8217; tag, because all this stuff is code. I&#8217;m going to then change from XML tags to simple &#8216;(&#8216; and &#8216;)&#8217; without the names &#8211; and make an assumption &#8211; the first word that appears is always a function call. Except for define &#8211; which I&#8217;ll use to denote a definition for a function. I&#8217;ll also lose the XML attribute names, assuming that words that follow the function name are always parameters (unless it&#8217;s a code block itself &#8211; which would get evaluated first). So, we&#8217;re left with -</p>
<pre>
<code>
<strong>

(define (add_to_stock counter)
    (increment counter))

 (define (remove_from_stock item)
    (decrement_from_stock_file item))

</strong>
</code>
</pre>
<p>Where does this leave us?</p>
<p>It&#8217;s the same exact XML syntax, but it&#8217;s just a bit modified and has a few rules thrown in. Importantly, it&#8217;s still as easy to generate as XML. It&#8217;s just a list of lists of words. As in, a unit of code in this format would always start and end with parenthesis, and they would enclose either a bunch of zero or more symbols, or other lists.</p>
<p>In fact, a language that was good at <strong>lis</strong>t <strong>p</strong>rocessing and had an eval function would probably do a really good job with this stuff!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sexp.wordpress.com/38/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sexp.wordpress.com/38/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sexp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sexp.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sexp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sexp.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sexp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sexp.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sexp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sexp.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sexp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sexp.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sexp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sexp.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sexp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sexp.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=s-expressions.com&amp;blog=4254185&amp;post=38&amp;subd=sexp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://s-expressions.com/2007/02/14/lisp-syntax-and-when-code-is-data/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/69c7419d10661aa2ab01de450a0127b2?s=96&#38;d=monsterid" medium="image">
			<media:title type="html">Amit Rathore</media:title>
		</media:content>
	</item>
		<item>
		<title>Beyond objects &#8211; I</title>
		<link>http://s-expressions.com/2007/02/09/beyond-objects-i/</link>
		<comments>http://s-expressions.com/2007/02/09/beyond-objects-i/#comments</comments>
		<pubDate>Fri, 09 Feb 2007 19:53:17 +0000</pubDate>
		<dc:creator>Amit Rathore</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[meta]]></category>

		<guid isPermaLink="false">http://epistemologic.wordpress.com/2007/02/09/beyond-objects-i/</guid>
		<description><![CDATA[&#8230; It&#8217;s in words that the magic is&#8211;Abracadabra, Open Sesame, and the rest&#8211;but the magic words in one story aren&#8217;t magical in the next. The real magic is to understand which words work, and when, and for what; the trick is to learn the trick. &#8230; And those words are made from the letters of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=s-expressions.com&amp;blog=4254185&amp;post=34&amp;subd=sexp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><i>&#8230; It&#8217;s in words that the magic is&#8211;Abracadabra, Open Sesame, and the rest&#8211;but the magic words in one story aren&#8217;t magical in the next. The real magic is to understand which words work, and when, and for what; the trick is to learn the trick.<br />
&#8230; And those words are made from the letters of our alphabet: a couple-dozen squiggles we can draw with the pen. This is the key! And the treasure, too, if we can only get our hands on it! It&#8217;s as if&#8211;as if the key to the treasure is the treasure!</i></p>
<p>John Barth, <i>Chimera</i></p>
<p>It&#8217;s a copy of a quotation that appears in <a href="http://mitpress.mit.edu/sicp/full-text/sicp/book/">The Structure and Interpretation of Programs</a>. It speaks to the fact that if you know the name of something, as in a way to refer to it, then you have control over it. Like functions or closures for instance. If you can refer to it by name, you can tell it to someone else. Or pass it to another function. You can remember the name, so you can speak it when you&#8217;re ready. When the <i>context</i> is right. Like when all the variables, objects, and functions have <i>aligned</i>.</p>
<p>The idea of metalinguistic abstraction is a fundamental one. It&#8217;s a bottom-up way of thinking about your problem-space, and figuring out primitives and operations on those primitives. Rather than trying to build a system that satisfies a particular set of constraints in the given problem-space, the idea is to build a way to express more complex concepts using those very primitives and operations. That way, changes to the system can be made by changing higher level constructs &#8211; and going further down the layered stack of abstractions as needed, depending on how fundamental the changes are. This way, changes (or any new set of constraints) can be handled in a more clean and elegant fashion, and the entire system benefits automatically by a change made at a lower level.</p>
<p>If this sounds like building what these days is called a domain-specific language (DSL), then sure, but it is not a new way of doing things. It is however, possible to do a lot of this stuff easier in &#8220;enterprise-type&#8221; scenarios, because of a larger acceptance of more dynamic languages. Like Ruby, for example. It isn&#8217;t particularly easy to write software this way &#8211; it involves a change in the way most of us were taught programming. Everything is not imperative any more, code doesn&#8217;t have to be &#8220;written&#8221; before it runs, lines between data and code begin to blur. On that last point, what is a closure? Is it data or is it a procedure? It&#8217;s sometimes one, sometimes the other, and occasionally both, and indeed, sometimes it is neither. Here&#8217;s an example -</p>
<p><code><br />
def create_coord(x, y)<br />
   Proc.new { |selector|<br />
    if selector == <img src='http://s2.wp.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /><br />
      x<br />
    elsif selector == :y<br />
      y<br />
    end<br />
  }<br />
end</p>
<p>c = create_coord 10, 20<br />
puts "X is " + c.call(:x).to_s<br />
puts "Y is " + c.call(:y).to_s<br />
</code></p>
<p>So, the question here is, what is <b>c</b>? Is it data? Or is it code, that does something? Here, <b>c</b> is an object that represents a cartesian coordinate, but without any explicit data elements like variables or attributes&#8230; so, what is it?</p>
<p>Note &#8211; This is Ruby code, but a different language, (like <a href="http://www.lua.org/">Lua</a>, for example, that has syntactic sugar for things like keys on a hash table) could make it unnecessary for the <i>call(:symbol)</i> syntax and instead, just calling x and y would translate it to a call on the object with the given method name.</p>
<p>The point, however, is that there are more ways to think about abstraction than just OO, and to paraphrase Paul Graham, some of them can transcend objects. The bottom-up approach itself can be implemented using plain objects and OO thinking, but there are other ways which can make for some powerful expressablilty. The code above approaches functional programming, and I&#8217;m interested in scaling that model to build large systems from pure or near-pure functional constructs.  I&#8217;m nowhere close to being proficient in writing software this way, but it is my intention to learn.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sexp.wordpress.com/34/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sexp.wordpress.com/34/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sexp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sexp.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sexp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sexp.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sexp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sexp.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sexp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sexp.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sexp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sexp.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sexp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sexp.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sexp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sexp.wordpress.com/34/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=s-expressions.com&amp;blog=4254185&amp;post=34&amp;subd=sexp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://s-expressions.com/2007/02/09/beyond-objects-i/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/69c7419d10661aa2ab01de450a0127b2?s=96&#38;d=monsterid" medium="image">
			<media:title type="html">Amit Rathore</media:title>
		</media:content>
	</item>
	</channel>
</rss>
