{"id":16754663,"url":"https://github.com/capnslipp/with","last_synced_at":"2025-03-21T22:32:18.085Z","repository":{"id":56927579,"uuid":"245455629","full_name":"capnslipp/With","owner":"capnslipp","description":"with(…) { … } Statement (a Swift µ-Library)","archived":false,"fork":false,"pushed_at":"2024-11-16T20:39:29.000Z","size":58,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T05:51:17.916Z","etag":null,"topics":["context","operator","operators","scope","swift","swift-library","utilities","utility"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/capnslipp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-06T15:34:41.000Z","updated_at":"2024-11-16T20:39:32.000Z","dependencies_parsed_at":"2024-09-12T04:21:04.760Z","dependency_job_id":null,"html_url":"https://github.com/capnslipp/With","commit_stats":{"total_commits":33,"total_committers":2,"mean_commits":16.5,"dds":0.1515151515151515,"last_synced_commit":"a6bbc5b71bff002a9a04caf33a313b891f056e62"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/capnslipp%2FWith","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/capnslipp%2FWith/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/capnslipp%2FWith/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/capnslipp%2FWith/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/capnslipp","download_url":"https://codeload.github.com/capnslipp/With/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244880448,"owners_count":20525507,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["context","operator","operators","scope","swift","swift-library","utilities","utility"],"created_at":"2024-10-13T03:05:31.769Z","updated_at":"2025-03-21T22:32:17.537Z","avatar_url":"https://github.com/capnslipp.png","language":"Swift","readme":"# With\n\nWith is a Swift micro-library that provides a `with` statement— _which is modeled after the `with` functionality found in Python, JavaScript, Visual Basic, Object Pascal, Delphi; `using` found in C#; and `also`/`let` found in Kotlin._\n\nWith provides a set of overloaded generic free functions that are useful for:\n\n* Object or value initialization and setup in a declarative style _(think Swift UI's hierarchical style, but for anything, anywhere)_.\n* Performing multiple operations on an object/value fetched via a method/property, while staying D.R.Y and without needing to create a local var _(while still avoiding invoking the method/getter repeatedly)_.\n* Performing calculations with an object/value that only needs to live long enough to be configured and do some calc _(and you're only interested in a result object/value)_.\n\nWith provides a free function `func with(_ subject: SubjectT, operations: (inout SubjectT) -\u003e Void) -\u003e SubjectT` that can be used on any object or value type to do stuff like this:\n\n```swift\n// initializes a value-type `hitTestOptions` dictionary  for use with\n//   `SCNView`'s `hitTest(…)` with the desired options some of which have\n//   changed in newer OS versions (which the standard dictionary literal syntax\n//   can't cleanly do)\nlet hitTestOptions = with([SCNHitTestOption : Any]()) {\n\t$0[.boundingBoxOnly] = true\n\t$0[.rootNode] = _myRootNode\n\tif #available(iOS 11.0, tvOS 11.0, macOS 10.13, *) {\n\t\t$0[.searchMode] = SCNHitTestSearchMode.all.rawValue\n\t} else {\n\t\t$0[.sortResults] = true\n\t\t$0[.firstFoundOnly] = false\n\t}\n}\n```\n\nOr like this:\n\n```swift\n// initializes the object-type `newButton` with title, sizing, styling, etc.\n//   and adds the view to a superview\nlet newButton = with(UIButton(type: .system)) {\n\t$0.titleLabel!.font = .systemFont(ofSize: 13)\n\t$0.setTitle(\"My Button\", for: .normal)\n\t$0.autoresizingMask = [ .flexibleLeftMargin, .flexibleBottomMargin ]\n\t$0.contentEdgeInsets = UIEdgeInsets(top: 6.5, left: 7, bottom: 6.5, right: 7)\n\twith($0.layer) { layer in\n\t\tlayer.borderWidth = 1.0\n\t\tlayer.borderColor = UIColor.white.cgColor\n\t\tlayer.cornerRadius = 5\n\t}\n\t$0.sizeToFit()\n\t\n\trootViewController.view.addSubview($0)\n}\n```\n\nWith also has an alternate `func withMap(_ subject: SubjectT, operations: (inout SubjectT) -\u003e ReturnT) -\u003e SubjectT` form that can return an arbitrary value from the closure (instead of the value passed in):\n\n```swift\n// initializes a `DateFormatter`, configures it, and uses it to calculate a\n//   `String` which is the only thing we want to hang onto\nlet dateString = withMap(DateFormatter()) {\n\t$0.dateStyle = .medium\n\t$0.timeStyle = .none\n\t$0.locale = Locale(identifier: \"en_US\")\n\t\n\tlet currentDate = Date()\n\treturn $0.string(from: currentDate)\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcapnslipp%2Fwith","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcapnslipp%2Fwith","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcapnslipp%2Fwith/lists"}