Git Product home page Git Product logo

Comments (11)

kytrinyx avatar kytrinyx commented on June 25, 2024

@AJEzk My guess is that it just kind of happened that way.

@marianaiam What's Gen's GitHub? I feel like between the two of you, you might have an opinion on this.

from swift.

hankturowski avatar hankturowski commented on June 25, 2024

This was a tricky one because the cleanest solution is either a struct that conforms to OptionSetType or, as you suggested, an enum with integer raw values. I haven't looked at Allergies in a while but I seem to remember it being tough to design an API that allowed for either solution.

In writing the tests, I tried to avoid requiring a certain approach. I'm fairly sure that's why this one didn't require enum. I haven't looked too deeply at this one since Swift 2, so if you can think of a way to allow for OptionSetType or enum, I'd love the pull request.

If you can't think of a way to allow either solution, submit a pull request anyway. We're short on enumerations and I think it would help to have a few more.

from swift.

hankturowski avatar hankturowski commented on June 25, 2024

I've thought about this some more and I'm having a lot of trouble figuring out a really clean way to solve this problem with enums that didn't require some tricky handling. You could store the list of allergies in a Set<Allergies> and use .contains() to check, but then how would we encapsulate that behavior without leaking the implementation? Maybe use a factory? Am I missing an obvious solution?

I'm starting to think that sticking with OptionSetType really is the best solution, but I also would like more Enums in the Swift problem set. Maybe we can find another problem that could use enums, or just write a new one entirely?

from swift.

masters3d avatar masters3d commented on June 25, 2024

https://gist.github.com/masters3d/5e91105d2ef994c4211e
This could allow struct or enum solutions.

from swift.

hankturowski avatar hankturowski commented on June 25, 2024

Clever. I was hung up on instantiating an allergy set, but your static method sidesteps the issue nicely. I like it.

from swift.

minmadic avatar minmadic commented on June 25, 2024

I was thinking something very similar to @masters3d.

The tests would look something like this:

class AllergiesTest: XCTestCase {

    func testBob() {

        let patient = Patient(34)

        XCTAssertTrue(patient.hasAllergy(Allergies.Peanuts), "Bob is allergic to peanuts")
        XCTAssertTrue(patient.hasAllergy(Allergies.Chocolate), "Bob is allergic to chocolate")
        XCTAssertFalse(patient.hasAllergy(Allergies.Cats),  "Bob is not allergic to cats")
    }

    func testEggsNcats() {

        let patient = Patient(129)

        XCTAssertTrue(patient.hasAllergy(Allergies.Eggs))
        XCTAssertTrue(patient.hasAllergy(Allergies.Cats))
        XCTAssertFalse(patient.hasAllergy(Allergies.Chocolate))

    }

    func testNone() {
        let patient = Patient(0)

        XCTAssertFalse(patient.hasAllergy(Allergies.Pollen))
    }

    func testAll() {

        let allInt =  UInt(Array(0...7).reduce(0){ return ($0 | (1 << $1)) })
        let patient = Patient(allInt)

        XCTAssertTrue(patient.hasAllergy(Allergies.Eggs))
        XCTAssertTrue(patient.hasAllergy(Allergies.Peanuts))
        XCTAssertTrue(patient.hasAllergy(Allergies.Shellfish))
        XCTAssertTrue(patient.hasAllergy(Allergies.Strawberries))
        XCTAssertTrue(patient.hasAllergy(Allergies.Tomatoes))
        XCTAssertTrue(patient.hasAllergy(Allergies.Chocolate))
        XCTAssertTrue(patient.hasAllergy(Allergies.Pollen))
        XCTAssertTrue(patient.hasAllergy(Allergies.Cats))

    }
}

A sample solution would look like this:

enum Allergies: UInt {
        case Eggs = 1
        case Peanuts = 2
        case Shellfish = 4
        case Strawberries = 8
        case Tomatoes = 16
        case Chocolate = 32
        case Pollen = 64
        case Cats = 128
}

struct Patient {
    let score: UInt

    init(_ score: UInt) {
        self.score = UInt(score)
    }

    func hasAllergy(allergen: Allergies) -> Bool {
        return allergen.rawValue & score == allergen.rawValue ? true : false
    }
} 

from swift.

masters3d avatar masters3d commented on June 25, 2024

Also you can drop the enum name from the test case
hasAllergy( .Pollen)
The compiler is able to tell which enum type it takes so it doesn't enforce the name to be included.

from swift.

masters3d avatar masters3d commented on June 25, 2024

Thanks @AJEzk for the PR!

from swift.

minmadic avatar minmadic commented on June 25, 2024

Yes! My first open source contribution! :)

from swift.

masters3d avatar masters3d commented on June 25, 2024

Nice!

from swift.

hankturowski avatar hankturowski commented on June 25, 2024

@AJEzk The first of many, I hope! Great work!

from swift.

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.