swift - 'Ambiguous reference' when using joinWithSeparator to intersperse into array -
i'm trying use joinwithseparator insert separator element between elements of array. based on documentation, should able do:
[1, 2, 3].joinwithseparator([0])
to get:
[1, 0, 2, 0, 3]
instead, get:
repl.swift:3:11: error: type of expression ambiguous without more context [1, 2, 3].joinwithseparator([0])
how can this?
joinwithseparator
not work this. input should sequence of sequence i.e.
// swift 2: [[1], [2], [3]].joinwithseparator([0]) // lazy sequence give `[1, 0, 2, 0, 3]`. // swift 3: [[1], [2], [3]].joined(separator: [0])
you intersperse flatmap , drop last separator:
// swift 2 , 3: [1, 2, 3].flatmap { [$0, 0] }.droplast()
Comments
Post a Comment