ruiaaperes / optionalextensions Goto Github PK
View Code? Open in Web Editor NEWSwift µframework with extensions for the Optional Type
License: MIT License
Swift µframework with extensions for the Optional Type
License: MIT License
This is not an issue!
I'm just interested in your opinion of the code below, it adds no additional functionality, hence, not an issue. As an exercise for my own entertainment, I've created a version of the extension that utilises as much sugary syntax as I could muster (using single expressions per function and using if case
syntax when apt, etc).
Your original is written in a clear, explicit style which is easy to decipher, the version below is undoubtedly more cryptic (one thing I do like is that each function contains one expression and one return
statement, but it is arguable how important that is). I think the versions of onSome
and onNone
would fit with the current style, but the others are a bit of a departure.
Anyway, I thought I'd throw it out there to see what you thought.
All the best,
Al
public extension Optional {
func filter(@noescape predicate: Wrapped -> Bool) -> Optional {
return map(predicate) == .Some(true) ? self : .None
}
func replaceNil(with replacement: Wrapped) -> Optional {
return self ?? replacement
}
func apply(@noescape f: Wrapped -> Void) {
if case let wrapped? = self { f(wrapped) }
}
func onSome(@noescape f: Wrapped -> Void) -> Optional {
apply(f)
return self
}
func onNone(@noescape f: Void -> Void) -> Optional {
if isNone { f() }
return self
}
var isSome: Bool {
return map { _ in true } ?? false
}
var isNone: Bool {
return !isSome
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.