Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Why does distinct not always return a list?

In the example for distinct elements given:

extract first value by value from "ABBAAC"

when all items are equal, this provides a single element instead of a single element list. I don’t know why, but in my head it seems more flexible to return a single element list.

This is what i ended up using instead, since select always returns a list. There is another workaround here since selecting the first element of each group in () will give (0), the default value, but at least that makes sense.

on uniq x do
  if x~() () else
    t:select list first value by value from x
    t.c0
  end
end
(2 edits) (+1)

I think the simplest way to get the edge cases you want would be using "()," to coerce lists or scalars to lists, and using "() unless" to coerce an empty result with "first" to an empty list.

  (),extract () unless first value by value from "ABBBCD"
("A","B","C","D")
  (),extract () unless first value by value from "AAAA"
("A")
  (),extract () unless first value by value from ""
()

The former coercion is always valid, but the latter does require some care depending on the data; this is the nasty side of unifying nullity and a numeric value:

  (),extract () unless first value by value from 1,1,0,5,1,2,0,1
(1,5,2)

Edit: and another approach entirely would be to use "dict":

  range (11,22,33,0,11,22) dict ()
(11,22,33,0)
  range (11,11) dict ()
(11)
  range (0) dict ()
(0)
  range () dict ()
()

Depending upon the context, you might not even need the "range".

seems like dict is the most foolproof method. unless has the problem of ignoring zeroes.

  (),extract () unless first value by value from 0,0,0
()

Among other things, Decker 1.32 revises the behavior of extract to remove its problematic "automatic de-listing".

extract first value by value from ()

Now returns (), like it ought to have from the beginning.