More coding fun
Apr. 6th, 2008 11:19 pmJulie was around for the weekend, although she then vanished off this afternoon to slave over a report. It was very relaxing - and I'm leaving plenty of holes in my schedule to make sure we both get more relaxing time, at least until her work calms down again.
Meantime, I've discovered that C# 3.0 allows you to write code like this:
which is the equivalent of the code from the other day, only even more concise. I don't think it's actually any more readable though. Lambda functions seem to be pretty neat - but I haven't spotted many places I'll be using them so far. Maybe when I've played with them a bit more...
Meantime, I've discovered that C# 3.0 allows you to write code like this:
doggies.ForEach(
dog => dog.Paws.ForEach(
paw => paw.Claws.ForEach(
claw => claw.Clip())));which is the equivalent of the code from the other day, only even more concise. I don't think it's actually any more readable though. Lambda functions seem to be pretty neat - but I haven't spotted many places I'll be using them so far. Maybe when I've played with them a bit more...
no subject
Date: 2008-04-06 11:31 pm (UTC)no subject
Date: 2008-04-07 01:04 am (UTC)Aside, here is a raytracer written in LINQ (Those C# 3.0 extentions)
http://blogs.msdn.com/lukeh/archive/2007/10/01/taking-linq-to-objects-to-extremes-a-fully-linqified-raytracer.aspx
no subject
Date: 2008-04-07 06:24 am (UTC)no subject
Date: 2008-04-07 07:49 am (UTC)no subject
Date: 2008-04-07 09:22 am (UTC)no subject
Date: 2008-04-07 09:26 am (UTC)no subject
Date: 2008-04-07 09:30 am (UTC)Presumably you can write anything in any Turning-complete language with a greater or lesser degree of difficulty, depending on what libraries and so forth are available to make the job easier?
no subject
Date: 2008-04-07 09:39 am (UTC)I _like_ strong typing, Object Orientation, etc. In fact, the thought of writing UI code in something that wasn't OOP fills me with dread.
no subject
Date: 2008-04-07 11:09 am (UTC)no subject
Date: 2008-04-07 11:25 am (UTC)no subject
Date: 2008-04-07 11:56 am (UTC)no subject
Date: 2008-04-07 07:35 pm (UTC)A case in point seems to be this 'simple' example of a trivial GTK+ application written in Haskell:
http://www.cse.unsw.edu.au/~chak/haskell/gtk/BoolEd.html
Ow, my aching head.
no subject
Date: 2008-04-07 09:40 am (UTC)no subject
Date: 2008-04-07 07:24 pm (UTC)F# looks potentially interesting.
no subject
Date: 2008-04-08 10:50 am (UTC)no subject
Date: 2008-04-08 02:21 pm (UTC)That's what really gives you the ability to reduce stuff like the above example to a one-liner along the lines of:
map(clip, descend([paws, claws], doggies))
no subject
Date: 2008-04-08 02:32 pm (UTC)I could certainly write a "descend" function that would look through "doggies" for "paws" and then through that for "claws" - using reflection. Ooh - in your example there, what type are "paws" and "claws"? I assume that the first parameter of descend() is an array of something - I'm just not sure what...
Oh - and yes, you can pass around function pointers, if that's what you mean. And they're strongly typed. Look up "delegates".
no subject
Date: 2008-04-08 02:51 pm (UTC)A function that deals with other functions, i.e. takes them as arguments and/or returns them.
The "descend" there is a function that takes a list of functions and then a list of values.
If the values are of type dog, the functions must be of types that chain together from that, e.g. paws is a function of type (dog -> list of paws), claws is (paw -> list of claws), and then the return value of the overall descend will be a list of claws.
The fact the type system is able to understand and enforce things like that is the cool thing.
With function pointers alone, any implementation of descend would have to set in advance the types of the functions it took. Using common forms of reflection, you'd pass in the names of functions as strings and have to do any type checking at runtime.
The descend function would be a line or two in a functional language and implicitly typed, yet formally provable to be typesafe at compile time.
no subject
Date: 2008-04-08 03:21 pm (UTC)C# has some type inference, I'm just trying to work out if I can use it in the way you're talking about. I don't think so though. At compilation it would have to look at the collection, find what type it was a collection of, check that that class had a particular method on it, check that whatever class that method returned had the next method in the array on it, and so on.
I can see how it works, but C# isn't going to allow that. Oh well.
You _can_ pass function pointers about - but not as flexibly as you need in your example while being assured that it would pass type checking when you got there.
no subject
Date: 2008-04-08 03:56 pm (UTC)no subject
Date: 2008-04-08 04:09 pm (UTC)And functions can't have bits of their parameters filled in before they're passed on, so you can't do that. You could fake it in various ways, but it wouldn't be caught by the compiler.
Gosh. All sorts of things I didn't even know I was missing!
no subject
Date: 2008-04-09 04:04 pm (UTC)Descend is still clearly beyond it - and some googling isn't finding me much about its use. Can you point me at a good resource?
no subject
Date: 2008-04-07 12:33 pm (UTC)doggies.each do |dog|
dog.Paws.each do |paw|
paw.Claws.each do |claw|
claw.Clip()
end
end
end
no subject
Date: 2008-04-07 12:51 pm (UTC)But yeah, reminiscient.
Of course, once you get used to imperative languages the basic stuff (loops, branches, assignments) all starts to look similar.
no subject
Date: 2008-04-07 01:08 pm (UTC)And, yes, totally.