Live data from Hacker News

List Comprehension in Swift

duan.ca

11–20 of 32 posts

Re: List Comprehension in Swift

#11

> List comprehension should be no stranger to a Python or (and?) Haskell user. It’s a really compact syntax that deals with Cartesian product [emphasis mine] of lists. Ugh, no. I fail to see where the Cartesian product is in:: [(x,y,z) | x --- @dennisvennink What you feel doesn't matter. It's not a zip.

I think you're being downvoted for being pedantic (and rude, in your edit), but I agree. This is a pretty good example of software engineers adopting mathematical terms they don't quite grasp in order to add an air of rigour to what they write. The statement is unhelpful for anyone that doesn't know what a Cartesian product is and irritatingly inaccurate for anyone who does.

Re: List Comprehension in Swift

#13
List comprehension is a bad idea, IMO. They were an improvement over not having anything like it, but python has improved over it (https://wiki.python.org/moin/Generators)

Generator expressions are the way to go. They would give you the sequence of elements without generating the (potentially enormous) data structure.

From there, methods to reify the items in a sequence would give you your list, array, or dictionary, where needed.

So, for Swift, I wouldn’t use

    Array(1..
but the slightly longer

    Array(for i in 1..
I’m not sure that is easy to fit in the existing parser, though. If it can be fit in, I would allow that code in ‘normal’ nested for loops, too.

Re: List Comprehension in Swift

#14
post #12

Can someone explain the n modulo operation to me?

it's an easy way to filter out odd/even numbers. `n % m` means "divide `n` by `m` and return the remainder". If you divide any even number by 2, the remainder is always 0, if you divide an odd number, it's always 1. Translate that to a boolean and you've got a nice odd/even filter.

Re: List Comprehension in Swift

#15
post #10

I really dislike the proposed way to do this. If this is "Swifty", give me something else. The Haskell list comprehension made sense to me the first time I saw it, but there is no way I'd know what the mess in the article was doing until someone explained to me. Design has to come first. You can't just go with what's "Swifty" if that doesn't convey what's happening in a reasonable fashion.

Do you know Swift...?

Re: List Comprehension in Swift

#16
post #10

I really dislike the proposed way to do this. If this is "Swifty", give me something else. The Haskell list comprehension made sense to me the first time I saw it, but there is no way I'd know what the mess in the article was doing until someone explained to me. Design has to come first. You can't just go with what's "Swifty" if that doesn't convey what's happening in a reasonable fashion.

>The Haskell list comprehension made sense to me the first time I saw it, but there is no way I'd know what the mess in the article was doing until someone explained to me.

Given that this is basic Swift, the complaint makes no sense:

  let a = Array(1..

Re: List Comprehension in Swift

#17
post #9

Earlier quoted context omitted.

A subset of a Cartesian product is not a Cartesian product. inb4, spare me the wisecrack “It's the Cartesian product of itself and no other factors”.

> It’s a really compact syntax that deals with Cartesian product of lists. Couldn't taking the subset of a Cartesian product be considered to be dealing with one?

Exactly. Indeed, from what I recall from relational database theory, joins are usually treated as cartesian products (outer joins) which are then filtered by the join condition.

Re: List Comprehension in Swift

#18
post #9

Earlier quoted context omitted.

> It’s a really compact syntax that deals with Cartesian product of lists. Couldn't taking the subset of a Cartesian product be considered to be dealing with one?

Exactly. Indeed, from what I recall from relational database theory, joins are usually treated as cartesian products (outer joins) which are then filtered by the join condition.

An outer join a Cartesian product is not.

Even if you stick to ordinary (inner) joins, they are pullbacks, not products.

https://en.wikipedia.org/wiki/Pullback

Re: List Comprehension in Swift

#19
post #16
post #10

I really dislike the proposed way to do this. If this is "Swifty", give me something else. The Haskell list comprehension made sense to me the first time I saw it, but there is no way I'd know what the mess in the article was doing until someone explained to me. Design has to come first. You can't just go with what's "Swifty" if that doesn't convey what's happening in a reasonable fashion.

> The Haskell list comprehension made sense to me the first time I saw it, but there is no way I'd know what the mess in the article was doing until someone explained to me. Given that this is basic Swift, the complaint makes no sense: let a = Array(1..

What the heck? Why does it end up like this? I see very little connection, and I've seen list comprehensions in several different languages.
Post reply on HN