Skip to content

Commit

Permalink
Update package
Browse files Browse the repository at this point in the history
  • Loading branch information
pridees committed Aug 26, 2023
1 parent a35f5e4 commit fd92da5
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 63 deletions.
17 changes: 6 additions & 11 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -7,20 +7,15 @@ let package = Package(
name: "Composer",
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(name: "Composer", targets: ["Composer"]),
.library(name: "ComposerCombine", targets: ["ComposerCombine"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
dependencies: [],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(name: "Composer", dependencies: []),
.testTarget(name: "ComposerTests", dependencies: ["Composer"]),

.target(name: "ComposerCombine", dependencies: [.init(stringLiteral: "Composer")]),
]

.target(name: "ComposerCombine", dependencies: [.init(stringLiteral: "Composer")]),
],
swiftLanguageVersions: [.v5]
)
153 changes: 148 additions & 5 deletions Playground.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,151 @@
import Foundation
import Combine
import Composer
import UIKit

let str = ["1", "2", "3", "4"]
//indirect enum List<T> {
// case empty
// case cons(head: T, tail: List<T>)
//}
//
//func listOf<T>(_ t: T...) -> List<T> {
// func go(array: [T]) -> List<T> {
// if array.isEmpty {
// return .empty
// } else {
// return .cons(
// head: array[0],
// tail: go (array: Array (array.dropFirst ()))
// )
// }
// }
// return go(array: t)
//}
//
//extension List {
// func head() -> T? {
// switch self {
// case .empty: return .none
// case .cons(head: let h, tail: _): return .some(h)
// }
// }
//}
//
//extension List {
// func drop (n: Int) -> List<T> {
// switch self {
// case .empty: return self
// case .cons (head: _, tail: let t):
// if n == 0 {
// return self
// } else {
// return t.drop (n: n-1)
// }
// }
// }
// func dropWhile(_ f: (T) -> Bool) -> List<T> {
// switch self {
// case .empty: return self
// case .cons (head: let h, tail: let t):
// if f(h) {
// return t.dropWhile(f)
// } else {
// return self
// }
// }
// }
//
// func append(_ n: T) -> List<T> {
// switch self {
// case .empty:
// return .cons (head: n, tail: .empty)
// case .cons(head: let h, tail: let t):
// return .cons (head: h, tail: t.append (n))
// }
// }
//
// func foldLeft<C> (_ initialValue: C, _ f: (C, T) -> C) -> C {
// switch self {
// case .empty:
// return initialValue
// case .cons(head: let h, tail: let t):
// return t.foldLeft(f(initialValue, h), f)
// }
// }
//
// func foldRight<B> (_ initialValue: B, _ f: (T, B) -> B) -> B {
// switch self {
// case .empty:
// return initialValue
// case .cons(head: let h, tail: let t):
// return f(h, t.foldRight(initialValue, f))
// }
// }
//
// func appendInTermsFoldRight(_ n: T) -> List<T> {
// foldRight(listOf(n)) { t, c in
// return .cons(head: t, tail: c)
// }
// }
//
// func appendInTermsFoldLeft(_ n: T) -> List<T> {
// foldLeft(listOf(n)) { c, t in
// switch c {
// case .empty: return .empty
// case .cons(let head, let tail):
// return .cons(
// head: head,
// tail: tail.appendInTermsFoldLeft(t)
// )
// }
// }
// }
//
// func print() {
// Swift.print(self.foldLeft(Array<T>.init()) { b, t in
// return b + [t]
// })
// }
//}
//
//var list = List.cons(
// head: 10,
// tail: .cons(
// head: 20,
// tail: .cons(
// head: 30,
// tail: .empty
// )
// )
//)
//
//list = list.appendInTermsFoldLeft(0)
//
//list.print()

str |> join(with: " ")

import Foundation
import os

final class Atomic<A> {

private let accessQueue = DispatchQueue(label: "ru.tinkoff.accessQueue")
private var _value: A

init(_ value: A) {
self._value = value
}

var value: A {
self._value
}

func mutate(_ work: @escaping (A) -> A) {
accessQueue.sync {
self._value = work(value)
}
}
}

var x = Atomic(0)
DispatchQueue.concurrentPerform(iterations: 100) { _ in
x.mutate { $0 + 1 }
}
print(x.value)
10 changes: 10 additions & 0 deletions Playground.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/alexo/Developer/Personal/swift-composer/Playground.playground#CharacterRangeLen=527&amp;CharacterRangeLoc=3178&amp;EndingColumnNumber=0&amp;EndingLineNumber=151&amp;StartingColumnNumber=12&amp;StartingLineNumber=121&amp;Timestamp=689514764.655509"
selectedRepresentationIndex = "0">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
1 change: 1 addition & 0 deletions Sources/Composer/Core/Alternative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ precedencegroup AlternativePrecedence {

infix operator <|> : AlternativePrecedence


public func <|> <A> (_ a: A?, _ alt: @autoclosure @escaping () -> A) -> A {
return a ?? alt()
}
36 changes: 0 additions & 36 deletions Sources/Composer/Core/Applicative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,8 @@ public func |> <T1, T2>(_ t1: T1, _ f: @escaping (T1) -> T2) -> T2 {
return f(t1)
}

@inlinable
public func |> <T1, T2>(_ t1: T1, _ f: @escaping (T1) throws -> T2) rethrows -> T2 {
return try f(t1)
}

@inlinable
public func |> <T1>(_ t1: inout T1, _ f: @escaping (inout T1) throws -> Void) rethrows -> T1 {
try f(&t1)
return t1
}

@inlinable
@discardableResult
public func |> <T1: AnyObject>(_ t1: T1, _ f: @escaping (T1) throws -> Void) rethrows -> T1 {
try f(t1)
return t1
}

/// MARK: Inverted
@inlinable
public func <| <T1, T2>(_ f: @escaping (T1) -> T2, _ t1: T1) -> T2 {
return f(t1)
}

@inlinable
public func <| <T1, T2>( _ f: @escaping (T1) throws -> T2, _ t1: T1) throws -> T2 {
return try f(t1)
}

@inlinable
public func <| <T1>(_ f: @escaping (inout T1) throws -> Void, _ t1: inout T1) rethrows -> T1 {
try f(&t1)
return t1
}

@inlinable
@discardableResult
public func <| <T1: AnyObject>(_ f: @escaping (T1) throws -> Void, _ t1: T1) rethrows -> T1 {
try f(t1)
return t1
}
12 changes: 10 additions & 2 deletions Sources/Composer/Core/Monoid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@

import Foundation

// MARK: - Monoid Definition

/// Monoid procotol declaration
///
/// Reducing extra syntax
///
/// ## Usage
/// ```
/// extention Int: Monoid {
/// static var empty: Self { 0 }
/// }
/// ```
public protocol Monoid: Semigroup {
static var empty: Self { get }
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/Composer/Core/OptionalChaining.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ precedencegroup OptionalChainingPrecedence {

infix operator <?> : OptionalChainingPrecedence

/// Optional chaingin operator
///
/// ## Usage
///
public func <?> <A, B, C> (_ a2b: @escaping (A) -> B, _ b2c: @escaping (B) -> C) -> (A?) -> C? {
return { $0
.map(a2b)
Expand Down
84 changes: 75 additions & 9 deletions Sources/ComposerCombine/Sink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,96 @@ import Foundation
import Combine

public func fireOnce<P, T>(
_ completion: @escaping (T) -> Void
_ receiveValue: @escaping (T) -> Void
) -> (P) -> Void where P: Publisher, P.Output == T, P.Failure == Never {
return { publisher in
var subsciption: AnyCancellable?
subsciption = publisher
.handleEvents(receiveCompletion: { _ in
.sink(receiveValue: {
receiveValue($0)
subsciption?.cancel()
subsciption = nil
})
.sink(receiveValue: completion)
}
}

public func fireAndForget<P, E>(
_ completion: @escaping (Subscribers.Completion<P.Failure>) -> Void
) -> (P) -> Void where P: Publisher, P.Output == Void, P.Failure == E {
public func fireOnce<P, T, E>(
_ recieveValue: @escaping (T) -> Void,
_ recieveError: ((P.Failure) -> Void)? = nil
) -> (P) -> Void where P: Publisher, P.Output == T, P.Failure == E {
return { publisher in
var subsciption: AnyCancellable?
subsciption = publisher
.handleEvents(receiveCompletion: { _ in
.sink(
receiveCompletion: { competion in
switch(competion) {
case let .failure(error):
recieveError?(error)
fallthrough
case .finished:
subsciption?.cancel()
subsciption = nil
}
},
receiveValue: {
recieveValue($0)
subsciption?.cancel()
subsciption = nil
}
)
}
}

public func fireAndForget<P, E>(_ publisher: P) -> Void where P: Publisher, P.Failure == E {
var subsciption: AnyCancellable?
subsciption = publisher
.ignoreOutput()
.sink(
receiveCompletion: { _ in
subsciption?.cancel()
subsciption = nil
})
.sink(receiveCompletion: completion, receiveValue: { _ in })
},
receiveValue: { _ in }
)
}

public func fireAndForget<P, E>(
_ completion: @escaping (Subscribers.Completion<P.Failure>) -> Void
) -> (P) -> Void where P: Publisher, P.Failure == E {
return { publisher in
var subsciption: AnyCancellable?
subsciption = publisher
.ignoreOutput()
.sink(
receiveCompletion: {
subsciption?.cancel()
subsciption = nil
completion($0)
},
receiveValue: { _ in }
)
}
}

public func run<P, T, E>(
_ recieveValue: @escaping (T) -> Void,
_ recieveError: ((P.Failure) -> Void)? = nil
) -> (P) -> Void where P: Publisher, P.Output == T, P.Failure == E {
return { publisher in
var subsciption: AnyCancellable?
subsciption = publisher
.sink(
receiveCompletion: { competion in
switch(competion) {
case .finished:
subsciption?.cancel()
subsciption = nil
case let .failure(error):
recieveError?(error)
}
},
receiveValue: recieveValue
)
}
}

Expand Down

0 comments on commit fd92da5

Please sign in to comment.