Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ideas about global storage #97

Open
bwetherfield opened this issue Oct 19, 2018 · 0 comments
Open

Ideas about global storage #97

bwetherfield opened this issue Oct 19, 2018 · 0 comments
Labels

Comments

@bwetherfield
Copy link
Member

I wonder if the following could be of use, @jsbean ?

struct Prism <A,B> {
    let unwrap: (A) -> B?
    let wrap: (B) -> A
}

enum Music {
    
    static var objects: [Music] = []
    
    case duration(Duration)
    case pitch(Pitch)
...
}

extension Music {
    enum prism {
        static let duration = Prism<Music, Duration>(
            unwrap: {
                if case let .duration(object) = $0 { return object }
                return nil
            },
            wrap: Music.duration
        )
        
        static let pitch = Prism<Music, Pitch>(
            unwrap: {
                if case let .pitch(object) = $0 { return object }
                return nil
            },
            wrap: Music.pitch
        )
    }
...
}

extension Music {

    func allDurations() -> [Duration] {
        return Music.objects.compactMap(Music.prism.duration.unwrap)
    }
    
    func allPitches() -> [Pitch] {
        return Music.objects.compactMap(Music.prism.pitch.unwrap)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant