🌊 Let your code flow.
This library provides a bunch of extension methods for a better fluent syntax in Swift. This style is very useful for some operations that benefit from being able to be chained (composed) together.
.then
to configure reference and value types. Useful for configuration at the point of initialization.
.mutate
in place value types.
.let
to transform an object into another.
.do
to perform multiple actions with the same object.
Free function variants, for when you prefer this syntax or don't want to conform to the protocol:
with
(similar to .then
)withLet
(similar to .let
)run
as an alternative to immediately executed closures.
.then
Use .then
to perform an object configuration inline. It applies statements in the closure to the object. It's very useful to set the properties of an object when defining it.
let label = UILabel().then {
$0.text = "Hello"
$0.textColor = .red
$0.font = .preferredFont(forTextStyle: .largeTitle)
$0.sizeToFit()
}
let size = CGSize().then {
$0.width = 20
}
There are two overloads of this method provided. One that works on
AnyObject
(a.k.a. classes) and another that operates onAny
(intended for value types). The compiler picks the correct one appropriately.
self
or an inout
copy in case of value types.Influences:
.mutate
Mutates a value in place. It s like .then
but applies to self
instead of a new copy. The value needs to be defined as a var
.
view.frame.mutate {
$0.origin.y = 200
$0.size.width = 300
}
inout
reference to self
.This should be used only for value types. For reference types is recommended to use
.then
.
.let
You can think of .let
as a map
operation but for all the types (not only for Functors). It lets you transform the object into an object of another type.
let dateString: String = Date().let {
let formatter = DateFormatter()
return formatter.string(from: $0)
}
It works especially well for type conversions based on initializers:
let number: Int? = "42".let(Int.init)
Don't overuse this when you can use just plain dot syntax. You can use it to access a member of the object
Date().let { $0.timeIntervalSince1970 }
but that's just the same asDate().timeIntervalSince1970
.
self
in the closure.Influences:
let
declaration..do
Use this method to perform multiple actions (side effects) with the same object. It helps to reduce the verbosity of typing the same name multiple times.
UserDefaults.standard.do {
$0.set(42, forKey: "number")
$0.set("hello", forKey: "string")
$0.set(true, forKey: "bool")
}
This behaves like other methods if you discard their return, but is preferred to use do
to convey the intention better. It also lets you avoid writing the return
on some occasions.
self
in the closure.Influences:
.debug
By default, it prints self
to the console. This method is useful for debugging intermediate values of a chain of method calls.
let result = Object()
.then { ... }
.debug("prefix")
.let { ... }
.debug()
self
in the closure.with
Executes a closure with the object. This free function it's a substitute for .then
when you can't use the method or if you prefer the free function style.
let label = with(UILabel()) {
$0.text = "Hello"
$0.textColor = .red
$0.font = .preferredFont(forTextStyle: .largeTitle)
$0.sizeToFit()
}
inout
copy of self
in the closure.Influences:
with
or using
function.withLet
Variant of with
that let's you return a different type. It's a free function alternative of let
.
run
Executes a closure of statements, useful to be used when you need an expression. This is like making a closure and invoking immediately but sometimes is clearer to have a specific name for it.
let result = run { ... } // same as { ... }()
Influences:
Since Swift doesn't let us extend non-nominal types like Any
we need to conform each type to the Flowable
protocol.
The library provides out of the box conformances for a bunch of Standard Library, Foundation and UIKit types. See Conformances.swift for the entire list.
You can conform any type yourself by just extending it:
extension YourType: Flowable {}
Note that you can use the free function variants without the types conforming to the protocol.
with
or using
functions.Alejandro Martinez | https://alejandromp.com | @alexito4
link |
Stars: 102 |
Last commit: 2 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics