<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">I tend to look at Sequence.map (and the like) as an optimized shortcut for</div><div class=""><br class=""></div><div class="">sequence.lazy.map(mappingFn).array</div><div class=""><br class=""></div><div class="">and where array, count maxElement, etc are optimized shortcuts for reductions</div><div class=""><br class=""></div><div class="">In that light, I don’t know if other collection types are needed for optimized forms. I might prefer promoting the use of reductions instead.</div><div class=""><br class=""></div><div class="">For instance, imagine I have a Set of employees, and I map based on wage. I want the flexibility for the behavior to be:</div><div class="">- Create a new Set, collapsing duplicate wages</div><div class="">- Create a new Array, allowing for duplicate wages</div><div class="">- Create a new Set, failing if two employees have the same wage (for some definition of failing)</div><div class="">- something else</div><div class=""><br class=""></div><div class="">examples of promoting the use of reductions would be supporting mutable reduction, and possibly having a protocol for reducers as types (example that springs to mind is java’s stream Collector)</div><div class=""><br class=""></div><div class="">-DW</div><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 20, 2016, at 10:04 PM, Howard Lovatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">RE: map etc. returning Array<div class=""><br class=""></div><div class="">You could currently return AnyCollection though, and that would then let Optional.map also return AnyCollection.</div><div class=""><br class=""></div><div class="">There is also talk in completing generics so that map could return Any&lt;CollectionType where Element == Element&gt; which would be even better than AnyCollection since a wrapper is removed.</div></div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class="gmail_signature">&nbsp; -- Howard.<br class=""></div></div>
<br class=""><div class="gmail_quote">On 15 March 2016 at 12:29, Dmitri Gribenko via swift-evolution <span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Mon, Mar 14, 2016 at 5:51 PM,&nbsp; &lt;<a href="mailto:davesweeris@mac.com" class="">davesweeris@mac.com</a>&gt; wrote:<br class="">
&gt;<br class="">
&gt; On Mar 14, 2016, at 5:51 PM, Dmitri Gribenko via swift-evolution<br class="">
&gt; &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">
&gt;<br class="">
&gt; Optional.map returns an Optional.<br class="">
&gt;<br class="">
&gt; Array.map returns an Array.<br class="">
&gt; Set.map returns an Array.<br class="">
&gt; &lt;any other collection&gt;.map returns an Array.<br class="">
&gt;<br class="">
&gt; I can't say that it is not valid to think about an Optional as a tiny<br class="">
&gt; collection, but as implemented in Swift, .map() does objectively<br class="">
&gt; behave differently...<br class="">
&gt;<br class="">
&gt; That behavior is, at least partially, because protocols don’t currently<br class="">
&gt; support this:<br class="">
&gt; protocol CollectionType {<br class="">
&gt;&nbsp; &nbsp; &nbsp;typealias T<br class="">
&gt;&nbsp; &nbsp; &nbsp;func map&lt;U&gt;(transform: T-&gt;U) -&gt; Self&lt;U&gt; // error: Cannot specialize<br class="">
&gt; non-generic type 'Self'<br class="">
&gt; }<br class="">
<br class="">
</span>This feature is called higher-kinded types, and it is not planned for<br class="">
design or implementation in the near future.<br class="">
<span class=""><br class="">
&gt; I *think* I remember reading on here somewhere that the intent is to change<br class="">
&gt; map and flatMap to return “Self&lt;U&gt;" pretty much as soon as the language<br class="">
&gt; supports it.<br class="">
<br class="">
</span>As a standard library engineer I can with certainty that this is not<br class="">
the plan.&nbsp; We like the current formulation of map() that returns an<br class="">
Array.<br class="">
<br class="">
<a href="https://github.com/apple/swift/blob/master/docs/StdlibRationales.rst#high-order-functions-on-collections-return-arrays" rel="noreferrer" target="_blank" class="">https://github.com/apple/swift/blob/master/docs/StdlibRationales.rst#high-order-functions-on-collections-return-arrays</a><br class="">
<br class="">
I don't think that having HKTs in the language would change the rationale.<br class="">
<div class="HOEnZb"><div class="h5"><br class="">
Dmitri<br class="">
<br class="">
--<br class="">
main(i,j){for(i=2;;i++){for(j=2;j&lt;i;j++){if(!(i%j)){j=0;break;}}if<br class="">
(j){printf("%d\n",i);}}} /*Dmitri Gribenko &lt;<a href="mailto:gribozavr@gmail.com" class="">gribozavr@gmail.com</a>&gt;*/<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</div></div></blockquote></div><br class=""></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>