If you're on any programming language that is higher level than C, there is literally no reason that you would still write for loops.
If you're on any programming language that is higher level than C...
anime website
retard
no thx why would any one consider more complexity with zero benefit.
what is wrong with
for(var n& : {1 .. 6})
{
if(even(n))
cout
You're not wrong.
Ease up on the kool aid
>two loops instead of one
Oh boy, there is so much wrong with your example code.
>Arrays.asList(..).stream()
Just write Stream.of(...)
>foreach
It's forEach. Shameful.
>i -> ...
Just write System.out::println instead of that ugly lambda.
>>two loops instead of one
Do you even Java?
Now try doing that but stopping when the total sum of the numbers printed reaches 10.
System.out.print("2\n4\n6\n");
That's just a filter
No it's not.
List ints = Arrays.asList(1,2,3,4,5,6);
int sum = 0;
ints.stream()
.filter(i->i%2==0)
.filter(sum{System.out.println(i); sum+=i;});
multiple statement lambdas was a mistake
So that's just a more retarded way of writing a for loop.
No, that's a more composable way of writing one. The benefits come when you have to modify the code or reuse parts of it without repeating yourself so much you guarantee failure.
Of course this is just a stupid example so yeah, it's about the same in practice. The purpose of functional is only obvious when maintaining a real application over time to cater the needs of real businesses (read: cancer, but pays the bills)
whatever NERD hahaah
its performant if you are using a good language
Enjoy your compiler warnings and essentially undefined behaviour.
Java streams literally kill your performance. They are for convenience, not a replacement.
AtomicInteger tempSum = newAtomicInteger(0);
ints.stream()
.takeWhile(num -> tempSum.addAndGet(num) < 10)
.forEach(System.out::println);
That's the difference between a new language built around it and optimized vs one that has it hacked in.
Here's rust
(1..).filter(|x| x % 2 == 0)
.scan(0, |a, x| { *a = *a + x; Some(*a) })
.take_while(|x| *x < 10)
.for_each(|x| println!("{}", x))
>.filter(sum>=10)
Nice larp
because it's a fundamentally different way of thinking.
to write out things in an imperitive way introduces error handling complexity and managing branching complexity with the only real benefit is possibly soe more optimal code over whatever the compiler would write with the alternatives.
jesus christ why... what is the point.. so much harder to read
Sometimes you need the index. I usually use the C++ range loops.
>there is literally no reason that you would still write for loops.
I know you're being facetious for meme funnies, but there are still reasons to use for loops. For one thing, the Java streams system is total garbage, but generally you can take a reasonably large performance hit using lambdas and automatic iterators. If you're going over large collections and performing arbitrary mutations or copying then just using a for loop or foreach loop is more appropriate. If you need an index, it's also way more clear what's going on if you just use a for loop.
I say this, being a massive advocate for automatic iterators.
What font is this?
Trying to make this compile, I found out you aren't allowed to modify variables outside the scope of a lambda. This is new to me, but nice try though.
Menlo. It may be macos only.
Much appreciated
now try putting it inside an object
interator and stream are glorified for loops because the only alternative to loop is recursion, no exception.
its about abstraction retard. Of course its still looping.
>its about abstraction retard.
justify the abstraction then.
the only benefit that matters
new[] { 1, 2, 3, 4, 5, 6, }
.TakeWhile(nums => nums.Sum() Console.WriteLine(num));
C# master race
If you want the cumulative sum as the output list then it's just sumTo10 = takeWhile (< 10) . scanl (+) 0
if you want the original list as the output list then sumTo10' xs = zipWith const xs $ sumTo10 xs
>mutating a local variable as you go
literally what is the point?
when you get right down to it most of these people are about abstraction for abstraction's sake. The whole point is to seem clever instead of just writing something simple and straightforward.
Or even just sumTo10' = zipWith const sumTo10
Haskell is cute!
Exception handling is fucked in Java streams.