4 ms·
This reminds me of the joke about OOP and monkeys[1] Really though, I think this is more related to culture that Java (and many Java books) tends to nurture. A
by kaylarose 16y ago
This reminds me of the joke about OOP and monkeys[1]
Really though, I think this is more related to culture that Java (and many Java books) tends to nurture. Anybody that's read a fair share of "Enterprise" Java code has seen FooBarFactoryFactoryFactoryImpl classes.
Hopefully as dynamic languages on the JVM become more popular, this will change. But I find many "Enterprise" developers aren't really interested enough to look outside the box.
"jRuby? I think I've heard of that, what's wrong with Java?"
"It's not a real programming language unless it's statically typed."
[1]http://www.codersatwork.com/ http://www.codersatwork.com/
- jcromartie 16y agoJust for fun, a fairly fast and low-memory factorial in Clojure: (defn factorial [n] (reduce * (range 1 (inc n)))) It's almost criminal how simple it is, yet it's pretty correct. It works for int, float, BigInteger, etc., treats negatives as 0, and throws on non-numeric types. (Edit: needed an inc in there)
- jacquesm 16y agoIf there ever was a powerful argument for expressiveness in languages I think you've just made it in a very hard to refute way.
- mahmud 16y agoThe funny thing is that you could also write REDUCE and RANGE, with one line each. In Scheme you could even write DEFN, again, one line. [Edit: Not RANGE you don't. Let's see how long it takes to get it under 5 lines]
- jacquesm 16y agoSounds like a code golf assignment in progress. Actually there might be an interesting contest, cross-language code golf.
- silentbicycle 16y agoIn K (kona): f:{*/1+!x} Which could be paraphrased as "product fold 1 plus [array of numbers 0 .. x-1]", or "(reduce #'* (1+ (upto x))".
- sofuture 16y agoI raise you a J: f=:! f=:*/1+i. NB. included implementation because it's not a built in, but is still funny
- silentbicycle 16y agoWell, sure, but it's a predefined factorial operator. I'm sure the J version from first principles would be similar though.
- jacquesm 16y agobuilt-ins are not the same as implementations.
- Peaker 16y agoIn Haskell: factorial n = product [1..n] Also polymorphic to all of those types and results in 1 for negatives. If you want the polymorphic "memoizing version": factorials = scanl (*) 1 [0..] factorial = (factorials !!)
- jcromartie 16y agoHaskell wins!
- eru 16y agoNo, see: http://www.willamette.edu/~fruehr/haskell/evolution.html http://www.willamette.edu/~fruehr/haskell/evolution.html aka "How not to write factorial in Haskell".
- hiena03 16y agoI don´t know Clojure, but it looks like it will give a wrong answer for 0
- jcromartie 16y agoNope. The result of (range 1 0) is an empty range, and when you apply * with no arguments you get 1.
- hiena03 16y agook, thanks, it looks to similar to haskell: factorial n=foldl1 (*) [1..n] that will throw an exception for n<1 because you will have an empty list
- chrisaycock 16y agoSince we're playing code golf, here's q/kdb+: factorial:{prd 1+til x}
- kanak 16y ago> It works for int, float, BigInteger Actually it is incorrect for floats. For non integer types, the factorial is given by the gamma function [1]. So, the factorial of 3.5 is Gamma(2.5) which is 3.3325 [2] not 6 as your function suggests. [1] http://en.wikipedia.org/wiki/Gamma_function http://en.wikipedia.org/wiki/Gamma_function [2] http://www.wolframalpha.com/input/?i=gamma%283.5%29 http://www.wolframalpha.com/input/?i=gamma%283.5%29
- sesqu 16y agoYes but no. For non-integers, the factorial isn't defined. Just because the gamma function is an analytic continuation (read: generalization) of the factorial doesn't mean you can go ahead and return anything other than an error code in those cases.
- jimbokun 16y ago"It's not a real programming language unless it's statically typed." Then see what excuses come forth when you suggest Haskell.
- mahmud 16y agoOcaml. Haskell is like that guitarist bum friend who lives on other people's couches but somehow ends up at the Grammys every year.