Last active
February 11, 2021 21:22
-
-
Save 23inhouse/835ba8ebe8117078069420bc407e1d7a to your computer and use it in GitHub Desktop.
Swift refactoring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TL;DR: look at this cool thing in swift | |
let ibsTags = try ibsTagRecords().reduce(into: [Int64: [Int64]]()) { | |
// this line here | |
$0[$1.ibsID, default: []].append($1.tagID) | |
} | |
// V1 original | |
if $0[$1.ibsID] == nil { | |
$0[$1.ibsID] = [$1.tagID] | |
} else { | |
$0[$1.ibsID]?.append($1.tagID) | |
} | |
// V2 better than ruby's ||= | |
if $0[$1.ibsID]?.append($1.tagID) == nil { | |
$0[$1.ibsID] = [$1.tagID] | |
} | |
// v3 the ultimate in readablity in only the way swift can | |
if $0[$1.ibsID]?.append($1.tagID) == nil { $0[$1.ibsID] = [$1.tagID] } | |
// v4 or the swift way | |
$0[$1.ibsID, default: []].append($1.tagID) | |
// This is the old way pre swift 4.1 | |
$0[$1.ibsID] = ($0[$1.ibsID] ?? []) + $1.tagID |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment