Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

To claim this, I am signing this object:

Answer to http://disq.us/p/1za4u75.


Hi, your problem is actually quite hard to solve with composition, at least without a profunctor-based approach, which is really hard to do in Swift due to the lack of higher-kinded types.

There is a solution, though, but we must be clear about what we're searching for here. In your ViewState<T> there could be no Prism that points just to T, because the inject function wouldn't know what case to produce with a value of type T: it's probably going to be an Affine.

At the bottom of this answer you'll find all the code needed to implement it: it can be directly copy-pasted into a playground.

@broomburgo
broomburgo / SwiftProfunctorHKT.swift
Last active July 3, 2018 16:00
Swift Profuctor Optics
protocol TaggedType {
associatedtype TagType
}
protocol TypeConstructor1: TaggedType {
associatedtype ParameterType1
}
protocol TypeConstructor2: TypeConstructor1 {
associatedtype ParameterType2
@broomburgo
broomburgo / lenses-and-prisms-in-swift-a-pragmatic-approach.swift
Last active September 6, 2017 11:38
code for the article "Lenses and Prisms in Swift: a pragmatic approach"
/// source: https://broomburgo.github.io/fun-ios/post/lenses-and-prisms-in-swift-a-pragmatic-approach/
protocol LensType {
associatedtype WholeType
associatedtype PartType
var get: (WholeType) -> PartType { get }
var set: (PartType,WholeType) -> WholeType { get }
init(get: @escaping (WholeType) -> PartType, set: @escaping (PartType,WholeType) -> WholeType)
feedbackModelController.deltaSignal
.filter { $0.feedback.rawValue < $1.feedback.rawValue}
.filter { $1.feedback == .Good || $1.feedback == .ReallyGood }
.onReception § eachTime § inAnyCase § showThankYouAlert
func collectFeedbackModelChange() -> Signal<FeedbackModelChange> {
return pageFactory.signalMakeMainPage
.flatMap { $0.signalPolarizedChanged }
.map(FeedbackModel.transformWithPolarized)
+ pageFactory.signalMakeSelectionPage
.flatMap { $0.signalSelection }
.map(FeedbackModel.transformWithFeedback)
}
pageFactory.signalMakeMainPage
.flatMap { $0.signalLeaveFeedback }
.onReception § eachTime § inAnyCase § presentSelectionPage
pageFactory.signalMakeSelectionPage
.flatMap { $0.signalSelection }
.onReception § eachTime § inAnyCase § popTopPage
/// MainPage initializer
init(feedbackModelController: ModelController<FeedbackModel>) {
super.init(nibName: nil, bundle: nil)
feedbackModelController.updateSignal.onReception § eachTime § updateViewsWithFeedbackModel
viewReadyEmitter.signal.onReception § eachTime § feedbackModelController.notify
}
import Foundation
public enum Persistence {
case Stop
case Continue
}
public final class Signal<Subtype> {
typealias Observation = Subtype -> Persistence
@broomburgo
broomburgo / Optional.h
Last active September 5, 2018 08:21
Optional type for Objective-C
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Optional : NSObject
+ (Optional*)with:(id _Nullable)value;
+ (Optional*)with:(id _Nullable)value as:(Class _Nonnull)valueClass;