Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ways to get mutable strings from a Match object's chunks?

Tags:

raku

I'm trying to map the chunks from a Match object to an array of Pairs with mutable string values -which I suppose requires scalarizing the strings. The only way I could figure out how to do this by putting them in square brackets and indexing them:

 my @n =map { $_.key => [$_.value.Str][0] }, G::parse($str).chunks;

Scalarizing with $(..) didn't work nor did cloning and such. There must be a more canonical way. And why didn't $(..) work -isn't that what it's for?

like image 693
wamiks Avatar asked Jun 26 '19 18:06

wamiks


People also ask

How can a mutable string be obtained in Java?

In a mutable string, we can change the value of string and JVM doesn't create a new object. In a mutable string, we can change the value of the string in the same object. To create a mutable string in java, Java has two classes StringBuffer and StringBuilder where String class is used to the immutable string.

Are strings immutable are strings ordered can we slice strings?

The strings in Python are immutable and support the buffer interface. It could be efficient to return not the new strings, but the buffers pointing to the parts of the old string when using slices or the . split() method. However, a new string object is constructed each time.

Does using the += operator to concatenate strings violate Python's string immutability?

It violates the rules of how ID values and += are supposed to work - the ID values produced with the optimization in place would be not only impossible, but prohibited, with the unoptimized semantics - but the developers care more about people who would see bad concatenation performance and assume Python sucks.

What is mutable string in Java?

With Mutable string, we can change the contents of an existing object, which does not create a new object. Therefore mutable strings are those strings whose content can be changed without creating a new object. StringBuffer and StringBuilder are mutable versions of String in java, whereas the java String class is immutable.

Are strings immutable in Python?

One of the basic rules of Python is that strings are immutable. That is, any operations on strings such as adding them together, slicing them, or substituting characters create new objects and leave the old ones behind. This makes references to strings safer, in a sense, from unexpected changes at a distance.

What are immutable objects in Java?

Immutable objects are those objects whose contents cannot be modified once created. Whenever an immutable object’s content is changed, there will be a creation of a new object. How to Use Mutable String in Java?

What should you not do with Python strings?

Things not to do: mutable Python strings. And there are the things you do because you can. One of the basic rules of Python is that strings are immutable. That is, any operations on strings such as adding them together, slicing them, or substituting characters create new objects and leave the old ones behind.


2 Answers

A few things to unpick here:

  • All strings in P6 are immutable. (Thanks Liz. :)) But you probably don't mean you want to get a mutable string. You probably just mean having strings in containers so the contents of those containers can be changed.

  • The => pair constructor does not decontainerize its right hand value if it's in a container. So if bar is a Scalar container that contains a string then foo => bar constructs a pair with its value being that Scalar container containing that string.

  • $(...) is used to parenthesize an expression that is to be treated as singular even if is a plural container. (This mirrors @(...) which is used to parenthesize an expression that is to be treated as plural even if it is a singular container or value.)

  • It's not surprising that you thought $(...) would construct a Scalar container. (After all, %(...) constructs a Hash, so why not?) But instead you must use a Scalar declarator.

  • The most succinct Scalar declarator is for an anonymous state Scalar using $ = .... But @Håkon has used my $ = ... in their answer. Why? Because the {...} closure called by the map retains state between calls of it. If you use just $ = ... then you'd be reusing the same Scalar container for all the pairs. Instead you need my $ = ... to get fresh Scalars for each pair's value.

like image 126
raiph Avatar answered Oct 16 '22 12:10

raiph


You can put the immutable string into a scalar container by doing:

my @n = map { $_.key => my $ = $_.value.Str }, G::parse($str).chunks;

then you can later modify the content of the scalar container (but not the content of the string):

@n[0].value = "Hello";
like image 36
Håkon Hægland Avatar answered Oct 16 '22 13:10

Håkon Hægland