Converting Lists


#1

Greetings Mad Scientists, I am wondering if it is possible to convert a list of one type to another type.

I am looking for this kind of conversion:

!list1 : {true false}
list1>>List{Integer} //should return a list that looks like: {1 0}

Alternatively:

!list1 : {true false}
list1 := list1.do[item := item>>Integer] //should return a list that looks like: {1 0}

The first option does not work as the compiler considers the conversion to be redundant,
it ignores the list’s ItemClass and instead tries to convert the list to the class 'List’
which it deems it to already be.

The second option compiles without error, but when run, it returns the original unconverted
list. I assume that the assign operator does not work the way I assume it does when it comes
to using multiple lists of differing ItemClasses. Am I missing a step?

Any help is appreciated

Edit: Will it be possible, sometime in the future, for conversions for specific list types e.g. List{Boolean}, List{Integer} to be manually defined as conversion methods.

I am not sure how this would work as lists aren’t considered to be different depending on their ItemClass and so there would be no place to define the conversion methods within the IDE.


#2

Interesting question. You can remap the items in one type of list to another list that has the item type that you want.

This is probably the simplest current mechanism:

!list1 : {true false}
!list2 : List{Integer}!fill list1.length [list1{idx}>>Integer]
// {1, 0}

Alternatively you can use:

!list1 : {true false}
!list2 : Integer{}
list1.do[list2.append(item>>)]
list2

I’ll also add a new List!map() constructor that will allow you to do the following:

!list1 : {true false}
!list2 : List{Integer}!map list1 [item<>Boolean>>Integer]

Here is what List!map() would look like:

( List list // no item type specified
  (Object item) ItemClass_ item_gen
)
  [
  // Call internal constructor
  List@!()
  
  list.do[append(item_gen(item))]
  ]

The parser could theoretically be enhanced to allow the following, though it wouldn’t be a high priority task so I can’t say when you’d get it. It would iterate over items and recursively call conversion methods to populate a new list with the desired item class types.

!list1 : {true false}
list1>>List{Integer}

Let me know if this helps or if you have any additional questions. :madsci: