Skip to content

Instantly share code, notes, and snippets.

View gist:bd4871f7c35be3790a7883f829925af3

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 Jul 3, 2018
Swift Profuctor Optics
View SwiftProfunctorHKT.swift
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 Sep 6, 2017
code for the article "Lenses and Prisms in Swift: a pragmatic approach"
View lenses-and-prisms-in-swift-a-pragmatic-approach.swift
/// 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)
View AppDelegate.swift
feedbackModelController.deltaSignal
.filter { $0.feedback.rawValue < $1.feedback.rawValue}
.filter { $1.feedback == .Good || $1.feedback == .ReallyGood }
.onReception § eachTime § inAnyCase § showThankYouAlert
View FeedbackCollector.swift
func collectFeedbackModelChange() -> Signal<FeedbackModelChange> {
return pageFactory.signalMakeMainPage
.flatMap { $0.signalPolarizedChanged }
.map(FeedbackModel.transformWithPolarized)
+ pageFactory.signalMakeSelectionPage
.flatMap { $0.signalSelection }
.map(FeedbackModel.transformWithFeedback)
}
View PageFactory.swift
pageFactory.signalMakeMainPage
.flatMap { $0.signalLeaveFeedback }
.onReception § eachTime § inAnyCase § presentSelectionPage
pageFactory.signalMakeSelectionPage
.flatMap { $0.signalSelection }
.onReception § eachTime § inAnyCase § popTopPage
View MainPage.swift
/// MainPage initializer
init(feedbackModelController: ModelController<FeedbackModel>) {
super.init(nibName: nil, bundle: nil)
feedbackModelController.updateSignal.onReception § eachTime § updateViewsWithFeedbackModel
viewReadyEmitter.signal.onReception § eachTime § feedbackModelController.notify
}
View Signal+Emitter.swift
import Foundation
public enum Persistence {
case Stop
case Continue
}
public final class Signal<Subtype> {
typealias Observation = Subtype -> Persistence
@broomburgo
broomburgo / Optional.h
Last active Sep 5, 2018
Optional type for Objective-C
View Optional.h
#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;
@broomburgo
broomburgo / gist:ba8e5dd6cd509fdfd781
Created May 4, 2015
type-first-development-in-swift-code-for-blog-article
View gist:ba8e5dd6cd509fdfd781
/* NOT QUITE FLEXIBLE
struct Field {
let id: String
let name: String
let value: String
let visible: Bool