[swift-users] Why does withUnsafePointer(to:) require a var argument?

2017-04-26 Thread Rick Mann via swift-users
We have withUnsafePointer(to:) and withUnsafeMutablePointer(to:). Why does the 
first take an inout parameter? The function names imply that the first will not 
modify the pointer (which I take to mean its contents), and it makes it quite 
clunky to pass in constant things.

-- 
Rick Mann
rm...@latencyzero.com


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Any way to enhance C interop "out-of-band?"

2017-04-26 Thread Rick Mann via swift-users
I'm using a third-party C library whose header doesn't properly tag things like 
enums, and so they show up in Swift without their cases. Is there any way to 
write an auxiliary file to help the compiler do the conversion (e.g. something 
in the modulemap file), so that I don't have to modify the distributed header?

-- 
Rick Mann
rm...@latencyzero.com


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] edit array

2017-04-26 Thread J.E. Schotsman via swift-users

On 26 Apr 2017, at 18:13, Ole Begemann  wrote:

> There have been requests for something like this on swift-evolution, e.g. 
> here in the context of the discussion about a `reduce` variant that takes 
> `inout` arguments: 
> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170123/030641.html
>  
> 

Jonathan Hull is indeed asking for the same thing.

> But as far as I know there hasn't been a proposal for adding this. (Is it 
> important enough to have in the standard library? I don't know.)

I do it all the time!

BTW, it seems that the possibility to edit an array in this way isn’t mentioned 
explicitly anywhere in The Swift Programming Language.

Formally it requires two copies, like

myInstanceCopy = array[index]
myInstanceCopy.field = newValue
array[index] =  myInstanceCopy

Jan E.___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] edit array

2017-04-26 Thread Nate Birkholz via swift-users
Relatively straightforward to write this, which usually has meant "no" to 
adding something to the standard library. (Not that I agree with that measure.)

Sent from my iPhone, please excuse brevity and errors

> On Apr 26, 2017, at 11:13 AM, Ole Begemann via swift-users 
>  wrote:
> 
> On 26.04.2017 17:01, J.E. Schotsman via swift-users wrote:
>>> On 26 Apr 2017, at 16:54, Rien  wrote:
>>> 
>>> Agree, though the function should probably be named something like: 
>>> withEach instead of forEach.
>>> Maybe worth a proposal on evolution?
>> Let’s wait until the people at the other side of the big lake have had time 
>> to react.
> 
> There have been requests for something like this on swift-evolution, e.g. 
> here in the context of the discussion about a `reduce` variant that takes 
> `inout` arguments: 
> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170123/030641.html
> 
> An alternative might be to add a variant of `map` to `MutableCollection` that 
> mutates the collection directly, see: 
> https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170123/030643.html.
> 
> But as far as I know there hasn't been a proposal for adding this. (Is it 
> important enough to have in the standard library? I don't know.)
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] edit array

2017-04-26 Thread Ole Begemann via swift-users

On 26.04.2017 17:01, J.E. Schotsman via swift-users wrote:

On 26 Apr 2017, at 16:54, Rien  wrote:

Agree, though the function should probably be named something like: withEach 
instead of forEach.
Maybe worth a proposal on evolution?

Let’s wait until the people at the other side of the big lake have had time to 
react.


There have been requests for something like this on swift-evolution, 
e.g. here in the context of the discussion about a `reduce` variant that 
takes `inout` arguments: 
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170123/030641.html


An alternative might be to add a variant of `map` to `MutableCollection` 
that mutates the collection directly, see: 
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170123/030643.html.


But as far as I know there hasn't been a proposal for adding this. (Is 
it important enough to have in the standard library? I don't know.)


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Memoization in enums

2017-04-26 Thread Tierry Hörmann via swift-users
Hi all

I discovered that memoization can be pretty hard to implement for pure 
functions in enums and I wanted to ask if anyone knows of a good way to 
implement it?
To explain the problem further look at this example of a primitive lazy linked 
list:

public struct LazyList {
let root: LLE

init(_ root: LLE = LLE.End) {
self.root = root
}

public var hd: T? {
get {return root.val()}
}


lazy private var tail: LLE = self.root.tail()()
public var tl: LazyList {
mutating get {
return LazyList(tail)
}
}
}

enum LLE {
case End
indirect case Node(T, () -> LLE)

func val() -> T? {
switch self {
case let .Node(v, _):
return v
default:
return nil
}
}

func tail() -> () -> LLE {
switch self {
case let .Node(_, tl):
return tl
default:
assert(false, "Can't call tail on empty list")
return {self}
}
}
}

For the memoization I need a separate struct which wraps the actual linked 
list. For this primitive list this would work, but as soon as you want to 
implement “count”, it gets very complicated: A call of count would require the 
evaluation of the tail, so it would make sense to memoize the tail (and its 
computed count property). But this way it is not possible, because a struct 
does not allow to have a stored property that references itself. And as I 
cannot have any stored property at all in a enum, I don’t find a way how I 
could implement memoization for a pure function like “tail” in the enum.

So is it somehow possible to memoize the output of a pure function in an enum?

Greetings
Tierry Hoermann___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] edit array

2017-04-26 Thread J.E. Schotsman via swift-users

> On 26 Apr 2017, at 16:54, Rien  wrote:
> 
> Agree, though the function should probably be named something like: withEach 
> instead of forEach.
> Maybe worth a proposal on evolution?

Let’s wait until the people at the other side of the big lake have had time to 
react.

Jan E.

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] edit array

2017-04-26 Thread Rien via swift-users
Agree, though the function should probably be named something like: withEach 
instead of forEach.
Maybe worth a proposal on evolution?

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - A server for websites build in Swift






> On 26 Apr 2017, at 16:47, J.E. Schotsman via swift-users 
>  wrote:
> 
> 
> On 26 Apr 2017, at 16:27, Rien  wrote:
> 
>> To edit the value in the array itself use:
>> 
>> array[index].number += 1
> 
> That requires a for loop.
> Functional programming lets you write for loops more succinctly.
> Why can’t $0 not be used as a reference, like array[index] ?
> I've checked, it’s not among the commonly rejected proposals.
> 
> Jan E.
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] edit array

2017-04-26 Thread J.E. Schotsman via swift-users

On 26 Apr 2017, at 16:27, Rien  wrote:

> To edit the value in the array itself use:
> 
> array[index].number += 1

That requires a for loop.
Functional programming lets you write for loops more succinctly.
Why can’t $0 not be used as a reference, like array[index] ?
I've checked, it’s not among the commonly rejected proposals.

Jan E.

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] edit array

2017-04-26 Thread Rien via swift-users
Because you are (trying) to edit a copy.

To edit the value in the array itself use:

array[index].number += 1

Regards,
Rien

Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Balancingrock
Project: http://swiftfire.nl - A server for websites build in Swift






> On 26 Apr 2017, at 16:15, J.E. Schotsman via swift-users 
>  wrote:
> 
> Simple question: why can’t I edit a variable array with a functional method?
> 
> For example:
> 
> struct TestStruct
>   {
>   var number = 0
>   }
> 
> var array = [TestStruct](repeatElement(TestStruct(), count: 2))
> array.forEach { $0.number += 1 }
> 
> Jan E.
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] edit array

2017-04-26 Thread J.E. Schotsman via swift-users
Simple question: why can’t I edit a variable array with a functional method?

For example:

struct TestStruct
{
var number = 0
}

var array = [TestStruct](repeatElement(TestStruct(), count: 2))
array.forEach { $0.number += 1 }

Jan E.
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users