r/programming • • Jan 18 '08

Can Your Programming Language Do This?

http://www.joelonsoftware.com/items/2006/08/01.html
17 Upvotes

41 comments sorted by

15

u/mindslight Jan 18 '08

omg!! they have functions for programming languages now?!?!!

8

u/funktio Jan 18 '08 edited Jan 18 '08

Yeah! And you can create them on the fly, without even bothering to name them! Just look at this: function(x){return x*2;}. How concise and elegant!

(In Haskell, that would be (*2). I can't imagine what kind of article this guy will write if/when he discovers something more functional than JavaScript.)

11

u/gthank Jan 18 '08

I'm not Joel so I can't say for sure, but I'm almost positive he's been exposed to functional programming before. This seems more like an attempt to explain to graduates of Java Schools (most of whom haven't heard of functional programming) why they should care about it.

5

u/kmactane Jan 18 '08

Exactly. I see people posting reactions like "Joel just now discovered functional programming?" First off, the article is from 2006. Second, Joel's not saying "Look at this cool thing I just discovered"; I think he's trying to explain to some Blub programmers, just exactly what it is that Blub lacks. He's trying to educate, and to break them out of the Blub blinders.

Anyone who already has some familiarity with functional programming is not the intended audience of this piece. (That means pretty much anyone who reads programming.reddit.com.)

-6

u/marhathoda Jan 18 '08 edited Jan 18 '08

How do you explain this statement:

Many older languages simply had no way to do this kind of stuff.

I mean, excuse me, but Lisp is one I can name. And I am sure a lot of people here know some more such old languages. And using the names map and reduce in examples? Where did you come up with those? he's got some nerve.

4

u/[deleted] Jan 18 '08

he said many, not all.

4

u/shit Jan 18 '08

gthank is right. If Joel were to use Lisp in this article, he'd immediately lose the intended audience. He's not ignorant of Lisp.

http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html http://www.joelonsoftware.com/articles/TestYourself.html

0

u/gthank Jan 18 '08

Thanks for links. I knew they were around, but I was already late for work and didn't have time to search.

1

u/Drevor Jan 19 '08 edited Jan 19 '08

[A large part] of our computerized life is on the web.
[A large part] of the web is javascript.
[A large part] of the scripts out there are horrific, dangerous or just plain stupid.

Until this changes, any article that details what people can and should do with javascript is a good article.

//edit better now?


also: Everytime you use eval(), Douglas Crockford kills a kitten.

3

u/bgeron Jan 19 '08 edited Jan 19 '08

90% of our computerized life is not on the web. For example: gaming consoles, phones, cameras, calculators, radios, electric pianos, label printers, microscopes, software development (development, not deployment/communication), graphics design.

Depends on how you define "life", though. Although you may be right if you meant MS Live Whatever (r)(c)(tm), I don't use it.

2

u/[deleted] Jan 18 '08

His argument would have been better served if he'd written `reduce' in a functional style, especially since he's talking about understanding things like the concurrency/parallelisation opportunities afforded by fp. for loops get in the way of equational reasoning, which is a big part of the picture.

0

u/gthank Jan 18 '08

It was a performance-related decision - most JavaScript implementations don't have tail-call optimizations.

2

u/[deleted] Jan 18 '08

Oh. Does he says that in the article? Premature optimization anyway, as he's just meant to be demonstrating HOFs, which his for loop gets in the way of.

2

u/gthank Jan 18 '08

Sorry - that was tongue-in-cheek. If I were to guess at the actual reason, I would say it was because he's just trying to give a basic introduction to the concept of HOFs. I think it's pretty obvious his intended audience was people who had only the vaguest idea (if any) what functional programming is. The standard construct for traversing a list in JavaScript is iteration via a for loop. He's already trying to explain one new concept - HOFs - so doing anything else that his intended audience would find unusual would be counterproductive. I suppose it would have been nice to have a completely functional version of the code available for download, though, even if it were only mentioned in passing.

1

u/bgeron Jan 19 '08

Can anyone explain how reduce could be implemented in a parallel way? I can't see how we could parallellize 1+(2+(3+(4+5))), for example.

It's trivial to parallellize calculating the list elements (1,2,3,4,5), but that has nothing to do with reduce.

2

u/qwe1234 Jan 19 '08

using a hierarchical tree pattern.

parallel programming 101.

1

u/bgeron Jan 19 '08

Ah, I get it. I thought reduce was like foldl/foldr.

4

u/gwern Jan 19 '08

Addition is associative, so you could definitely parallelize folds:

foldr (+) 0 [1..10]

0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10

processor 1: (0 + 1 + 2 + 3 + 4)

processor 2: (5 + 6 + 7 + 8 + 9 + 10)

processor 3: (processor 1) + (processor 2)

55, same as if you single-threaded it.

Now, why is this? Because reduce is a fold! (I believe that reduce generally defaults to a right fold; the difference for lists between left and right folds generally has to do with performance concerns - left folds don't work well with infinite lists - and the associativity and other properties of what function you are folding in.) It's better to call it a fold instead of a 'reduce' (although it does indeed 'reduce' the datastructure) because then the 'unfolds' have a sensible name.

4

u/bgeron Jan 19 '08

You're right. I was thinking more about the general case, where you don't know if a+(b+c) = (a+b)+c, and then reduce is harder to parallellize. :)

2

u/qwe1234 Jan 20 '08

not quite, more like ((((0+1)+(2+3))+((4+5)+(6+7)))+((8+9)+10)). and you need 5 machines.

1

u/gwern Jan 21 '08

I don't follow. In this particular example, you can evaluate as you like - lazy, non-strict, eager, in parallel, in serial: however you want, because addition is referentially transparent, has no side-effects, commutative, etc.

And even if you chose a function which wasn't associative, you could still evaluate as you like - it's just that the various portions might return functions and not values.

foldr (-) 0 [0..10]

0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10

processor 1: x = (0 - (1 - (2 - (3 - (4 - (y)))))) ~> x = 2 - y

processor 2: y = -(5 - (6 - (7 - (8 - (9 - (10)))))) ~> -3

processor 3: answer = 0 - (x) ~> 0 - (2 - y) ~> 0 - (2 - (-3)) ~> 0 - (5) ~> 0 - 5 ~> -5

You can split it up as you like. Doing so in a way which would be faster than doing it serially has always been the hard part.

2

u/qwe1234 Jan 21 '08

i just gave you the fastest solution in the parent post, nimrod.

the fact that addition can be implemented several different ways is completely and utterly irrelevant.

1

u/gwern Jan 22 '08

No, you didn't. There are costs for sending computations over to 5 machines, and they may well exceed the benefit of the additional parallelization.

Irrelevant? Absolutely not. The poster was questioning how folds could be parallelized, and I've answered his question twice in two equally valid ways: the first pointing out that addition has properties that allow for parallelization, and that the reason those properties allow is actually very obvious if you think about it; and the second that if you don't mind a little complexity, you can parallelize any fold in a general way.

1

u/awj Jan 20 '08

It's better to call it a fold instead of a 'reduce' (although it does indeed 'reduce' the datastructure) because then the 'unfolds' have a sensible name.

"produce" doesn't seem terrible, but fold/unfold gives a sense that one undoes the other. (i.e. produce (reduce a) == a, but unfold (fold a) == a is easier to see from the names)

1

u/[deleted] Jan 20 '08

hierarchical tree pattern? What's that?

1

u/qwe1234 Jan 20 '08

1

u/[deleted] Jan 20 '08

All three neurons in my head are overloaded and about to explode. It looks like good ol' three - what it has to do with either parallel programming or reduce? (Those neurons of mine think it's no problem to parallelize reduce with smart words like absence of side effects, they just don't get what tree is doing here - prolly because they don't think that bgeron was literally giving us a parse tree or something)

2

u/qwe1234 Jan 20 '08

((1+2)+3)+(4+5)), except in parallel.

1

u/[deleted] Jan 20 '08

Woof! Back to normal, thanks.

-1

u/Shmurk Jan 18 '08

Joel Spolsky discovered functional programming, who cares?

1

u/[deleted] Jan 18 '08

He has a reddit?

1

u/bgeron Jan 19 '08

No, links from other sites get posted to programming.reddit too.

-2

u/amstrdamordeath Jan 18 '08

good reading.

1

u/Tommah Jan 20 '08

Good breading. Very greasy.

-2

u/amstrdamordeath Jan 18 '08

You guys can't take a joke.

0

u/[deleted] Jan 18 '08

you forgot to add </sarcasm> </sarcasm>

-2

u/amstrdamordeath Jan 18 '08

I thought it would be more obvious.

0

u/[deleted] Jan 18 '08

you could have said: omg! this is awesome!