<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br>Thanks Brent. Was that real code? :-)</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">If it was, I did not realize it was possible to do something like that in Swift. That seems very cool, attractive and powerful. It certainly would look much better with the sugary syntax. </div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">I can imagine building a collection of sort orderings like this for example. </div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">// Order by lastName asc, firstName asc</div><div id="AppleMailSignature">let orderings = Person.lastName.asc().then(.firstName.asc())</div><div><br></div><div>Don't know if it would be possible but I think it would be nice to be able to do it. The asc() method would create an ascending sort ordering and would establish Root as Person. I wonder if it would be possible to have the then() know that Root is person so that its argument can be a key path relative to Person. Or maybe this can be expressed differently if this is not the right way. </div><div><br></div><div>Anyways, the sugary syntax looks very attractive IMHO. </div><div><br></div><div>This is what my Java code looks like for something like that:</div><div><br></div><div>NSArray<EOSortOrdering> orderings = Person.LAST_NAME.asc().then(Person.FIRST_NAME.asc());</div><div><br></div><div>or this:</div><div><br></div><div><span style="background-color: rgba(255, 255, 255, 0);">NSArray<EOSortOrdering> orderings = </span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> new NSArray<>(</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> Person.LAST_NAME.asc(),</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> Person.FIRST_NAME.asc()</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"> );</span></div><div><br></div><div><br></div><div>On Apr 1, 2017, at 10:24 PM, Brent Royal-Gordon <<a href="mailto:brent@architechies.com">brent@architechies.com</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><br class=""><div><blockquote type="cite" class=""><div class="">On Apr 1, 2017, at 5:56 PM, Karl Wagner via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class=""><div class=""><font face="Monaco" class="" style="font-size: 12px;">let isPuppyQualifier = Pet.type.equals(.dog).and(Pet.age.lessThan(12))</font></div><div class=""><font face="Monaco" class="" style="font-size: 12px;">let familyQualifier = Family.pets.hasAtLeastOne(satisfying: isPuppyQualifier)</font></div><div class=""><div class=""><font face="Monaco" class="" style="font-size: 12px;">let familiesWithPuppies = Family.fetch(editingContext, familyQualifier)</font></div></div><div class=""><div class=""><br class=""></div></div><div class="">For those unfamiliar with EOF, the<span class="Apple-converted-space"> </span><font face="Monaco" class=""><span class="" style="font-size: 12px;">editingContext</span></font> in the code above is an<span class="Apple-converted-space"> </span><font face="Monaco" class=""><span class="" style="font-size: 12px;">EOEditingContext</span></font><span class="Apple-converted-space"> </span>which is analogous to<span class="Apple-converted-space"> </span><font face="Monaco" class=""><span class="" style="font-size: 12px;">NSManagedObjectContext</span></font>. </div></div></div></div></div></blockquote><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"></div><div class="">Theoretically, you could do something like that with this proposal...</div><div class=""><br class=""></div><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><font face="Courier" class="">struct UnaryPredicate<Parameter, Result> {</font></div><div class=""><font face="Courier" class=""> let evaluate: (Parameter) -> Result</font></div><div class=""><font face="Courier" class="">}</font></div><div class=""><div class=""><font face="Courier" class="">struct BinaryPredicate<Left, Right, Result> {</font></div></div><div class=""><div class=""><font face="Courier" class=""> let evaluate: (Left, Right) -> Result</font></div></div><div class=""><div class=""><font face="Courier" class="">}</font></div></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">extension KeyPath where Value: Equatable {</font></div><div class=""><font face="Courier" class=""> func equals(_ value: Value) -> UnaryPredicate<Root, Bool> {</font></div><div class=""><font face="Courier" class=""> return UnaryPredicate { $0[keyPath: self] == value }</font></div><div class=""><font face="Courier" class=""> }</font></div><div class=""><div class=""><font face="Courier" class=""> func equals<KP: KeyPath>(_ other: KP) -> BinaryPredicate<Root, KP.Root, Bool> where KP.Value == Value {</font></div></div><div class=""><div class=""><font face="Courier" class=""> return BinaryPredicate { $0[keyPath: self] == $1[keyPath: other] }</font></div></div><div class=""><div class=""><font face="Courier" class=""> }</font></div></div><div class=""><font face="Courier" class="">}</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">let isDog = #keypath(Pet, .type).equals(.dog) // UnaryPredicate<Pet, Bool></font></div><div class=""><font face="Courier" class="">if isDog.evaluate(somePet) {</font></div><div class=""><font face="Courier" class=""> print(“It’s a dog”)</font></div><div class=""><font face="Courier" class="">}</font></div><div class=""><br class=""></div><div class=""><font face="Courier" class="">let areSameLength = #keypath(Array<Int>, .count).equals(#keypath(Array<String>, .count))</font></div><div class=""><span class="" style="font-family: Courier;">// BinaryPredicate<Array<Int>, Array<String>, Bool></span></div><div class=""><font face="Courier" class="">if areSameLength.evaluate([1,2,3], [“a”, “b”, “c”]) {</font></div><div class=""><font face="Courier" class=""> print(“same lengths”)</font></div><div class=""><font face="Courier" class="">}</font></div></blockquote></div></div></blockquote></div><div class=""><br class=""></div><div class="">You guys aren't thinking big enough.</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// This implementation is closure-based because it's too complex for an email even *with* </div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// generics system upgrades. Without type system upgrades, things get *really* complicated, </div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// though probably still tractable.</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>typealias Predicate<Element> = (Element) -> Bool</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span></div><div class=""><div><span class="Apple-tab-span" style="white-space: pre;">        </span>func == <Root, Value: Equatable> (lhs: KeyPath<Root, Value>, rhs: Value) -> Predicate<Root> {</div><div><span class="Apple-tab-span" style="white-space: pre;">                </span>return { $0[keyPath: lhs] == rhs }</div></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><div><span class="Apple-tab-span" style="white-space: pre;">        </span></div></div><div><div class=""><div><span class="Apple-tab-span" style="white-space: pre;">        </span>func < <Root, Value: Comparable> (lhs: KeyPath<Root, Value>, rhs: Value) -> Predicate<Root> {</div><div><span class="Apple-tab-span" style="white-space: pre;">                </span>return { $0[keyPath: lhs] < rhs }</div></div><div><span class="Apple-tab-span" style="white-space: pre;">        </span>}</div><div class=""><div><br class=""></div></div><div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func && <Root>(lhs: Predicate<Root>, rhs: Predicate<Root>) -> Predicate<Root> {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>return { lhs($0) && rhs($0) }</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>extension KeyPath where Value: Collection {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>func contains(where predicate: Predicate<Value.Element>) -> Predicate<Value> {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                        </span>return { $0.contains(where: predicate) }</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><br class=""></div><div class="">That gives you:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let isPuppyQualifier = #keyPath(Pet, .type) == .dog && #keyPath(Pet, .age) < 12</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let familyQualifier = #keyPath(Family, .pets).contains(where: isPuppyQualifier)</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let familiesWithPuppies = Family.fetch(editingContext, familyQualifier)</div><div class=""><br class=""></div><div class="">Or, in one line with a more sugary syntax:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let familiesWithPuppies = Family.fetch(editingContext, (.pets).contains(where: .type == .dog && .age < 12))</div></div></div><br class=""><div class="">
<span class="Apple-style-span" style="border-collapse: separate; font-variant-ligatures: normal; font-variant-east-asian: normal; font-variant-position: normal; line-height: normal; border-spacing: 0px;"><div class=""><div style="font-size: 12px; " class="">-- </div><div style="font-size: 12px; " class="">Brent Royal-Gordon</div><div style="font-size: 12px; " class="">Architechies</div></div></span>
</div>
<br class=""></div></blockquote></body></html>