Which and why?

Which and why?

Other urls found in this thread:

ideone.com/ruqTcb
ideone.com/f2X8Yr
twitter.com/SFWRedditGifs

c++ because that's what the person who pays for my programming wants

omg that python and javascript is disgusting

>void main()

Why the fuck would you ever use a recursive array sum?

Which one compiles into a non recursive loop?

what's wrong with it?

>using namespace std;

why not? is it counter-effective?

Matlab
>sum([6 4 9 2 1])

sum is builtin in python, why would you redefine it?

.pop is a crime against humanity

at least the functional ones make sense even if they invent the universe first

numbers = [2,1,3,7]
print(sum(numbers))

Fixed the Python for you.

C/C++ first, because every other language follows some element or standard or convention that C/C++ has. I personally recommend C++.

Javascript next because next to Java it is the most used language. Definitely the most used scripting language.

Then Python to appreciate how easy it is compared to Javascript.

>why not? is it counter-effective?
Recursion creates a new closure at every level. The stack will grow proportional to the length of the array, for no good reason. .pop() will also mutate the original array every time, again for no reason. A simple loop does not have that issue.

>size = sizeof(numbers) / sizeof(numbers[0])
lmao what the fuck is that

Pleb

it calculates the size of the array by comparing how big it is to how big the first member is
lol, i dunno if it's the best way to do it in C

That is some very poor JavaScript

Here you go, OP:

const sum = arr => arr.reduce((total, i) => total + i, 0);

console.log(sum([1, 2, 3]));

Clojure/Scala master race

New and improved hask-lel
sum' :: [Int] -> Int
sum' x = foldl (+) 0 x

main = print(sum([2,1,3,7]))

fn main() {
let numbers = [2,1,3,7];
let sum: i8 = numbers.into_iter().sum();
println!("{}",sum);
}

thanks, user
this is ES6, right?

so, general rule would be
>don't use recursion if language is not enforcing you to do so

looks sorcery af

is it rust?

second this. weird as hell.

Try using square brackets, my lad.

The C one is missing a #pragma omp simd to use single-instruction-multiple-data instructions that the CPU can do at the hardware level and which other languages can't do at all, or a #pragma omp parallel for reducing(+:total), which would allow it to be multithreaded, and other languages can't do it in parallel without double the lines of code (if they support it at all)

I've been looking everywhere but still can't figure out how to do it properly...

Like this?

it may be weird, but still it is possible
but what is any other option to recursively sum array and not use "pop"?

fn main() {
let numbers = [2,1,3,7];
let sum: i8 = numbers.into_iter().sum();
println!("{}",sum);
}

excellent

Yup

why sum has to be defined with type and array not?

>but what is any other option to recursively sum array and not use "pop"?
Counter to store array position

That's not recursive though.

Haskell > Scala > C > Clojure > C++ > Python > Javascript

>Scala better than C

I guess you're right
Haskell > C > Scala > Clojure > C++ > Python > JS

>Python worse than Clojure

>so, general rule would be
>>don't use recursion if language is not enforcing you to do so
Recursion is generally slower and uses more memory than a loop. The only times you should use it are when the problem is inherently recursive to think about and you know it's not going to cause a stack overflow. Examples would be tree traversal, flood fill algorithm, etc.

Very simple, you're just adding things dude.
function sum(arr){
let sum = 0
for(let i=0; i

The sum type allows rust to infer that numbers is [i8]. Why sum has to be defined regardless.. dunno.

#include
#include
#include

int main()
{
std::vector v { 2, 1, 3, 7 };
std::cout

Fixed the js
function sum(arr){
return arr.reduce((s,n)=>s+n, 0)
}

This You can even hand down the array as an argument in every recursive call, it will not be copied in memory because only a reference to it is passed

Unfortunately we have to disagree on that one

>Very simple, you're just adding things dude.
I meant, how to write sum function recursively but without using pop, for and adding numbers to counter is easy, but I look for functional solution

>so, general rule would be
>>don't use recursion if language is not enforcing you to do so
Yes, although modern compilers (if your language can be compiled) should be able to undo the recursion... which is not the case of Python and Javascript

Absolutely disgusting JavaScript.

This is not a recursive problem so you should not ever do that for any reason, but this

function sum(arr){
if (!arr) {return 0}
if (arr.length == 1) {return arr[0]}
return recurse(0)

function recurse(pos){
if (a.length == pos+1){
return arr[pos]
}
return arr[pos] + recurse(arr, pos+1)
}
}

actually this
function sum(arr){
if (!arr || arr.length == 0) {return 0}
if (arr.length == 1) {return arr[0]}
return recurse(0)

function recurse(pos){
if (a.length == pos+1){
return arr[pos]
}
return arr[pos] + recurse(pos+1)
}
}

I know, but it looked like that guy didn't even understand a loop. This is the real way

C, most elegant,simple and non-autistic.

I'd say even C++ if you write it in a C-style, or at least while not using the features that make your code look like a toilet filled with turds

C++ version:
1) fails easily with large vectors.
ideone.com/ruqTcb
150 megabytes for summing 690 ints lmaoing my ass
2) one condition is extra
3) std::accumulate
ideone.com/f2X8Yr

won't hire this idiot/10

>let's write the solution in a completely retarded way in a language I don't like to make said language look bad
Utter fucking retard. Kys.

hire me please

Haskell, but i would never use it for something... REAL. It's beautiful and powerful language, but developing something in it is lottahellapainintheass.

For practical reasons, I preffer python. And missing Go, for shit and giggles.

C, the rest is bullshit, a for loop is always faster.

R

numbers = c(2,1,3,7)
sum(numbers)

s/won't/wouldn't
Actually I'd like someone to hire me as well.

Ruby ways:
# Sum method
[2, 1, 3, 7].sum
#=> 13

# Inject and Reduce are the aliases
# symbol inject
[2, 1, 3, 7].inject :+
#=> 13

# block syntax
[2, 1, 3, 7].reduce { |n, m| n + m }
#=> 13

What indian made that C++ program?

#include
#include
#include

using namespace std;

int main() {
std::vector vals = { 2,1,3,7 };
printf("%d", std::accumulate(vals.begin(), vals.end(), 0));
return 0;
}

>cstdio
>printf
>in C++14+
lmao

iostream is pig disgusting.

>using namespace std
YOUR FIRED!!!!!!!!!!!!!111111111!

ideone added that automatically. I didn't even use it.

Then again, there's not that much wrong with using it in source files.

>summing integers
boring

not an argument

...

Noob question but is C the same as C# and that's why it's not listed on OP picture or how does that work?

>
>so, general rule would be
>>don't use recursion if language is not enforcing you to do so

Thats good advise if you don't know what the compiler will do with it, otherwise recursion can be both more efficient and readable than a loop (that is not the case in OP).

Pretty ugly code. Every example is pretty bad.

Literally on the sticky lol

haskell is annorexic!

C# is very different from C. It's not in OP pic because whoever made the picture didn't think it was relevant enough to include it in the comparison.

t. pajeet - junior java """"""enginer""""""

Projecting much?

No, it's not projecting.
>Every example is pretty bad.
how Clojure or Haskell common solution can be pretty bad? I understand if recursion is too much or just weird, but any other code is just ok.
You sounds like Java developer hating anything that is not this pile of shit.

Well whoever made that picture triggered my OCD regardless how objectively relative C# is then.

I shouldn't have said that every single one of them is bad. See and

C, smaller compiled size, more cross compilation support

21:37 xd

>xd

...

Brainlets on suicide watch

way to go using abstract patterns like folding. much nicer then ops version, unless you have no idea of functional programming. However i like eta reduction, and i don't like parantheses, moreover sum would basically already be defined in Prelude but for the sake of comparison i'll write my own version

```
sum' = foldl (+) 0
main = print $ sum' [2, 1, 3, 7]
```

>recursion
>pop
>back
Use the standard library you tard.
std::sum(numbers.begin(), numbers.end());

see

Haskell should probably be a point-free foldl.

Scala is nice because it has a proper accumulator to avoid the tail call recursion causing stack overflows.

I don't have any experience with Clojure, but in Common Lisp + is polyadic so a sum function is completely redundant.

That C++ code is a sin. Changing vector size and making a copy of it times N. Just do it the c way in c++ or use std::accumulate

C++ version is quite slow and prone to overflow.

Here you go brainlets
import re

def sum(arr):
return eval(re.sub(r"\,",r"+",str(arr)))

numbers = [2,1,3,7]
print sum(numbers)


You can thank me later

wow, this is art.
I'd love to suck your dick