Skip to content

Infix operator version #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/Wrap.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1330"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Wrap"
BuildableName = "Wrap"
BlueprintName = "Wrap"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WrapTests"
BuildableName = "WrapTests"
BlueprintName = "WrapTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WrapTests"
BuildableName = "WrapTests"
BlueprintName = "WrapTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Wrap"
BuildableName = "Wrap"
BlueprintName = "Wrap"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let package = Package(
],
targets: [
.target(name: "Wrap", dependencies: []),
.testTarget(name: "WrapTests", dependencies: ["Wrap"])
],
swiftLanguageVersions: [.v5]
)
66 changes: 33 additions & 33 deletions Sources/Wrap/Wrap.swift
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@

postfix operator &>

public postfix func &> <T>(argument: T) -> Wrap<T> {
.init(argument)
precedencegroup WrapPrecedence {
associativity: left
}

public struct Wrap<Value> {

public let value: Value
infix operator &>: WrapPrecedence
infix operator <&: WrapPrecedence

public init(_ value: Value) {
self.value = value
}

public func <& <T, O>(lhs: inout T, modifier: WrapModifier<T, O>) -> O {
modifier.modify(&lhs)
}

public func modify<Value>(_ value: inout Value, _ modifier: (inout Value) -> Void) {
modifier(&value)
@discardableResult
public func &> <T, O>(lhs: T, applier: WrapApplier<T, O>) -> O {
applier.apply(lhs)
}

extension Wrap {

public func map<U>(_ transform: (Value) throws -> U) rethrows -> U {
try transform(value)
}

@discardableResult
public func `do`(_ applier: (Value) throws -> Void) rethrows -> Value where Value : AnyObject {
try applier(value)
return value
public struct WrapModifier<Input, Output> {

public let modify: (inout Input) -> Output

public init(_ modify: @escaping (inout Input) -> Output) {
self.modify = modify
}

public func modify(_ modifier: (inout Value) throws -> Void) rethrows -> Value {
var v = value
try modifier(&v)
return v
public static func modify(_ modify: @escaping (inout Input) -> Void) -> WrapModifier<Input, Void> {
return .init {
modify(&$0)
}
}

}

public func filter(_ filter: (Value) -> Bool) -> Value? {
guard filter(value) else {
return nil
public struct WrapApplier<Input, Output> {

public let apply: (Input) -> Output

public init(_ apply: @escaping (Input) -> Output) {
self.apply = apply
}

public static func `do`(_ applier: @escaping (Input) -> Void) -> WrapApplier<Input, Input> {
return .init {
applier($0)
return $0
}
return value
}

}
23 changes: 23 additions & 0 deletions Tests/WrapTests/Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import XCTest

import Wrap

final class Tests: XCTestCase {

func testApplier() {

"1" &> .do {
XCTAssertEqual($0, "1")
}

var text = ""

text <& .modify {
$0 = "muuk"
}

XCTAssertEqual(text, "muuk")

}

}