ngs-lang / ngs

Next Generation Shell (NGS)
https://ngs-lang.org/
GNU General Public License v3.0
1.4k stars 43 forks source link

Add ensure(Eachable1, Present) #646

Closed ilyash-b closed 7 months ago

antonio-pedro99 commented 1 year ago

Hi @ilyash-b, could you provide more info on this?

ilyash-b commented 1 year ago

Hi @antonio-pedro99!

In general, ensure(original, pattern) returns a value which would match the pattern. The operation to make the original conform to the pattern should be minimal and obvious.

my_arr = [1,2,3]
my_arr =~ Has(1)  # true
my_arr =~ Has(4)  # false
ensure(my_arr, Has(4))  # [1,2,3,4]
antonio-pedro99 commented 11 months ago

I did not understand this

ensure(my_arr, Has(4))

Returning 1, 2, 3 + the element that is not present.

ilyash-b commented 11 months ago

Not sure I understand your comment. I'll try to explain slightly different and see if it works.

ensure(x, pattern) should return a value that would match the pattern. In other words, ensure(x, pattern) =~ pattern should succeed.

In the example of ensure(my_arr, Has(4)), where 4 is not in my_arr that minimal transformation would be to return a new value with all the elements of my_arr with the item 4 appended.

ilyash-b commented 7 months ago

In dev