Absolutely. I typed up an example in Scala.

class RentOrSell(val Months_To_Find_Tenant: Int, val Months_In_Lease:
Int, val Lease_Cycles: Int, val Months_To_Sell: Int) {
  val months_in_business: Int = ((Months_To_Find_Tenant +
Months_In_Lease) * Lease_Cycles) + Months_To_Sell
}

When I create a class instance I pass in the user defined variables (I
only used 4 in this example). Those values are then bound as invariant
values and used to create months_in_business which is itself an
invariant value (e.g. bound at class creation time and then
immutable).

This is the effect I was expecting. That I could define both the user
supplied values and the derived values as immutable values. Instead I
had to create thunks for the derived values for the reasons previously
described.

But again I'm completely open to the idea that I'm just going about
this all wrong. I have spent more years of my life than I care to
remember writing OO code so I have no doubt I'm relying on instincts
that just don't apply here.

Ideally I'd love to figure out how to properly write a program that
implements http://www.goland.org/sellorrent.pdf in Clojure rather than
beat Clojure into looking like what I'm used to from my OO days. Put
another way, I speak Clojure with a really thick OO accent and I'd
like to learn better how to speak it like a native. :)

       Yaron

On Feb 17, 11:53 am, Laurent PETIT <laurent.pe...@gmail.com> wrote:
> Hello,
>
> 2010/2/17Yaron<ygol...@gmail.com>:
>
>
>
> > I actually started off doing exactly what you suggested. The original
> > version of my program used a map for the arguments and then used
> > explicit arguments when the number of arguments fell to a reasonable
> > level.
>
> > For example, I started off with:
>
> > (defn months_in_business
> >        "The total number of months we will be in the business of renting out
> > our home"
> >        [Months_To_Find_Tenant Months_In_Lease Lease_Cycles Months_To_Sell]
> >        (-> (+ Months_To_Find_Tenant Months_In_Lease) (* Lease_Cycles) (+
> > Months_To_Sell)))
>
> > Which got called as (months_in_business Months_To_Find_Tenant
> > Months_In_Lease Lease_Cycles Months_To_Sell)
>
> > But this was a lot of typing every time I wanted to call the function.
> > So I changed it to:
>
> > (defn months_in_business
> >        "The total number of months we will be in the business of renting out
> > our home"
> >        [:keys [Months_To_Find_Tenant Months_In_Lease Lease_Cycles
> > Months_To_Sell]]
> >        (-> (+ Months_To_Find_Tenant Months_In_Lease) (* Lease_Cycles) (+
> > Months_To_Sell)))
>
> > Which got called as (months_in_business bag_o_args)
>
> > This at least meant less typing when calling the function but defining
> > the function still required a bunch of typing.
>
> > So eventually I just went to:
>
> > (defn months_in_business
> >        "The total number of months we will be in the business of renting out
> > our home"
> >        []
> >        (-> (+ *Months_To_Find_Tenant* *Months_In_Lease*) (* *Lease_Cycles*)
> > (+ *Months_To_Sell*)))
>
> > Which was called as: (months_in_business)
>
> > At least there wasn't much typing involved but now I had a bunch of
> > thunks running around. Which is what brought me to the group in the
> > first place.
>
> > It seems to me that there is a design principal here somewhere that
> > says something like "One shouldn't have to pass static values around
> > as arguments to functions". But because there is nothing like an
> > object context in Clojure this ended up meaning that derived values
> > like months_in_business have to be thunks. Which I though was
> > inelegant.
>
> > If, on the other hand, I was implementing this in an object oriented
> > language I would just say:
>
> > class foo
> >  var A
> >  var B
> >  foo (bag_o_args)
> >  {
> >    A = (bag_o_args A)
> >    B = A+1
> >  }
>
> I would like to try to help, but I first need to understand your
> (pseudo?) OO langage above.
> Could you detail it a little bit more ?
>
>
>
> > And I would be done. No heaps of typing. No thunks. Just what looks to
> > me like a nice simple solution.
>
> > But I recognize that I'm just running home to mommy because my
> > background is OO. That's why I came to the group in the first place.
> > Since I know I'm blinded by my OO past I wanted to see if there was an
> > approach to the problem in Clojure that was as easy and straight
> > forward as what I would have done in an OO language.
>
> > That having been said, defining a bunch of thunks isn't the worst
> > thing in the world. But it is unfortunate from both a design and
> > performance perspective.
>
> >    Yaron
>
> > On Feb 16, 11:07 pm, Richard Newman <holyg...@gmail.com> wrote:
> >> > It seems however that the consensus of the group based on what I've
> >> > said so far is to pass around state in a structmap.
>
> >> Not necessarily a struct-map. Just a map.
>
> >> > This is nice in
> >> > that it makes each function completely self contained (e.g. no
> >> > external references). It's unfortunate in that it now means that every
> >> > single function needs this extra argument and every variable access
> >> > either needs to use the :keys feature in the arguments or has to
> >> > directly refer to the keys in the map.
>
> >> Not necessarily. At some point your functions should be fine-grained
> >> enough that they only take a couple of arguments. As soon as you drop
> >> below 6 or 7, where they're all mandatory, switch to ordinary function
> >> argument style.
>
> >> Wherever you call those functions should do the unpacking.
>
> >> E.g.,
>
> >> (defn outer-1 [{:keys [foo bar baz noo]}]
> >>    (let [interm (foo-bar foo bar)
> >>          fiddle (frostrup baz noo)]
> >>      (tweep interm fiddle)))
>
> >> After all, your house-sale-profit function should be expressed in
> >> terms of two arguments:
>
> >> (defn house-sale-profit [house-sale-price house-sale-expenses]
> >>    ...)
>
> >> It doesn't care about the other 17.
>
> >> Another thing: that big entry point function is like a much tidier
> >> version of the Java constructor that you created with 19 arguments --
> >> tidier in that you can use named keys or a map to identify the
> >> arguments.
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Clojure" group.
> > To post to this group, send email to clojure@googlegroups.com
> > Note that posts from new members are moderated - please be patient with 
> > your first post.
> > To unsubscribe from this group, send email to
> > clojure+unsubscr...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to