It does not do the same thing, but due to Swift automatically promoting values as needed to Optionals, you're seeing the same result.
The difference between map and flatMap is that the given transform in one case returns T, in the other T?.
A better snippet for your example would be
var a : Int? = nil
let x = a.flatMap { $0 > 0 ? $0 : nil }
print("x = \(String(describing: x))")
That's right. Because map's generic parameter in inferred as Int?, hence it returns Int??, while flatMap's generic parameter is inferred as Int , hence it returns Int?.
In general, map() transforms each element to another type, leaving the outer wrapper type alone. On the other hand, flatMap() transforms each element to *the wrapper type* (with another element type), and then βflattensβ all the results into a single wrapper.
Here is a summary of how they work for Array, Optional, and a generic wrapper type W: