Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-27 Thread Tino Heth via swift-evolution
> I wrote about this in 2015 > http://alejandromp.com/blog/2015/6/24/swift-flatmap/ > and forgot about > it. That text has a line which illustrates the source of dissent: > It’s kind of combining map (to apply the function), flatten (to unwr

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-27 Thread Tino via swift-evolution
> I wrote about this in 2015 > http://alejandromp.com/blog/2015/6/24/swift-flatmap/ > and forgot about > it. That text has a line which illustrates the source of dissent: > It’s kind of combining map (to apply the function), flatten (to unwr

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-27 Thread David Waite via swift-evolution
I'm not attached to the name filteredMap, but having a specific signature for the optional version makes sense. I think the problem is that while Optional can be contemplated as a sequence of zero or one items, implicit conversion to Optional means anything can then be interpreted as a collect

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-26 Thread Alejandro Martinez via swift-evolution
I wrote about this in 2015 http://alejandromp.com/blog/2015/6/24/swift-flatmap/ and forgot about it. I just get used to it now but it's still hard to explain to newcomers why flatMap has this "special" behaviour on Swift. I'm not an expert on functional programming but if this doesn't make sense in

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-25 Thread Max Moiseev via swift-evolution
> On Oct 25, 2017, at 2:15 AM, Tino <2...@gmx.de> wrote: > > >>> It’s unfortunate when a piece of code gets a different meaning — but as you >>> said: >>> It’s a misuse, and when you write code that is wrong, the results might be >>> wrong as well. >> And this is exactly that we’re trying to

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-25 Thread Tino via swift-evolution
>> It’s unfortunate when a piece of code gets a different meaning — but as you >> said: >> It’s a misuse, and when you write code that is wrong, the results might be >> wrong as well. > And this is exactly that we’re trying to address. Not the impurity of the > function, not the performance, bu

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-24 Thread Max Moiseev via swift-evolution
> On Oct 24, 2017, at 11:36 AM, Tino <2...@gmx.de> wrote: > > >>> If the biggest problem of flatMap is that people who don’t understand it >>> write code that isn’t optimal (but still does the right thing!), I don’t >>> think there is any change needed. I’m not even sure that that >>> wrappi

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-24 Thread Tino via swift-evolution
>> If the biggest problem of flatMap is that people who don’t understand it >> write code that isn’t optimal (but still does the right thing!), I don’t >> think there is any change needed. I’m not even sure that that >> wrapping/unwrapping is actually done, because it should be discoverable by

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-24 Thread Max Moiseev via swift-evolution
> On Oct 23, 2017, at 8:38 PM, Howard Lovatt wrote: > > +1 good idea. > > Re. the naming I would suggest `mapFilterNil` since it says what it does and > filter, nil, and map are all understood already in Swift. (I have sympathy > for people wanting `mapFilteringNil`, but Swift chose `filter`

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-24 Thread Max Moiseev via swift-evolution
> On Oct 24, 2017, at 5:10 AM, Tino <2...@gmx.de> wrote: > > >> Calling "flatMap" a map + filtering out the nil values was a TERRIBLE idea. > maybe I’m the one who’s getting this all wrong, but afaik the purpose of > flatMap isn’t filtering out nil at all, but just a combination of map & > fl

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-24 Thread Matthew Johnson via swift-evolution
> On Oct 24, 2017, at 1:57 AM, David Hart via swift-evolution > wrote: > > I also cast my vote for filterMap. It’s concise, understandable and is kind > of a term of art also. +1. This seems like the best option to me. > >> On 24 Oct 2017, at 02:56, BJ Homer via swift-evolution >> mailto:

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-24 Thread Elviro Rocca via swift-evolution
"Flattening" in the context of functional programming (and that's where map and flatMap come from) means something like this: F> -> F So for example: Array> -> Array or Optional> -> Optional but NOT: Array> -> Array It's simply the wrong word for the operation. If "Map" is something like

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-24 Thread Tino via swift-evolution
> Calling "flatMap" a map + filtering out the nil values was a TERRIBLE idea. maybe I’m the one who’s getting this all wrong, but afaik the purpose of flatMap isn’t filtering out nil at all, but just a combination of map & flatten… Optional is like Array with a maximal capacity of one (so it can

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-24 Thread Adrian Kashivskyy via swift-evolution
+1 from me as well. —— Adrian Kashivskyy On 24 Oct 2017, 00:24 +0200, BJ Homer via swift-evolution , wrote: > I strongly agree! In fact, I just started writing up a similar proposal the > other day, but hadn’t had time to finish it yet. > > The current name for this particular filtering variant

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-24 Thread Elviro Rocca via swift-evolution
Huge +1 Calling "flatMap" a map + filtering out the nil values was a TERRIBLE idea. Elviro > Il giorno 24 ott 2017, alle ore 00:15, Max Moiseev via swift-evolution > ha scritto: > > Hi swift-evolution! > > I would like to propose the following change to the standard library: > > deprecate

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-23 Thread David Hart via swift-evolution
I also cast my vote for filterMap. It’s concise, understandable and is kind of a term of art also. > On 24 Oct 2017, at 02:56, BJ Homer via swift-evolution > wrote: > > I agree with Xiaodi; I like ‘filterMap’ more than ‘filteredMap’. But both are > superior to ‘flatMap’ in this context. > >

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-23 Thread Nobuo Saito via swift-evolution
Hi there, In my opinion, `map` vs `flatMap` confusing comes from implicitly casting `T` to `T?`. I believe it make many confusing and problems that compiler allow this implicitly casting. 1 way of solve this topic that show warning if use implicitly casting `T` to `T?`. Of course it should be expl

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-23 Thread Thorsten Seitz via swift-evolution
Well, I have no problems with `flatMap` but then I am used to Monads. I would not rename that overload if there is any hope that we might get Monads sometime in the future but probably there is none (which is sad). So I would suggest `mapDroppingNil` or maybe `mapRejectingNil`. -Thorsten > Am

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-23 Thread Xiaodi Wu via swift-evolution
Well, in Swift as in other languages, to filter means to keep the stuff that matches the predicate. So it's not "filter nil" but "filter some." But actually, it's not a filter; it's a flat map operation, since the optional is being unwrapped. Also, based on the precedent of "flat map," I think it

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-23 Thread Howard Lovatt via swift-evolution
+1 good idea. Re. the naming I would suggest `mapFilterNil` since it says what it does and filter, nil, and map are all understood already in Swift. (I have sympathy for people wanting `mapFilteringNil`, but Swift chose `filter`.) The problems I see with `filterMap` are that: 1. It sounds like

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-23 Thread BJ Homer via swift-evolution
I agree with Xiaodi; I like ‘filterMap’ more than ‘filteredMap’. But both are superior to ‘flatMap’ in this context. -BJ > On Oct 23, 2017, at 5:22 PM, Max Moiseev wrote: > > It occurred to me that filteringMap(_:) should be even more descriptive, > still conform to the guidelines, although s

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-23 Thread Max Moiseev via swift-evolution
It occurred to me that filteringMap(_:) should be even more descriptive, still conform to the guidelines, although similarly unprecedented and un-googlable. Max > On Oct 23, 2017, at 3:52 PM, Xiaodi Wu wrote: > > +1 in general. As to the name: since 'map' is used as a term of art, > 'filterMa

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-23 Thread Xiaodi Wu via swift-evolution
On Mon, Oct 23, 2017 at 6:11 PM, Braden Scothern wrote: > The reason `flatMap(_:)` has its name is for code like this: > > ``` > let nestedArray = [[1, 2, 3], [4, 5, 6]] > let flatArray = nestedArray.flatMap { > $0 > } > > print(flatArray) // prints [1, 2, 3, 4, 5, 6] > >

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-23 Thread Braden Scothern via swift-evolution
The reason `flatMap(_:)` has its name is for code like this: ``` let nestedArray = [[1, 2, 3], [4, 5, 6]] let flatArray = nestedArray.flatMap { $0 } print(flatArray) // prints [1, 2, 3, 4, 5, 6] let anotherNestedArrays = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] let an

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-23 Thread Xiaodi Wu via swift-evolution
+1 in general. As to the name: since 'map' is used as a term of art, 'filterMap' seems superior to 'filteredMap', which half follows naming guidelines and half is a term of art; neither is immediately comprehensible but 'filterMap' can be googled and has precedents in other languages. On Mon, Oct 2

Re: [swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-23 Thread BJ Homer via swift-evolution
I strongly agree! In fact, I just started writing up a similar proposal the other day, but hadn’t had time to finish it yet. The current name for this particular filtering variant is not particularly descriptive. It’s certainly not obvious to newcomers that ‘flatMap’ will filter out results. An

[swift-evolution] [draft] Introduce Sequence.filteredMap(_:)

2017-10-23 Thread Max Moiseev via swift-evolution
Hi swift-evolution! I would like to propose the following change to the standard library: deprecate `Sequence.flatMap(_: (Element) -> U?) -> [U]` and make this functionality available under a new name `Sequence.filteredMap(_:)`. The draft is available at https://gist.github.com/moiseev/2f36376