for only makes sense in languages where you have to manage memory yourself and/or you don't have easy access to true higher order functions.
Once you start passing functions as arguments, fold and its children make your life way more easy to deal with.
Take this contrived Haskell example:
isLetterA::Char→BoolisLetterAc|c=='A'=True|c=='a'=True|otherwise=FalsestripChars∷(Char→Bool)→String→StringstripCharscfn=filter(not.cFn)stripCharsC.isSpace"I made a string!"-- Should return "Imadeastring!"stripCharsisLetterA"I made a string!"-- Should return "I mde string!"
Ok, that's unreadable... let's convert it into its rough Javascript equivalent:
functionisSpace(c){returnc=='';}functionisLetterA(c){returnc=='a'||c=='A'}functionnegate(boolFn){return(v)=>!(boolFn(v));}functionstripChars(charFilterFn){return(s)=>s.filter(negate(charFilterFn));}stripChars(isLetterA)("I made a string!");// should return "I mde string!"stripChars(isSpace)("I made a string!");// should return "Imadestring!"
I know it looks like a lot, but it's a REALLY powerful concept that lets you make even MORE reusable things while not having to increase the complexity of the underlying building blocks. For small examples like this, it seems like a lot of overhead, but combined with a strong type system (and tail call optimization) you can more easily make trivially correct programs.
I wish I had the words to convey this better, but it's part of what makes functional and declarative styles so powerful and so difficult to get into after really learning the imperative style. Also, the fact that languages like c, Javascript and Python have dealt with recursion and corecursion so badly in the past hasn't helped us either. fold really relies on recursion as an underlying concept.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
for
only makes sense in languages where you have to manage memory yourself and/or you don't have easy access to true higher order functions.Once you start passing functions as arguments,
fold
and its children make your life way more easy to deal with.Take this contrived Haskell example:
Ok, that's unreadable... let's convert it into its rough Javascript equivalent:
I know it looks like a lot, but it's a REALLY powerful concept that lets you make even MORE reusable things while not having to increase the complexity of the underlying building blocks. For small examples like this, it seems like a lot of overhead, but combined with a strong type system (and tail call optimization) you can more easily make trivially correct programs.
I wish I had the words to convey this better, but it's part of what makes functional and declarative styles so powerful and so difficult to get into after really learning the imperative style. Also, the fact that languages like c, Javascript and Python have dealt with recursion and corecursion so badly in the past hasn't helped us either.
fold
really relies on recursion as an underlying concept.