FAQ
I don't want to debate this at length, but this really isn't semantic:

s2 := append(s1[:i], s1[i+1:]...)

Semantic would be a keyword or something else that actually expresses
deletion, e.g.:

delete s1[i];

I understand that's not how slices "work" in Go - because the slice itself
is a value, and not an "object"... but from this example, it seems that's
both good and bad - though most of the time thus far I have thought this
was a good thing, when you want to manipulate an existing value, even for a
simple manipulation like removing an item (or excluding it) this really
doesn't come out legible at all... "parsing" all those parens and brackets
and colons is *hard*...

Any chance Go will ever have something like e.g. macros or "inline"
functions, which could be used to give name (thus adding semantics) to
simple operations like these?

But back to the situation at hand -
you might consider using a custom slice type with methods to make it look
nicer

I really would like that - could you give an example of doing that?

Thanks guys.


On Tue, Mar 18, 2014 at 10:19 PM, Caleb Spare wrote:

Sanjay beat me to describing your issue (you're modifying a copy of
the map slice without saving it back to the map).
On Tue, Mar 18, 2014 at 8:02 PM, Rasmus Schultz wrote:
Yes, I looked at that, it explains how to delete from a slice - isn't that
what I'm doing in my code?

So I just set up a brand new Go 1.2.1 on my new computer tonight, and
checked out my code and ran it - and now, what should be two calls, and was
3 calls on my other system, generates 5 calls on this system!

Could somebody run the test on their setup and compare the output?

=== RUN TestObservable
--- FAIL: TestObservable (0.00 seconds)
observable_test.go:23: LoadEvent handler #1: LoadEvent#1
observable_test.go:30: LoadEvent handler #2: LoadEvent#1
observable_test.go:37: SaveEvent once-handler #1: SaveEvent#1
observable_test.go:44: SaveEvent handler: SaveEvent#1
observable_test.go:44: SaveEvent handler: SaveEvent#2
observable_test.go:55: SaveEvent once-handler #2: SaveEvent#3
observable_test.go:44: SaveEvent handler: SaveEvent#3
observable_test.go:30: LoadEvent handler #2: LoadEvent#2
observable_test.go:30: LoadEvent handler #2: LoadEvent#2
observable_test.go:30: LoadEvent handler #2: LoadEvent#3
observable_test.go:30: LoadEvent handler #2: LoadEvent#3
observable_test.go:76: expected 2 calls to LoadEvent handler #2, got 5
FAIL
exit status 1
FAIL github.com/mindplay-dk/riot 0.512s

Meanwhile, I am none the wiser, and slice deletion, as explained on the wiki
(and here) doesn't make a lick of sense to me.

It's making me feel pretty dumb. This must be the first and least idiomatic
thing I've seen in Go so far - an operation this basic should not make you
feel dumb, and should not require code that is, more or less, void of any
semantics.
It's really a straightforward application of 'append' and certainly
isn't "void of any semantics".

s1 := []string{"a", "b", "c", "d"}
i := 2
s2 := append(s1[:i], s1[i+1:]...) // ["a", "b", "d"]

This works for any (valid) slice index i. All it's saying is that s2
is composed of everything before index i (s1[:i]) and everything after
index i (s1[i+1:]). The ... is necessary because append takes a list
of elements for the second part (it's variadic). Note that this works
fine even if i == 0 or if i == len(s1)-1, in which case s1[:i] or
s1[i+1:] is empty, respectively.

If you were doing a lot of vector operations like this in your code,
you might consider using a custom slice type with methods to make it
look nicer. It doesn't look like there's a lot of that kind of stuff
going on in your linked code, though.

http://play.golang.org/p/IpvC4Vkf8V
I love the simplicity of Go, but this doesn't seem simple at all - it seems
unnecessarily complicated, and I'm more than a a little taken aback by
this... :-/

Any better alternatives - or any hope of this improving in the future?

Thanks,
Rasmus
On Tuesday, March 18, 2014 7:14:42 AM UTC-5, Matthew Zimmerman wrote:

https://code.google.com/p/go-wiki/wiki/SliceTricks

On Tue, Mar 18, 2014 at 7:45 AM, Carlos Castillo <cook...@gmail.com>
wrote:
Example: http://play.golang.org/p/DiwQ7XF3oP
On Tuesday, March 18, 2014 4:39:26 AM UTC-7, Carlos Castillo wrote:

The slice (list) that's being pulled out of the map on line #71 is
not
being assigned back into the map, so it still has the original slice
bounds
(when removing should have shortened it), as a result the last
element
is
duplicated.

You should assign the list back to the map before breaking.
On Monday, March 17, 2014 8:39:21 AM UTC-7, Rasmus Schultz wrote:

Is it working differently if I'm deleting the last item in a slice?

I posted the whole thing here:

https://github.com/mindplay-dk/riot.go



On Sun, Mar 16, 2014 at 11:10 PM, Ross Salas <ross....@gmail.com>
wrote:
Works here too. I deleted item 5. You may want to check value of
listener.id.

http://play.golang.org/p/2NqcLNLBqu


On Sun, Mar 16, 2014 at 8:56 PM, Dan Kortschak
wrote:
Works here http://play.golang.org/p/WNd9wmkq9c

--
You received this message because you are subscribed to the Google
Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it,
send
an email to golang-nuts...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google
Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send
an
email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 13 of 30 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 17, '14 at 3:34a
activeMar 20, '14 at 3:25a
posts30
users14
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase