Swift 3 Fun Algorithms: Filter, Map, Reduce Higher Order Functions

  Рет қаралды 32,605

Lets Build That App

Lets Build That App

7 жыл бұрын

Let's talk about Higher Order functions today, namely Filter, Map, and Reduce. In today's lesson, I'll go over some very simple and basic examples of these three functions and how you would go about using them.
iOS Training Course:
www.letsbuildthatapp.com/basi...
Completed Source Code:
www.letsbuildthatapp.com/cour...
Follow me on Twitter: / buildthatapp

Пікірлер: 103
@adnanasghar007
@adnanasghar007 7 жыл бұрын
I always missed a senior developer at my job. Now I have one called Brian.
@andy_skerjie
@andy_skerjie 7 жыл бұрын
Hi Brian, I'm from Belarus, really like your lessons, saw the implementation of these methods used by other teacher. These methods can be written even shorter let sum = [1,2,3,4].reduce(0, +) let filtered = numbers.filter{$0 % 2 == 0} and let transformed = [1,2,3,4].map{ $0 * 2} Good luck and sorry for bad english
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
+Андрей Полонский great stuff, sometimes when the syntax is too simple it gets hard to understand, but having multiple options is definitely welcomed.
@StoreyofGee
@StoreyofGee 7 жыл бұрын
Love your content, more stuff like this is greatly appreciated.
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
+Geemakun Storey awesome I'll try to make one of these every Friday.
@AsangaRamanayake
@AsangaRamanayake 4 жыл бұрын
Awesome explanation!!!
@arturkazais944
@arturkazais944 7 жыл бұрын
Thanks for the quality videos!
@owenmoore3782
@owenmoore3782 7 жыл бұрын
Loving these videos, look forward to these after a long day at college 👍🏽
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
+Owen Moore good old college courses, I think videos created by good teachers will eventually replace traditional school classes.
@jamesh4129
@jamesh4129 7 жыл бұрын
Really great video. This is how you take yourself to the next level
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
Agreed, algorithms are fun and help you understand language syntax a lot.
@feliciavera3525
@feliciavera3525 7 жыл бұрын
good. first time hearing this. would like to see more videos like algorithms.
@michaelvoline6205
@michaelvoline6205 7 жыл бұрын
Great job man! Would be cool to see more advanced and complicated algorithms though:)
@richardgmartinonline
@richardgmartinonline 7 жыл бұрын
Hi Brian: Great video and explanation of these concepts. Thank you. A small request: could you do a similar video explaining the mechanics of closures, functions and completion handlers (in Swift)?
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
+Richard Martin completion handlers are very interesting and can be a little confusing due to its asynchronous nature. More on this stuff in the future.
@RohanLokeshSharma
@RohanLokeshSharma 7 жыл бұрын
great video as always. Would love to see a tutorial on avoiding memory leaks in closures.
@israman30
@israman30 7 жыл бұрын
Great video. Very helpful. Swift greeting from east coast.
@joycelu962
@joycelu962 7 жыл бұрын
新年快乐!!Thank you again for a great tutorial!
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
+Joyce Lu 新年快樂 恭喜發財 hope everyone has a good and prosperous year ahead
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
+Bruce Berry that's great, wish you much success in the future.
@kryptoshi4706
@kryptoshi4706 7 жыл бұрын
Thanks for a great informative video Brain you are the best! I would love to see more algorithm videos, on graphs and trees. Maybe an introductory video on RxSwift?
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
We have a recursion video already queued up for this Friday, hope you enjoy that one.
@wilmercb
@wilmercb 3 жыл бұрын
Great videos! ...this would be what's call know on Swift UI closures? ....Thanks
@dragosstrugar3097
@dragosstrugar3097 7 жыл бұрын
Hi! I really love your videos. And I love the no-storyboard principe. Can you make a beginner video for no-storyboard best practises?
@thebeatles9
@thebeatles9 7 жыл бұрын
so COOLLL ... next, do a video on FP principles :) it really makes imperative code better... even OOP code better!!
@In0pon
@In0pon 7 жыл бұрын
Birthday present is a LBTA video :D good one as always
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
A good solid 10 minutes of a swift is my birthday present.
@chejarlavenkatesh6476
@chejarlavenkatesh6476 3 жыл бұрын
3:40 filtered is a 'let' constant, so it can't be mutated, if you make it a 'var' you can append new values.. But great tutorial as always. Thank you Brian.
@kenny_speaks
@kenny_speaks 7 жыл бұрын
!! thanks. Earlier this week I had hard a really hard time making a view controller talk to another view controller, understanding the navigation stack present/dismiss, and how to implement a protocol + delegate. I have a decent working knowledge now, but anything you could highlight about those topics would be neat :) /source: beginner struggles
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
+Drew Wiskus yeah you can watch my AppStore series on how to send info from one controller to the next. Good luck.
@doktoren99
@doktoren99 7 жыл бұрын
This series is great bro. Can i ask how much time you spend preparing for this video for example? It seems very fluent and perfectly executed with no errors :)
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
Usually takes 4 hours to make each of these videos which includes preparation, recording, editing.
@harshitgupta2438
@harshitgupta2438 5 жыл бұрын
Nice video Sir
@iamjia7467
@iamjia7467 7 жыл бұрын
Nice video, thank you. Can you make a video for the "Where" in Swift3, saw that in swift 2, but don't know how to do it in swift 3
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
This is swift 3....
@HolaSoyPedro2
@HolaSoyPedro2 6 жыл бұрын
reduce form i understand like this: array = [1,2,3,4] , var sum = array.reduce(0,{(value1:Int, value2:Int) ->Int in value1 + value2})
@geomichelon
@geomichelon 7 жыл бұрын
its possible a video tutorial about implement a viper design pattern? Thanks
@developerios6096
@developerios6096 7 жыл бұрын
These functions can be hard to understand, but powerful if you get how they work
@thongtran1653
@thongtran1653 7 жыл бұрын
Hey Brian: How do we use the data which display in Singly Linked List to Uitable view Cell
@HenrikHansenMasterOfManyThings
@HenrikHansenMasterOfManyThings 5 жыл бұрын
Closure expression syntax explained: docs.swift.org/swift-book/LanguageGuide/Closures.html
@arunjack5604
@arunjack5604 4 жыл бұрын
Can you tell me how to do dynamic reduce() method for all type array
@1Watchmerock
@1Watchmerock 7 жыл бұрын
Great video. btw now in Swift 3, we can use the reduce function like sum = [ 1, 2, 3, 4].reduce(0, +) sweet right!?
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
Great stuff Ronald, the shorter syntax hit me after a few people mentioned it in the comments.
@rikyriky966
@rikyriky966 5 жыл бұрын
Is there a Lodash kind of library?
@HaiyanMa
@HaiyanMa 7 жыл бұрын
新年快乐,😆💫mentor!
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
新年快乐徒弟
@fankamthong529
@fankamthong529 7 жыл бұрын
新年快乐 Brian
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
祝你新年快乐 Fan Kam.
@user-yg9yp2ok1q
@user-yg9yp2ok1q 7 жыл бұрын
i want to learn how i can drag and drop image in swift 3 xcode 8? can you help me,please?
@ximenadelatijera
@ximenadelatijera 5 жыл бұрын
Do you have any video where you show how to filter in Firebase?
@LetsBuildThatApp
@LetsBuildThatApp 5 жыл бұрын
Yes there are some lessons here: www.letsbuildthatapp.com/course_video?id=4392
@adriancervantes1898
@adriancervantes1898 6 жыл бұрын
Hi Brian and everyone! I saw there many tips to do the same trick in reduce function, writing with some differences but get the same result... just to be curious, exist any kind of difference between on the next?, the result is the same but writed with some diferences inside on the function.... but the compiler say, 4 times in one, and 5 times on the other, obtaining the same result, 10 in this case... let sum = [1,2,3,4].reduce(0) { (sum, number) in sum + number } sum. result is 10. and the compiler say -- ( 4 times) let otherSum = [1,2,3,4].reduce(0, { sum, number in sum + number }) otherSum. result is 10. and the compiler say ( 5 times). what is the difference? and why say 4 times in the first code and on the other say 5 times? maybe doesn't matter, I just curious thanks Brian! and thanks to all those who comment helping each other!
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
You can put print statements and breakpoints in a regular single view app to see exactly whats happening
@adriancervantes1898
@adriancervantes1898 6 жыл бұрын
Lets Build That App, thanks for the answer and take the time for it, I will check that, cheers!
@MohammedEnnabah
@MohammedEnnabah 6 жыл бұрын
A shorthand for the reduce function is: let sum = [1,2,3,4].reduce(0, {$0 + $1}) //prints 10 A shorthand for the shorthand for the the reduce function let sum = [1,2,3,4].reduce(0, +) //prints 10
@hotmandead1
@hotmandead1 6 жыл бұрын
Further reduction to the sum (Shorthand argument names) let sum = [1,2,3,4,5].reduce(0, {$0+$1})
@sophiabnrm8730
@sophiabnrm8730 6 жыл бұрын
you can even write it shorter as: let sum = [1,2,3,4,5].reduce(0, +)
@hotmandead1
@hotmandead1 6 жыл бұрын
Agreed
@hanisster
@hanisster 7 жыл бұрын
can we use the map function with optional ! array objects? those that can be nil..
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
I don't think you can use these functions on optional arrays, give it a try and see what happens.
@DC4TH
@DC4TH 6 жыл бұрын
You missed FlatMap. Btw, great video!
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Flatmap... I only barely understand what it does to this day.
@limbomimbo8987
@limbomimbo8987 7 жыл бұрын
One doubt, when you used map , the number of times increased to 5 where as the old school way shows 4 times .. is it running more number of times ??
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
+Limbo Mimbo I wouldn't worry about what playgrounds is reporting, there are other subtle efficiencies that come with using these functions that I haven't mentioned in the video. Hope this piqued your interest.
@Abdi-uy1kh
@Abdi-uy1kh 7 жыл бұрын
I understand the purpose of core data, animation, UIkit, collection and table views and other stuff but what is swift algorithms used for?
@cprovatas
@cprovatas 7 жыл бұрын
interview questions and it is also helpful in easily organizing large collections of data that you may be getting back from an api
@Abdi-uy1kh
@Abdi-uy1kh 7 жыл бұрын
I don't need to use it right?
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
+ABDIKADIR ABDALLA well, how do you suppose code data and animations are implemented? The magic is within algorithms.
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
+ABDIKADIR ABDALLA well, how do you suppose code data and animations are implemented? The magic is within algorithms.
@Abdi-uy1kh
@Abdi-uy1kh 7 жыл бұрын
I have never seen any online courses on swift algorithms so I thought they were never important. I don't know anything about algorithms.
@pariveshsharma
@pariveshsharma 7 жыл бұрын
maybe some sorting algorithms?
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
I think sorting is probably my least favorite kind of algorithm to go through.
@TheSnifferdog2
@TheSnifferdog2 7 жыл бұрын
How are map and filter different? What would happen if I type [1,2,3,4].filter({return $0 * 2})?
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
You can give it a try in playgrounds and see what happens.
@TheSnifferdog2
@TheSnifferdog2 7 жыл бұрын
I think map will change the values and filter will only select values. So [1,2,3,4].filter({return $0 * 2}) gives an error.
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
+Snifferdog correct
@rogerwprice
@rogerwprice 7 жыл бұрын
Nice! ....and flatmap?
@mykhailoherasimov2753
@mykhailoherasimov2753 7 жыл бұрын
flatmap is the same as map, but you can return nil values: For example: let array = [1,2,1,2,1,2]; array.flatmap { $0 % 2 == 0 ? $0 : nil } - this statement will return [2,2,2]. Or you can use flat map for iteration over a few levels array like this: let arrayOfArrays = [[1,1,1,1],[2,2,2]]; arrayOfArrays.flatMap { $0 } - will return [1, 1, 1, 1, 2, 2, 2]
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
+Roger Price thanks roger, I have rarely used flatmap but see that it's useful.
@rogerwprice
@rogerwprice 7 жыл бұрын
nice - thanks - yes this seems popular with the Functional Programming folks...
@siddhantnigam4627
@siddhantnigam4627 6 жыл бұрын
awsme :)
@khaldrogo9451
@khaldrogo9451 7 жыл бұрын
at 6:49, the array [1, 2, 3, 4] has 4 elements, but the map function runs 5 times, why is that?
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
+Khal Drogo to be honest I'm not sure why it runs five times either, perhaps there is an initial run that sets up the array
@qqq2084ee
@qqq2084ee 7 жыл бұрын
do you functional programming, Brian?
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
Hello Yera, I did a lot of Scheme programming in college but ever since then I haven't touched functional programming for anything serious.
@qqq2084ee
@qqq2084ee 7 жыл бұрын
got it, thanks) Just now I`m a learning functional programming in swift and other advanced topics)
@JustinBurkard
@JustinBurkard 7 жыл бұрын
Why does $0 equal a value in the array? Is that just a universal Swift thing?
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
You can do a google search on Higher Order functions in Swift and the docs will give you a ton of very good examples.
@TheBooban
@TheBooban 7 жыл бұрын
3:36 That was confusing. Call inside of higher function call you can't append? Its only because you used Let which is immutable. If you used var it would be fine. Also didn't understand what you meant by "state" regarding an array. Also the () is not necessary in ({ }). {} is enough. Weird that both is allowed? And just found that you don't even need return. So var filtered = numbers.filter{ $0 == 3 } works. Its annoying that different syntax is allowed.
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
Not being able to append is the point of using one of these functions, I probably didn't explain it all that well.
@TheBooban
@TheBooban 7 жыл бұрын
Could you explain it here? I think you mean it is already appended in the variable "filtered".
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
Essentially, the idea is many times when you perform a filter on an array, you don't want to have it mutable afterwards. With a for loop, you're often left with some array that can be modified later on which is usually not ideal.
@TheBooban
@TheBooban 7 жыл бұрын
Ah, excellent, now I get it. In a regular loop it must be a var, while here, you get a choice. Thanks.
@arturmavlyuchenko7647
@arturmavlyuchenko7647 6 жыл бұрын
TheBooban it is the one of the basics of functional programming)
@ankit_gupta_23_Sept
@ankit_gupta_23_Sept 4 жыл бұрын
Sir can you just explain the difference between get,post and put differences in detail?
@BobstyleLiving
@BobstyleLiving 3 жыл бұрын
I've been using their own syntax since then because of readability, now just f**k I will use $0 and $1 haha
@MuhammadAli-zv5vz
@MuhammadAli-zv5vz 7 жыл бұрын
why this is excute 5 times but with for loop excute 4 times???
@LetsBuildThatApp
@LetsBuildThatApp 7 жыл бұрын
The playgrounds executes an extra iteration, I'm not sure why. Have you had a chance to do additional research on this?
@MuhammadAli-zv5vz
@MuhammadAli-zv5vz 7 жыл бұрын
Lets Build That App i tried it excute extra loop 🤔🤔🤔
@spell931
@spell931 5 жыл бұрын
I wish I have rather spent time watching your videos that pewdiepie's
@muhammadfarooqi
@muhammadfarooqi 6 жыл бұрын
you know?..another youtuber came here! and he clicked "dislike" !!!. common guys. appreciate don't' show jealousy
Swift 3 Fun Algorithms: Recursive Search through Binary Tree
16:34
Lets Build That App
Рет қаралды 22 М.
When You Get Ran Over By A Car...
00:15
Jojo Sim
Рет қаралды 4,7 МЛН
Swift Interview Algorithms: Stacks and Generics
13:03
Lets Build That App
Рет қаралды 29 М.
Interview Algorithm Challenge: Reverse Linked List - Swift
11:23
Lets Build That App
Рет қаралды 20 М.
Filter, Map, Reduce - Swift - iOS Interview Questions
11:08
Sean Allen
Рет қаралды 53 М.
A Simpler Way to See Results
19:17
Logan Smith
Рет қаралды 98 М.
Loops and Hash Maps Job Preparation Interview Question
15:55
Lets Build That App
Рет қаралды 20 М.
What is Unit Testing, Why We Use It, and Sample Test Cases
12:17
Lets Build That App
Рет қаралды 306 М.
When You Get Ran Over By A Car...
00:15
Jojo Sim
Рет қаралды 4,7 МЛН