[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology

Name
Options
Comment
Verification
4chan Pass users can bypass this verification. [Learn More] [Login]
File
  • Please read the Rules and FAQ before posting.
  • You may highlight syntax and preserve whitespace by using [code] tags.

08/21/20New boards added: /vrpg/, /vmg/, /vst/ and /vm/
05/04/17New trial board added: /bant/ - International/Random
10/04/16New board for 4chan Pass users: /vip/ - Very Important Posts
[Hide] [Show All]


Janitor application acceptance emails are being sent out. Please remember to check your spam box!


[Advertise on 4chan]


File: fuckingwat.jpg (24 KB, 371x297)
24 KB
24 KB JPG
So apparently this is more "Readable™", "Modern™" and "Elegant™" than loops.
Honest thoughts?
I'm learning and I like my old man loops (that all those methods use under the hood)
The immutable argument is invalid since you can write code that doesn't modify arrays without these methods
>>
implicit iterators have always been cancer
>>
use loops then
>>
File: squisher.jpg (21 KB, 500x500)
21 KB
21 KB JPG
>>107115634
>steamroll
>>
trannyroll
>>
>>107115634
>loops (that all those methods use under the hood)

The comment explicitly says it is recursive though. Can the compiler really transform this into a loop?
And if it can't, I guess this function will simply crash if there are million elements.
>>
>>107115634
>So apparently this is more "Readable™", "Modern™" and "Elegant™" than loops.
it isn't, it's made up by functional programming schizos that got filtered by for loops
if you really want to deal with arrays without loops then something like C#'s LINQ is probably a reasonable idea. but you shouldn't use that either because it uses loops internally and your performance will tank if you use it everywhere on large arrays.
>>
File: 1753052951880973.gif (2.55 MB, 498x428)
2.55 MB
2.55 MB GIF
>>107115634
function flatten(array) =
function f(list, [array]) = list.push(flatten(array))
| f(list, element) = list.push(element)
reduce(f, array)

I made it less retarded
>>
>>107115634
I fucking hate nuJavaScript
>>
>>107115634
You know that js has a Array.flat() method?
>Honest thoughts?
The functional style is nice when there's just one straightforward thing you wanna do (simple map, simple filter, ...). For anything that requires more than just one line, loops are generally more ergonomic
>>
>>107115634
what's the use case
>>
>>107115634
>map and filter = good
>reduce = bad
Simple as.
>>
It's hard for me to come to conclusion but I think I might agree that it's better practice, regardless of if it's readable or whatever.
Specifically because it forces a pattern of simple single functions. The composition might be big or verbose, but the use is small, and the definitions are also small.
What I mean is, your focus becomes writing functions that process a single item, in isolation. And then simply compose a collection and sequence of these however you need for whatever you need, like a single item or applied to elements in a collection.

It's not always a pure win though, sometimes it's easier and/or faster to just range over a list and do everything you need to do in one shot. But the downside of this is mixing responsibility, concepts, etc.

Not really sure yet, but again I'm leaning more towards functional concepts and simplicity of the code as I continue my own practice. For a variety of reasons that mostly center around reducing code + writing reusable code.
>>
>>107115918
no you made it more retarded
>he can't code flatten
>>
>>107116236
It's basically right don't worry about it
>>
File: read-nigga.jpg (59 KB, 800x723)
59 KB
59 KB JPG
if only there was an Array.flat method
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
>>
=> {
, []);
statements dreamed up by the deranged
>>
yes
>>
>>107115634
using anything other than filter and map is cancer.
>>
>>107116724
explain how and why
>>
>>107115634
I fucking hate these javascript meme loop patterns
>>
>>107116736
they're not from javascript
>>
>>107116747
the op code is literally js, and jsfags are the ones using them all the time
>>
>>107115634
const flattened = arr.flat(arrr);
>>
>>107115634
Javascript was originally intended to be a functional style (lisp style) language.

The C-style was bolted on after the fact.

This is why there's all of the functional constructs shoehorned in.
>>
>>107116852
console.log(arr2.flat(Infinity))
>>
>>107115634
You people will never understand coding. Ever.
>>
>>107115634
grim
arr.join()
>>
>>107116688
alien runes
>>
>>107116868
tell me class how to implement reduce without a loop
>>
wait what, what a wild opinion to have

i love list comprehensions for clarity and use them all the time, in some languages like JS you do get a perf boost from using loops, and i will do that when necessary (real languages like haskell don't have this problem)

but holy shit it's so much more natural for me to express things using comprehensions, the nature of programming is that you build these higher level constructs to make expressing things easier, and the advantage of encapsulating computation in these regular primitives is that it makes metaprogramming easier

i'm not like anti loop, i write plenty of C and i love doing it, just surprised to see this opinion i've never had anyone complain to me about list comprehensions, it's always people complaining about the lack thereof

>>107115961
bro folds is like, the moment higher order programming gets good wtf
>>
>>107116899
how is this relevant, tell me how to implement loops without GOTO (JMP)

higher order constructs are useful, you use them all the time, thinking that there's one "good" level and above and below is worse is exactly as stupid as saying that you should just use assembly because it's all assembly under the hood anyway

you should use the right tool for the job
>>
>>107116899
tail recursion lmao are you retarded?
>>
Truly demonic
>>
>>107116688
beautiful
>>
>>107116899
real men use goto and labels for this.
>>107116868
>coding
ngmi.
>>
>>107116889
you could instead write it out as english words, and quickly realize why specialized notation is useful
>>
>>107115865
That is literally the linq's aggregate method
>>
File: steamroll.png (54 KB, 570x555)
54 KB
54 KB PNG
>>107115634
functional Javascript is ugly because the syntax was made for Java-style OOP, and so were most of the built-in object methods.
Picrelated is what that algorithm looks like when you use a syntax more amenable to FP.
Mind you, the algo is vulnerable to stack-overflow for deeply nested functions.

This is how flatten is implemented in Clojure:
(defn flatten
"Takes any nested combination of sequential things (lists, vectors,
etc.) and returns their contents as a single, flat lazy sequence.
(flatten nil) returns an empty sequence."
[x]
(filter (complement sequential?)
(rest (tree-seq sequential? seq x))))


>>107116477
you didn't get the point of the code in OP
>>
File: GS9YUEOWUAAPc61.jpg (97 KB, 1000x1000)
97 KB
97 KB JPG
JS was originally made as a functional language.

The C-style was added near release.

This is why it's as convoluted as it is.

Enjoy faggoteers.
>>
>>107117315
Oh god, they were planning on making it even worse?
>>
>>107117206
>>107117315
Functional programming is a mental illness.
>>
>>107117382
that's rich coming from a barneyfag
>>
>>107115865
>made up by functional programming schizos that got filtered by for loops
>something like C#'s LINQ

And what the fuck do you think LINQ is retard?
>>
>>107116942
Except for simple things like calculating the sum of a number array, reduces always look polluted and not very clear compared to loops.
I see no reason whatsoever to use them instead of a for loop.
>>
>>107115634
javascript's syntax is not very good at functional programming. i'm not sure why people push it there.
type int_or_list = 
| Int of int
| Lst of int list

let steamrollList lst =
List.concat (List.map (function
| Int x -> [x]
| Lst xs -> xs
) lst)


>That only works on a list of integers
Fine, here's an erlang one
flatten([]) -> [];
flatten([H|T]) ->
case H of
L when is_list(L) -> L ++ flatten(T)
X -> [X | flatten(T)];
end.
>>
>>107117795
fun sumList = foldl (fn (i, acc) => acc + i) 0

What's unclear or polluted about this to you that's not just syntactic unfamiliarity?
>>
>>107117315
damn functionalfags ruin everything eh
>>
>>107117997
>>Except for simple things like calculating the sum of a number array
Read, anon. Read.
Even then, a for loop is more readable.
>syntactic unfamiliarity
I can tell that's a reusable function because of the fun keyword, but I don't understand why the list being parsed is not named.
>>
>>107117795
having to write a loop in the first place is often a form of pollution
>>
>>107118337
>having to write a loop in the first place is often a form of pollution
Words conjured by the utterly insane.
>>
>>107118337
>>107118358
You both don't know how to use English words.
You lost. It's over.
>>
>>107115634
That's a very overcomplicated way of using reduce, that's for sure. If you want to use proper functional programming try Haskell.
>>
>>107118367
Ok jeet.
>>
>>107118358
size_t n = sizeof(a)/sizeof(*a);
int s = 0;
for(size_t i = 0; i < n; ++i)
s += a[i];


vs

+/a
>>
>>107118600
>bad faith example
Now see:
let total = 0;
for (const n of numbers)
total += n;


>vs +/a
Is that some sort of built-in sum function? Not impressive.
>>
>>107118681
https://www.aplwiki.com/wiki/Reduce
>>
>>107115634
this is poor use of reduce, because acc.push does not return anything, hence you have to explicitly return it
reduce is only useful when your lambda can naturally return what it needs to, which is the case in functional, immutable languages, where all modifications to data structures are explicitly return rather than mutated in place
this is why I do not use `fold` often in Rust.
>>
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

Fucking retard.
>>
Bro, just use .flat()
>>
def steamroll_list(list) do
for e <- list do
if is_list(e) do
steamroll_list(e)
else
e
end
end
end

this is proper FP with a "for comprehension" (significantly different from for loops in normal languages)
>>
>>107115634
>steamrollArray
Sovl
>>
>>107116726
Reduce is too hard for smoothbrains
>>
>>107116840
I see them in every language nowadays, it's the influence of functional programming

Hating functional programming is jeetcoded btw
>>
>>107117206
I love clojure but fuck me it's a slow piece of shit. Python tier. Even Racket is like 5 times faster.
>>
>>107118730
this defaults to a single level of recursion, which is not correct
if you provide a depth, you cannot know the depth in advance, hence you must write a hacky solution with some large number or whatever
>>
>>107115634
.map() is the only higher order function I like using, everything is trash.
>>
>>107118386
Point proven.
>>
What exactly am I looking at here.
>>
>>107118807
chaining, zipping, chunking, etc. are all very useful
you can also trivially incorporate stepped sequences (counter), infinite streams of function calls with side effects or of certain values, etc.
>>
>>107115961
>>107116724
>>107118807
What eceleb you guys download your opinions from?
>>
>>107118703
You could use acc.concat([el]) or something to make it more functional

It's like TypeScript, nothing is truly enforced, it's just a suggestion, and no matter how diligent you are someone will throw some slop your way
>>
>>107118371
Some JS frameworks leverage immutability, so it's good to know and use in real JS applications
>>
>>107118795
>if you provide a depth, you cannot know the depth in advance, hence you must write a hacky solution with some large number or whatever
You can literally just do arr.flat(Infinity)

JS is only hard if you make it hard
>>
>>107118855
Example?

>>107118826
>projecting
>>
>>107118823
You're looking at a stack overflow.
>>
The real benefit of functional programming is not to have global state everywhere, but noobs think it's the fancy syntactic sugar.
Just have pure functions, use your loops there if you want, and your code is close to functional.
>>
>>107115634
function flatten(a) {
return a instanceof Array ? [].concat.apply([], a.map(flatten)) : a;
}



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.