Skip to content

Instantly share code, notes, and snippets.

View gist:bd4871f7c35be3790a7883f829925af3

Answer to

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 / 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 / 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:
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
.filter { $ < $}
.filter { $ == .Good || $ == .ReallyGood }
.onReception § eachTime § inAnyCase § showThankYouAlert
View FeedbackCollector.swift
func collectFeedbackModelChange() -> Signal<FeedbackModelChange> {
return pageFactory.signalMakeMainPage
.flatMap { $0.signalPolarizedChanged }
+ pageFactory.signalMakeSelectionPage
.flatMap { $0.signalSelection }
View PageFactory.swift
.flatMap { $0.signalLeaveFeedback }
.onReception § eachTime § inAnyCase § presentSelectionPage
.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 / Optional.h
Last active Sep 5, 2018
Optional type for Objective-C
View Optional.h
#import <Foundation/Foundation.h>
@interface Optional : NSObject
+ (Optional*)with:(id _Nullable)value;
+ (Optional*)with:(id _Nullable)value as:(Class _Nonnull)valueClass;
broomburgo / gist:ba8e5dd6cd509fdfd781
Created May 4, 2015
View gist:ba8e5dd6cd509fdfd781
struct Field {
let id: String
let name: String
let value: String
let visible: Bool