How to use sets for fast data lookup - Swift for Complete Beginners

Поделиться
HTML-код
  • Опубликовано: 1 ноя 2024

Комментарии • 6

  • @jordanwright5795
    @jordanwright5795 2 года назад

    Excellent series! I'm enjoying it immensely, especially after a few false starts with other languages. I think one area where I struggle is that some of the syntax rules feel so so arbitrary to me and without being able to rationalize why certain things are done a certain way it makes it difficult to commit to memory as the only way to is not through deductive reasoning when you can't remember but through repetition.
    For instance why in the case when creating an array do we only need square brackets
    let dinosaurFood = [
    "name": "T-rex",
    "food": "Stegasaurus",
    "lives": "Prehistoric Jungle"
    ]
    but when coding in a set it's a combination of brackets and parentheses.
    let troubleMakers = Set ([
    "Turkin",
    "Gerdy",
    "Porky",
    "Dutchy,
    "BuggaBoo"
    ])
    I know they perform different functions but what's the logical rationale for when a parenthesis is to be used VS a bracket or is it just arbitrary syntax based on what the Swift developers chose in the beginning?
    Thanks again - really enjoying the course!

    • @Blueberryisgood
      @Blueberryisgood 5 месяцев назад

      Sets are collections of unique values. The reason for using parentheses along with square brackets when initializing a set is that you are calling a function Set that takes an array as an argument to create the set. The parentheses are part of the function call syntax in Swift.

  • @Marwa-lb7oz
    @Marwa-lb7oz 6 месяцев назад

    why would we use array over set then if sets are more optimised? wouldn't we always want our apps to be optimised?

    • @WhatDoINameMyChannel
      @WhatDoINameMyChannel 4 месяца назад

      I'm only a few hours into learning Swift, but I believe you'd want to use an Array over a Set in case you want to store duplicate items, or store data in a particular order.
      With an Array, they are always in a particular order (the index) like 0, 1, 2, 3.
      With a Set, they are always in a random order (2, 3, 1 the first time you print, 1, 3 2 the next time, etc).
      Also duplicates are ignored in Sets, so if you have orange orange orange red, in a Set it'd just be orange red. So if you're displaying a list of temperatures each day of the week, if 2 days had the same temp, you don't want it to be ignored hence putting that in an Array.
      Perhaps I'm wrong, or perhaps you don't care, or perhaps you know this since your message was a month ago lol

    • @CésarManuelCuervoOrizondo
      @CésarManuelCuervoOrizondo 3 месяца назад

      A Set is a collection of unique, unordered values with high efficiency for searches. For example, you might have an array of repeated elements and convert it to a Set to remove duplicates. The great power of a Set comes from its ability to find values in a collection.

  • @Togidubnus
    @Togidubnus 3 года назад +2

    No Sets, Please, We're British.
    Let's not forget all the set membership methods that are so useful. But maybe I"m getting ahead of you.