DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Swift Sequences: The .next() Generator

Swift Sequences: The .next() Generator

A quick introduction to some of the Swift libraries for handling sequences and generators.

Alex Curylo user avatar by
Alex Curylo
·
Apr. 30, 16 · Mobile Zone · Opinion
Like (1)
Save
Tweet
5.47K Views

Join the DZone community and get the full member experience.

Join For Free

Here’s a nice gentle introduction (h/t: This Week In Swift) to modelling an algorithmic collection using the SequenceType and GeneratorType protocols:

Sequences and Generators

Let’s imagine, that we want to learn the new language. Spanish, for example. Your professor told you, that you will have one lesson each N days starting from today. But the bad thing – your professor doesn’t want to work on Sundays. So if the lesson is on Sunday, it will be rescheduled to the next day. We want to receive the schedule – Array<NSDate> for M days.

Grab the playground here, and walk through creating an NSDate sequence with:

  1. A generator class for a defined number of iterations
  2. Using AnyGenerator for a bounded number of iterations
  3. A generator class for an infinite number of dates

Along with using .prefix(), .filter() and .lazy() to make that infinite sequence actually usable in Swift 2, since filter() is no longer a free function like in Swift 1. More detailed explorations of laziness can be found here:

  • Being Lazy

  • True Lazy Sequences

  • Experimenting with Swift Sequences and Generators

And what do you get when you add indexability to a sequence? Why, a collection:

  • Swift Collections

  • Generic Collections, SubSequences and Overloading

For an application of the generator concept to a no doubt common modelling problem you’re presented with, check out — Practical Swift: pages generator – build once, use many.

In the real world, at some point, we all have to deal with data that is fetched from a backend service, page by page. It’s called paging. Everybody does this! Nothing to be ashamed of :-) Here is one of the ways in which a Swift pattern (well, not specific to Swift, but visible in Swift) can be used to build a neat solution that allows me to fetch data in chunks and display it in the application…

And finally, check out SwiftSequence, “A μframework of extensions for SequenceType in Swift 2.0, inspired by Python’s itertools, Haskell’s standard library, and other things,” for some handy manipulations:

  • ScanReduce
  • TakeDrop
  • Hopping and Slicing
  • Interpose
  • Combinations
  • Permutations
  • Cycle
  • Categorise
  • ChunkWindowSplit
  • Enumerate
  • Finding
  • NestedSequences
  • Zip
Swift (programming language)

Published at DZone with permission of Alex Curylo, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why I'm Choosing Pulumi Over Terraform
  • Deployment of Low-Latency Solutions in the Cloud
  • Modernize Legacy Code in Production: Rebuild Your Airplane Midflight Without Crashing
  • Java: Why Core-to-Core Latency Matters

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo