diff --git a/0080-combine-and-effects-pt1/Combine.playground/Contents.swift b/0080-combine-and-effects-pt1/Combine.playground/Contents.swift new file mode 100644 index 00000000..6bcd8a29 --- /dev/null +++ b/0080-combine-and-effects-pt1/Combine.playground/Contents.swift @@ -0,0 +1,82 @@ + +public struct Effect { + public let run: (@escaping (A) -> Void) -> Void + + public func map(_ f: @escaping (A) -> B) -> Effect { + return Effect { callback in self.run { a in callback(f(a)) } } + } +} + +import Dispatch + +let anIntInTwoSeconds = Effect { callback in + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { + callback(42) + callback(1729) + } +} + +anIntInTwoSeconds.run { int in print(int) } + +//anIntInTwoSeconds.map { $0 * $0 }.run { int in print(int) } + +import Combine + +//Publisher.init + +//AnyPublisher.init(<#T##publisher: Publisher##Publisher#>) + + +var count = 0 +let iterator = AnyIterator.init { + count += 1 + return count +} +Array(iterator.prefix(10)) + +let aFutureInt = Deferred { + Future { callback in + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { + print("Hello from the future") + callback(.success(42)) + callback(.success(1729)) + } + } +} + +//aFutureInt.subscribe(AnySubscriber.init( +// receiveSubscription: { subscription in +// print("subscription") +// subscription.cancel() +// subscription.request(.unlimited) +//}, +// receiveValue: { value -> Subscribers.Demand in +// print("value", value) +// return .unlimited +//}, +// receiveCompletion: { completion in +// print("completion", completion) +//} +//)) + +let cancellable = aFutureInt.sink { int in + print(int) +} +//cancellable.cancel() + +//Subject.init + +let passthrough = PassthroughSubject.init() +let currentValue = CurrentValueSubject.init(2) + +let c1 = passthrough.sink { x in + print("passthrough", x) +} +let c2 = currentValue.sink { x in + print("currentValue", x) +} + +passthrough.send(42) +currentValue.send(1729) +passthrough.send(42) +currentValue.send(1729) diff --git a/0080-combine-and-effects-pt1/Combine.playground/contents.xcplayground b/0080-combine-and-effects-pt1/Combine.playground/contents.xcplayground new file mode 100644 index 00000000..5da2641c --- /dev/null +++ b/0080-combine-and-effects-pt1/Combine.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/0080-combine-and-effects-pt1/README.md b/0080-combine-and-effects-pt1/README.md new file mode 100644 index 00000000..33be784e --- /dev/null +++ b/0080-combine-and-effects-pt1/README.md @@ -0,0 +1,5 @@ +## [Point-Free](https://www.pointfree.co) + +> #### This directory contains code from Point-Free Episode: [The Combine Framework and Effects: Part 1](https://www.pointfree.co/episodes/ep80-the-combine-framework-and-effects-part-1) +> +> Let's explore the Combine framework and its correspondence with the Effect type. Combine introduces several concepts that overlap with how we model effects in our composable architecture. Let's get an understanding of how they work together and compare them to our humble Effect type. diff --git a/README.md b/README.md index 6dea1848..cdb12f11 100644 --- a/README.md +++ b/README.md @@ -82,3 +82,4 @@ This repository is the home of code written on episodes of 1. [Effectful State Management: Unidirectional Effects](0077-effectful-state-management-unidirectional-effects) 1. [Effectful State Management: Asynchronous Effects](0078-effectful-state-management-async-effects) 1. [Effectful State Management: The Point](0079-effectful-state-management-wtp) +1. [The Combine Framework and Effects: Part 1](0080-combine-and-effects-pt1)