create account

A Beginners Guide to Dart - Scope, Iterators, Functional Programming and Collections - Part Six by tensor

View this thread on: hive.blogpeakd.comecency.com
· @tensor · (edited)
$50.78
A Beginners Guide to Dart - Scope, Iterators, Functional Programming and Collections - Part Six
![dart-bird.jpg](https://cdn.steemitimages.com/DQmVf52JbNULs9kr7M9BTaZfgoEtAUdHc9VpVvGpuu4mg24/dart-bird.jpg)

#### Repository
https://github.com/dart-lang/sdk

#### What Will I Learn?

- You will learn about Lexical Scoping
- You will learn about Iterators in Dart
- You will learn about Collection Types
- You will learn about Iterator and Collection Methods
- You will learn about Functional Programming Concepts

#### Requirements
##### System Requirements: 
- [IDEA intellij](https://www.jetbrains.com/idea/) or [Visual Studio Code](https://code.visualstudio.com/) with the Dart [Plugins](https://dartcode.org/)
- The [Dart SDK](https://www.dartlang.org/) 

##### OS Support for Dart:
- Windows
- macOS
- Linux 

#### Required Knowledge
- The Dart SDK
- A Dart supported text editor ([Dart Pad can be used](https://dartpad.dartlang.org))
- A little time to sit and watch a video and some patience to learn the language

#### Resources for Dart:
- Dart Website: https://www.dartlang.org/
- Dart Official Documentation: https://www.dartlang.org/guides/language/language-tour
- Dart GitHub repository: https://github.com/dart-lang/sdk
- Awesome Dart GitHub Repository: https://github.com/yissachar/awesome-dart
- Pub website: https://pub.dartlang.org

#### Sources:
Dart Logo (Google): https://www.dartlang.org/

#### Difficulty

- Beginner


#### Description

In this Dart Tutorial, we take a look at some different concepts.  We look at Lexical Scoping, Collections such as Lists and Maps, Iterators and Functional Methods for traversing through iterators and collections.  This includes the inheritance and class structure for these different collection types and how the iterator type relates to them. We also look at the `for-in` syntactic sugar. 

#### Lexical Scoping and Block Scope in Dart

Dart as a language has a very straight forward scoping model.  It embraces the concept of Lexical Scoping or Block Scoping.  When a variable is defined, it is automatically exposed to the block that it is defined in. This also includes any blocks that are inside of that original block.  The variable is not directly exposed to outside blocks unless the developer specifies otherwise.  Also, variables can be passed to objects and functions via parameters and through other methods in this way.  Dart also features a Global Scope which allows a variable or resource to be exposed to all of the code in a file and project. 

<center>
![scope.png](https://cdn.steemitimages.com/DQmYeLLJMdnfPWhLpZB5u9sZhYLxwX8vJiwHHMftHKqK6Xn/scope.png)
</center>


This image features some fairly basic code but it has a good example of lexical scoping.  In the top of the main function, an X value was defined.  This `x` value is exposed to anything that appears inside of the yellow box, including the two green boxes.  In the first green box, we have a for loop that defines an integer variable `i`.  This `i` variable is used to iterate through the list `x` and its scoped to this green box.  The second green box features yet another `i` variable.  This new `i` variable can be defined because the former `i` variable is no longer in the scope when it is declared.  Even though both of these variables use the same name, they are different from one another and can co-exist in the code since they are in seperate scopes. 

#### Working with Iterators/iterables and Collections 

Collections are a very common type to use in any programming language.  In Dart, the main collection types are the List, Set, Map and Queue.  List and Set both directly inherit from a class called iterable and Map and Queue both have their own parent classes that are very similar to this iterable class.  The iterable class is important because it allows the program to read over a collection of data without mutating the data.  It does this by way of lazy evaluation; the iterable only reads a value if it is asked to read that value.  The iterable is what enables the `for-in` loop as well as the three primary functional methods, `map`, `reduce` and `filter` or `where` in the case of Dart.


<center>
![iterator.png](https://cdn.steemitimages.com/DQmQXFkC9GUGPSaqWmVcGiEYtCdYSEbVitLDHPAAA2Ghanx/iterator.png) 
</center>

In the image above, the code defines an iterable value that contains 1000 items starting from 0 and ending at 999.  Below it are some examples of the various methods that are attached to the iterable class.  In the first line, the `reduce` method is being used to add all of the values in the iterable together into a single value; `0 + 1 + 2 + 3... + 999`.  On the next line, the `take` method is chained into a `map` method.  The `take` method uses the iterable's lazy nature to only evaluate the first 10 values then the `map` method takes those values and multiplies them all by 2 giving us a new list of values via `toList()`.  The final three lines show off three of the conditional operators in Dart which are all variants of the `where` or `filter` method.  The `any` method iterates through the collection and find out if any item matches the conditional.  If a single item does, then it will return a `true` value.  The `every` method is the exact opposite in that it only returns `true` if all of the values in the list meet the conditional statement.   Finally, we have the `where` method which will return values that satisfy the conditional in a iterable type.  In this case, we are turning that iterable into a `set` collection type through a chained `toSet()` method.  

The Source Code for this video may be found here: https://github.com/tensor-programming/dart_for_beginners/tree/tensor-programming-patch-5


#### Video Tutorial
<iframe width="560" height="315" src="https://www.youtube.com/embed/bsvZgW5_b8w" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

#### Curriculum 
- [A Beginners Guide to Dart - Inheritance, Abstract Classes, Interfaces, Mixins and Casting - Part Five](https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---inheritance-abstract-classes-interfaces-mixins-and-casting---part-five)
- [A Beginners Guide to Dart - Methods, Final, Static, and Class Inheritance - Part Four](https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---methods-final-static-and-class-inheritance---part-four)
- [A Beginners Guide to Dart - Intro to Classes and Objects - Part Three](https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---intro-to-classes-and-objects---part-three)
- [A Beginners Guide to Dart - Control Flow and Low Level Compilation - Part Two](https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---control-flow-and-low-level-compilation---part-two)
- [A Beginners Guide to Dart - Types, Functions, Variables and Objects - Part One](https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---types-functions-variables-and-objects---part-one)
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 180 others
properties (23)
authortensor
permlinka-beginners-guide-to-dart---scope-iterators-functional-programming-and-collections---part-six
categoryutopian-io
json_metadata{"tags":["utopian-io","video-tutorials","steemstem","programming","technology"],"app":"steemit/0.1","image":["https://cdn.steemitimages.com/DQmVf52JbNULs9kr7M9BTaZfgoEtAUdHc9VpVvGpuu4mg24/dart-bird.jpg","https://cdn.steemitimages.com/DQmYeLLJMdnfPWhLpZB5u9sZhYLxwX8vJiwHHMftHKqK6Xn/scope.png","https://cdn.steemitimages.com/DQmQXFkC9GUGPSaqWmVcGiEYtCdYSEbVitLDHPAAA2Ghanx/iterator.png","https://img.youtube.com/vi/bsvZgW5_b8w/0.jpg"],"links":["https://github.com/dart-lang/sdk","https://www.jetbrains.com/idea/","https://code.visualstudio.com/","https://dartcode.org/","https://www.dartlang.org/","https://dartpad.dartlang.org","https://www.dartlang.org/guides/language/language-tour","https://github.com/yissachar/awesome-dart","https://pub.dartlang.org","https://github.com/tensor-programming/dart_for_beginners/tree/tensor-programming-patch-5","https://www.youtube.com/embed/bsvZgW5_b8w","https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---inheritance-abstract-classes-interfaces-mixins-and-casting---part-five","https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---methods-final-static-and-class-inheritance---part-four","https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---intro-to-classes-and-objects---part-three","https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---control-flow-and-low-level-compilation---part-two","https://steemit.com/utopian-io/@tensor/a-beginners-guide-to-dart---types-functions-variables-and-objects---part-one"],"format":"markdown"}
created2019-03-09 05:46:33
last_update2019-03-09 05:49:39
depth0
children6
last_payout2019-03-16 05:46:33
cashout_time1969-12-31 23:59:59
total_payout_value38.174 HBD
curator_payout_value12.609 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,904
author_reputation87,856,203,149,624
root_title"A Beginners Guide to Dart - Scope, Iterators, Functional Programming and Collections - Part Six"
beneficiaries
0.
accountsteemplus-pay
weight100
1.
accountutopian.pay
weight500
max_accepted_payout100,000.000 HBD
percent_hbd10,000
post_id80,950,660
net_rshares74,078,687,281,718
author_curate_reward""
vote details (244)
@rosatravels ·
$11.82
Hi @tensor,

Great to have you doing these Dart tutorials for beginners.

You outline is easy to follow and your video tutorial is easy to understand with the flow of teaching.

It is a pleasure to watch these videos.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/9/1-1-1-1-1-1-1-1-3-).

---- 
Need help? Chat with us on [Discord](https://discord.gg/uTyJkNm).
πŸ‘  , , , , , , , , , , , , ,
properties (23)
authorrosatravels
permlinkre-tensor-a-beginners-guide-to-dart---scope-iterators-functional-programming-and-collections---part-six-20190311t070951726z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["tensor"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/9/1-1-1-1-1-1-1-1-3-","https://discord.gg/uTyJkNm"],"app":"steemit/0.1"}
created2019-03-11 07:09:51
last_update2019-03-11 07:09:51
depth1
children2
last_payout2019-03-18 07:09:51
cashout_time1969-12-31 23:59:59
total_payout_value8.977 HBD
curator_payout_value2.842 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length622
author_reputation422,827,447,688,168
root_title"A Beginners Guide to Dart - Scope, Iterators, Functional Programming and Collections - Part Six"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,053,816
net_rshares16,831,919,547,509
author_curate_reward""
vote details (14)
@tensor ·
Thank you @rosatravels.  Always great to read your comments on my contributions.
properties (22)
authortensor
permlinkre-rosatravels-re-tensor-a-beginners-guide-to-dart---scope-iterators-functional-programming-and-collections---part-six-20190311t201819125z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["rosatravels"],"app":"steemit/0.1"}
created2019-03-11 20:18:15
last_update2019-03-11 20:18:15
depth2
children0
last_payout2019-03-18 20:18:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length80
author_reputation87,856,203,149,624
root_title"A Beginners Guide to Dart - Scope, Iterators, Functional Programming and Collections - Part Six"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,090,040
net_rshares0
@utopian-io ·
Thank you for your review, @rosatravels! Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-tensor-a-beginners-guide-to-dart---scope-iterators-functional-programming-and-collections---part-six-20190311t070951726z-20190314t050026z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-03-14 05:00:27
last_update2019-03-14 05:00:27
depth2
children0
last_payout2019-03-21 05:00:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length63
author_reputation152,955,367,999,756
root_title"A Beginners Guide to Dart - Scope, Iterators, Functional Programming and Collections - Part Six"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,279,718
net_rshares0
@steem-plus ·
SteemPlus upvote
Hi, @tensor!

You just got a **5.99%** upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in [here](https://steemit.com/@steem-plus) to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.
properties (22)
authorsteem-plus
permlinka-beginners-guide-to-dart---scope-iterators-functional-programming-and-collections---part-six---vote-steemplus
categoryutopian-io
json_metadata{}
created2019-03-09 09:47:36
last_update2019-03-09 09:47:36
depth1
children0
last_payout2019-03-16 09:47:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length433
author_reputation247,952,188,232,400
root_title"A Beginners Guide to Dart - Scope, Iterators, Functional Programming and Collections - Part Six"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,957,913
net_rshares0
@steem-ua ·
$0.04
#### Hi @tensor!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
πŸ‘  
properties (23)
authorsteem-ua
permlinkre-a-beginners-guide-to-dart---scope-iterators-functional-programming-and-collections---part-six-20190311t071116z
categoryutopian-io
json_metadata"{"app": "beem/0.20.18"}"
created2019-03-11 07:11:18
last_update2019-03-11 07:11:18
depth1
children0
last_payout2019-03-18 07:11:18
cashout_time1969-12-31 23:59:59
total_payout_value0.028 HBD
curator_payout_value0.009 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length285
author_reputation23,214,230,978,060
root_title"A Beginners Guide to Dart - Scope, Iterators, Functional Programming and Collections - Part Six"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,053,858
net_rshares53,771,571,441
author_curate_reward""
vote details (1)
@utopian-io ·
Hey, @tensor!

**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Get higher incentives and support Utopian.io!**
 Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-a-beginners-guide-to-dart---scope-iterators-functional-programming-and-collections---part-six-20190312t035937z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-03-12 03:59:39
last_update2019-03-12 03:59:39
depth1
children0
last_payout2019-03-19 03:59:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length588
author_reputation152,955,367,999,756
root_title"A Beginners Guide to Dart - Scope, Iterators, Functional Programming and Collections - Part Six"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,111,704
net_rshares0