GithubHelp home page GithubHelp logo

Comments (3)

xwu avatar xwu commented on August 15, 2024 2

This is the expected behavior, if I’m understanding the point correctly.

Not all collection indices start at 0, and indeed they don’t need to have indices of type Int. However, when you obtain a slice of a collection (as in this case for chunks), that slice shares indices with its parent. When you attempt to access the element of a slice at index 1, this is supposed to be out of bounds if the slice doesn’t include the parent collection’s element that’s at index 1.

There isn’t anything specific to ChunksOfCountCollection but rather common to all of Swift. Understanding this point is key to using indices correctly.

from swift-algorithms.

rudedogdhc avatar rudedogdhc commented on August 15, 2024

You are correct and I've learned something new about Swift. Using $0.count == 2 ? $0[$0.startIndex + 1] : nil is the proper way to do this.

from swift-algorithms.

mdznr avatar mdznr commented on August 15, 2024

Using $0.count == 2 ? $0[$0.startIndex + 1] : nil is the proper way to do this.

index(_:offsetBy:) exists to do this. This would work for indices that aren’t Int and those that might not count by 1s.

from swift-algorithms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.