3 ms·
Let's compare apples and apples, rather than apples and oranges, shall we? (reduce #(+ %1 (:productioncost %2)) 0 (filter #(> (:liquidvalue %) (* 2 (:pro
by snprbob86 17y ago
Let's compare apples and apples, rather than apples and oranges, shall we?
(reduce #(+ %1 (:productioncost %2)) 0
(filter #(> (:liquidvalue %) (* 2 (:productioncost %)))
(map #(assoc % :liquidvalue (* (:sdfactor %) (:productioncost %))) assets)))
vs.
(from asset in assets
let liquidValue = asset.SDFactor * asset.ProductionCost
where liquidValue > asset.ProductionCost * 2
select liquidValue).Sum()
I'd prefer that C# _3_ code any day of the week.
- markerdmann 17y agoYou seem to be intentionally taking that Clojure code out of context. The refactored of version of that code, which compares much more favorably with your C# 3.0 version, was given as: (reduce (sum-field :productioncost) 0 (filter doubleMargin? (map attach-liquid-value assets)))
- snprbob86 17y agoOn the contrary, I think that the C# code the author presented was unfairly IN CONTEXT. It showed using statements, a namespace, the program entry point, and other boilerplate. While I agree that C# has a non-trivial class boilerplate and non-zero function boilerplate, the Clojure you quoted doesn't even show any function boilerplate (defn foo [args] body) I compared a C# 3 expression to a Clojure expression. And that bit of Clojure that you quoted uses helper functions which are not defined. The C# version of that is: (from asset in assets let liquidValue = GetLiquidValue(asset) where DoubleMargin(asset, liquidValue) select liquidValue).Sum() Clojure is a great language, but this article unfairly bashes C#, which is also a great language.
- tumult 17y agoagreed. i think his C# code is intentionally obtuse or something. and the 'refactored' version of the Clojure code with the gnarly bits removed is missing the logic to actually perform the calculations. speaking of functional, i'm new to Haskell, but wouldn't it just be f x = sum [v | (y,z) <- x, let v = y*z, v >= z*2] assuming the argument is a list of tuples in the form (SDFactor, productionCost) you could also foldl if you don't trust the compiler to optimize the extra list iteration away. i didn't really like the article. the tone was really grating. probably look elsewhere if you want some inspiration on functional programming style
- cgrand-net 17y agoLinq is indeed nice. The Clojure code can be made easier to understand using a list comprehension: (reduce + (for [{:keys [productioncost sdfactor]} assets :let [liquidvalue (* productioncost sdfactor)] :when (> liquidvalue (* productioncost 2))] productioncost))