ctietze,
@ctietze@mastodon.social avatar

Based on a condition, I want to make sure an element is either part of a set or removed from it.

(Like a toggle, but conditional on a Bool expression, not on the previous value.)

How would you call this?

extension Set {  
 mutating func foo(  
 _ element: Element,  
 insertWhen condition: Bool  
 ) {  
 if condition {  
 insert(element)  
 } else {  
 remove(element)  
 }  
 }  
}  

pyrtsa,

@ctietze Seems useful to define this in form of a subscript so you can form key paths into it (e.g. Binding syntax in SwiftUI!)

extension Set {
subscript(membershipOf element: Element) -> Bool {
get { contains(element) }
set {
if newValue {
insert(element)
} else {
remove(element)
}
}
}
}

chris,
@chris@m.objc.io avatar

@pyrtsa @ctietze I came here to say the same. Literally wrote that same code today.

ctietze,
@ctietze@mastodon.social avatar

@chris @pyrtsa That's a lovely idea!

pyrtsa,

@ctietze @chris Regarding naming, set[contains: element] is another logical argument naming.

CarstenSchridde,
@CarstenSchridde@norden.social avatar

@ctietze useItOrLoseIt

hawkrives,

@ctietze insert(if:), I guess?

teilweise,
@teilweise@layer8.space avatar

@ctietze Maybe something like:

mutating func include(_ element: Element, if condition: Bool)

ctietze,
@ctietze@mastodon.social avatar

@teilweise Thanks!

I like 'include' a bit more than 'insert', because 'insert' is used to, well, insert, not to also remove.

'include' could be interpreted to make sure it's not included otherwise.

misc,
@misc@mathstodon.xyz avatar

@ctietze @teilweise How about “ensure(… if condition:)”?

teilweise,
@teilweise@layer8.space avatar

@misc @ctietze ensure IMO does not say anything about what to do if the condition is false.

Thinking about it again I would probably use

func include(_ element: E, onlyIf condition: Bool)

ctietze,
@ctietze@mastodon.social avatar

@teilweise @misc Yes, "onlyIf" is a great argument label here!

icanzilb,
@icanzilb@mastodon.social avatar

@ctietze func if(_ condition: Bool, insert: Element) 😈

icanzilb,
@icanzilb@mastodon.social avatar

@ctietze oh wait, it removes when it's false? 🤦🏽‍♂️ huh .. maybe func xor(_ condition: Bool, element: Element)

icanzilb,
@icanzilb@mastodon.social avatar

@ctietze hmm ... maybe: insertOrRemove(_ value: Element, dependingOn: Bool) ?

ctietze,
@ctietze@mastodon.social avatar

@icanzilb I actually have that :) But it feels weird. Maybe it is weird? I could also have

element |> condition ? Set.insert : Set.remove |> theSet

Or something, which expresses that I'm just picking the operation depending on the condition. But the code base doesn't have that, sooo

mattgemmell,
@mattgemmell@mastodon.scot avatar

@ctietze I suppose the semantics of this could be described as validation, or more loosely as requirement.

ctietze,
@ctietze@mastodon.social avatar

@mattgemmell Oh sure, "validation" does make sense in the way I learned the word from AppKit!

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • kavyap
  • thenastyranch
  • GTA5RPClips
  • tester
  • InstantRegret
  • DreamBathrooms
  • ngwrru68w68
  • magazineikmin
  • everett
  • Youngstown
  • mdbf
  • slotface
  • rosin
  • cisconetworking
  • megavids
  • khanakhh
  • normalnudes
  • osvaldo12
  • cubers
  • tacticalgear
  • Durango
  • ethstaker
  • modclub
  • anitta
  • provamag3
  • Leos
  • JUstTest
  • lostlight
  • All magazines