This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

please explain this function  RSS feed

 
Bartender
Posts: 1810
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really beginning to hate fat arrow functions. Can someone re-write this so the inner function is a regular, private function called from the outer function? No anonymous inner stuff, just split this into two functions so maybe I can begin to understand how it works.

Thank you.



After this I've got one with nested functions. That will be fun. This stuff is giving me a headache.  
 
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin, nice to see you back.

I don't know the syntax of this language, but the logic what it does is pretty straight forward, right. What is the stuff you don't understand?

If that were Java, the more habitual code construct would look like:

The function you showed, is actually very similar what Java also has with streams, predicates... The find() method simply acts as a stream where you filter result by passing a predicate as a lambda function. i.e.:
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just googled a bit how TypeScript

Modern:

Habitual (I might made some syntax mistakes) :
 
J. Kevin Robbins
Bartender
Posts: 1810
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, good to be back.

It helps to see that in a format that I'm familiar with. One thing I found confusing is the "(s)". I thought the item or items in parenthesis just before the => were parameters to the inner function. But in this case is doesn't look like a parameter but rather a variable that will receive the value of the first return. But that doesn't make sense because the "const server" is where the final result ends up. If it's a parameter, what's it's value? Confusing.

As for streams, I actually haven't done much Java, almost no version 8, in the last couple of years. Some Java 5 (yes, really) lots of SQL, some PHP (ugh), some AWS cloud stuff (that was fun) and then I got tossed into the deep end of the Angular pool where I seem to do more sinking that swimming. Routing has me pulling out what little hair I have left. I know lambdas will sink in eventually but it's still frustrating at this point. I find them very difficult to read.

I miss JSP's and jQuery. That I understood. But that's obsolete tech now. I've been job hunting since November and saw very, very few jobs looking for JSP. I start my new job on Monday as an Angular developer so I've been bingeing on it for over a month. All those tutorials are exhausting.
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

J. Kevin Robbins wrote:One thing I found confusing is the "(s)". I thought the item or items in parenthesis just before the => were parameters to the inner function. But in this case is doesn't look like a parameter but rather a variable that will receive the value of the first return. But that doesn't make sense because the "const server" is where the final result ends up. If it's a parameter, what's it's value?


Well, that (s) is a variable of iterable item. It could have been called "kevin" or "server" or something else, it is just a variable. Functional programming guys for some reason all the time choose to pick non-descriptive and cryptic names, not sure why, i.e.: 'e' stands for kind of 'element', but since 'e' became a non-written standard, in most tutorials probably you'd see 'e' regardless what it represents.


J. Kevin Robbins wrote:As for streams, I actually haven't done much Java, almost no version 8


Yeah, I really recommend to assign some time for that, because those concepts you'd see all over the place, whether it be Kotlin or Scala or JavaScript or other languages. In some languages they were since beginning, and for instance Lisp or Clojure or Haskell guys probably loops would scare off.
 
J. Kevin Robbins
Bartender
Posts: 1810
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the light bulb just came on. When you said iterate it clicked.

this.servers is a collection of JavaScript objects of server information. Sort of like an List of Objects in Java terms. "Find" iterates through that list (like a for loop) and (s) holds a single server record as Find iterates through the collection, hence it can be used for comparison in the equality test in the first return statement. I couldn't see where (s) was getting initialized with a value because I didn't equate find to a for loop.

It all makes sense now. I don't know why I didn't see it. Thank you!
 
Crusading Chameleon likes the size of this ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!