Re: [swift-users] Generics question - can you handle both optional and non-optional args with the same function?

2018-01-11 Thread Kenny Leung via swift-users
Yep, it looks like Optional.flatmap() is what I need. Thanks! -Kenny > On Jan 11, 2018, at 6:55 PM, Hooman Mehr wrote: > > Hi, > > Two points: > > 1) What you want to do is a common operation in functional programming called > flatMap. Optional type already supports it. To

Re: [swift-users] Generics question - can you handle both optional and non-optional args with the same function?

2018-01-11 Thread Hooman Mehr via swift-users
Hi, Two points: 1) What you want to do is a common operation in functional programming called flatMap. Optional type already supports it. To get “x != nil ? f(x) : nil” you say: x.flatMap(f) 2) You don’t need multiple versions, because there is a subtype-supertype relationship between

Re: [swift-users] Generics question - can you handle both optional and non-optional args with the same function?

2018-01-11 Thread Charles Srstka via swift-users
> On Jan 11, 2018, at 7:55 PM, Kenny Leung via swift-users > wrote: > > I’m trying to write a utility method that is kind of the opposite of “x ?? > y”, which means “x != nil ? x : y”. I want “x != nil ? f(x) : nil” A good name for this method might be ‘map’ ;-) >

[swift-users] Generics question - can you handle both optional and non-optional args with the same function?

2018-01-11 Thread Kenny Leung via swift-users
Hi All. I’m trying to write a utility method that is kind of the opposite of “x ?? y”, which means “x != nil ? x : y”. I want “x != nil ? f(x) : nil” This is not difficult to write, but if you have "f(x)->z" and "g(x)->z?", I seem to need two versions of the function in order to handle it. Is